Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for declaring out field of OnResolveResult in plugin? #3133

Closed
noyobo opened this issue May 25, 2023 · 3 comments
Closed

Support for declaring out field of OnResolveResult in plugin? #3133

noyobo opened this issue May 25, 2023 · 3 comments

Comments

@noyobo
Copy link

noyobo commented May 25, 2023

The file is analyzed in the plugin at build runtime, and I want to specify the output path of the file, like

build.onResolve({ filter: /\.*/, namespace: 'file' }, async (args) => {
    if (isComponent(args.path)) {
		return  {
			path: args.path,
			out: 'dist/components/..'
		}
    }
});

This is very helpful for me to port the webpack build program, which added dependencies at runtime through the SingleEntryPlugin in the previous webpack build program.

@noyobo noyobo changed the title Support for declaring out field of OnResolveArgs in plugin? Support for declaring out field of OnResolveResult in plugin? May 26, 2023
@evanw
Copy link
Owner

evanw commented Jun 9, 2023

This proposed API doesn't make sense for multiple reasons. One reason is that onResolve can be called multiple times for a given input file, so this would be ill-defined if each onResolve call returned a different value (especially because these are called in parallel in a non-deterministic order). So onLoad makes more sense than onResolve since onLoad is only called once per input file. However, you are asking to be able to set the path of an output file, not an input file. So using onLoad doesn't make sense either because that operates on input files, not output files. Since esbuild is a bundler, there isn't a 1:1 mapping between input files and output files.

If you want to set the output path of an entry point, you can do that when you specify the entry point: https://esbuild.github.io/api/#entry-points. Alternatively if you're asking to set the output path of other files (including intermediate code splitting chunks), that is already tracked by #553. In any case I'm closing this issue because I do not plan to make the proposed API change.

@evanw evanw closed this as not planned Won't fix, can't repro, duplicate, stale Jun 9, 2023
@noyobo
Copy link
Author

noyobo commented Jun 12, 2023

In this case, there must be explicit entryPoints. Due to the changing dependencies of some files based on their content, it is necessary to analyze the files and obtain clear entryPoints before each esbuild.build() operation. This requirement cannot be fulfilled within esbuildContext.watch().

// app.js
export function App() {
  return <div>app</div>
}

entryPoints is ['app.js']

When the content of the file changes:

// app.js
import Foo from './foo.component.js';
export function App() {
  return <div>app</div>
}

must reset entryPoints to ['app.js', 'foo.component.js'] and call again esbuild.build() .

I said earlier that implementing the out field in onResolveResult or onLoadResult may not be a good solution. So what better way to support such a demand? @evanw thank you.

@evanw
Copy link
Owner

evanw commented Jun 12, 2023

If you want to change the entry points, then you can change them and restart esbuild's watch mode on a new esbuild context. This cannot be done from within a plugin because each plugin is specific to a single esbuild context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants