Handling imports with multiple plugins
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:
- The first plugin will be responsible for wildcard imports and adds an import statement for every possible file matching the wildcard pattern.
- The second plugin will resolve the absolute path of the import statements that started with the
srcalias. - The third plugin will load
.gqland.graphqlfiles. - The last plugin does what you said earlier. It will mark all files that are loaded from the
srcdirectory 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.
Similar to https://github.com/evanw/esbuild/issues/501?
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-me how far with this issue. stuck on the same problem