esbuild icon indicating copy to clipboard operation
esbuild copied to clipboard

Handling imports with multiple plugins

Open hmak-dev opened this issue 4 years ago • 4 comments

There are cases that we need multiple plugins to handle some import statements. For example, an alias plugin that resolves the paths to the alias imports. The result of this plugin has to be visible to all other plugins that have a similar filter RegExp. The following build script describes the situation:

Build Script:

await esbuild.build({
    entryPoints: await fastGlob('src/**/*.(js|ts)'),
    external: [...Object.keys(pkg.dependencies || {})],
    outbase: 'src',
    outdir: 'lib',
    platform: 'node',
    bundle: true,
    keepNames: true,
    plugins: [
        wildcardImport(),
        aliasImport({
            src: './src',
        }),
        graphqlImport(),
        makeExternal(),
    ],
});

Description:

  1. The first plugin will be responsible for wildcard imports and adds an import statement for every possible file matching the wildcard pattern.
  2. The second plugin will resolve the absolute path of the import statements that started with the src alias.
  3. The third plugin will load .gql and .graphql files.
  4. The last plugin does what you said earlier. It will mark all files that are loaded from the src directory as external modules to prevent them from bundling. It also provides a relative path to the imported module calculated from the location of the importer module.

Problem: The aliasImport plugin does its job pretty well, but it will end just right there. The modules processed in this plugin, will not be visible for the next plugins. They won't be passed to graphqlImport and makeExternal plugins.

I think there must be a way to let other plugins access the previously processed files.

hmak-dev avatar May 01 '21 12:05 hmak-dev

Similar to https://github.com/evanw/esbuild/issues/501?

unlocomqx avatar May 16 '21 15:05 unlocomqx

Similar to #501?

I was following the discussions on that issue. But, rebuilding the project again is not a clean and proper solution to this kind of problem. As a webpack user, I expected all of my plugins to be executed in the order of their placement in the build script one after another. I think there must be a better solution to achieve this without building the project again.

@evanw If you think it can't be done with the current version of esbuild, I would love to participate and make a pull request.

hmak-dev avatar May 23 '21 05:05 hmak-dev

@hmak-me how far with this issue. stuck on the same problem

henrhie avatar Jul 28 '22 09:07 henrhie