-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 outfile as a function #553
Comments
In the past I have thought about adding a plugin callback for when a file is emitted, which is sort of like what you're asking for. It would run after the code generation has finished but before the file is written to the file system. If the file is a code splitting chunk, the code generation for other files which import that chunk would potentially be blocked until the plugin completes since the chunk's file path is baked into the import statements in the importing files. I was thinking of an API sort of like this: let examplePlugin = {
name: 'example',
setup(build) {
build.onEmit({ filter: /something/ }, args => {
return { path: args.path, contents: args.contents }
})
},
} Using a plugin for this instead of |
This could be solved by #518, or replace it – though ideally it's a built-in option. |
Either way sounds good, having |
I like the I am building an Something like this would be perfect let htmlPlugin = {
name: 'example',
setup({ onEmit, onLoad }) {
onEmit({ filter: /\.html/ }, (args) => {
const htmlPath = args.path
const jsPath = args.path + '.js'
const scriptSrc = '/' + path.basename(jsPath)
return [
{ path: jsPath, contents: args.contents },
{
path: htmlPath,
content: `
<html>
<body>
<script src="${scriptSrc}" type="module"></script>
</body>
</html>
`,
},
]
})
onLoad({ filter: /\.html$/ }, async (args) => {
const html = await (
await fs.promises.readFile(args.path, {
encoding: 'utf-8',
})
).toString()
const jsUrls = await getHtmlScriptsUrls(html)
const contents = jsUrls
.map((importPath) => `import '${importPath}'`)
.join('\n')
let resolveDir = path.dirname(args.path)
return {
loader: 'js',
contents,
resolveDir,
}
})
},
} |
I believe we need more control over destination file paths, especially in complex cases. For instance, I've got an error require('esbuild').build({
entryPoints: [
'a/foo.js',
'b/foo.js'
],
outdir: 'out',
plugins: [{
name: 'example',
setup({ onResolve, onLoad }) {
onResolve({ filter: /.*/ }, args => ({
namespace: 'example',
path: args.path
}));
onLoad({ namespace: 'example', filter: /.*/ }, args => ({
contents: 'console.log(' + JSON.stringify(args.path) + ')'
}));
}
}]
}) Probably that's a bug (Should I fill a separate issue?). Anyway, in my case, I want to get Suggested solution with
|
Hashing is kind of complicated because of import cycles. Heads up that I am currently planning to use the algorithm described here: #518 (comment). Each output file is assigned a temporary identifier, then each output file is generated with the temporary identifiers of other modules used for import paths, then any |
I'm definitely missing some ability to change the output path. My workflow looks like this:
currently there's no way to do (3) in the esbuild process, as far as I can tell. esbuild will emit something like |
+1 for |
PS - a note for anyone who finds this thread and like me, needs outputs relative to the entrypoint source files, there is a way to do this. For an arbitrary path like
|
PPS - |
It's an interesting workaround, but it doesn't help in my build pipeline because I need to modify part of the filename to strip out some characters.
|
This issue is more important than yarn support. Just sayin' ;) |
I really like this idea of an onEmit function. My problem: => Problem: if my files all have [name]-[hash] format, i cannot distinguish them enough. So for example: all files from Then i can have two nginx rules:
|
As a total newbie and having only two weeks experience transferring to Esbuild, the onEmit() feature looks very promising still for my purposes. @evanw has been very clear in his vision concerning Esbuild future once it is stabilized. However onEmit has been on the radar for quite some time, so the question is if it is still to be implemented or not - would really appreciate some status update. Regards and thanks for your efforts. |
I would like to bring this feature back to attention since it really blocks us from moving to angular 18s app builder based on esbuild and vite, due to security issues. |
In my case I would like to write multiple entry points as revisioned paths with
rev-path
, but my understanding is that this currently isn't possible. My proposition is that theoutfile
can also be a function, which would then work for multiple entry points (where a stringoutfile
only works for one). That function would receive file name and contents so I can output a revisioned file name based on its contents.The only alternative I can see is renaming the path after already writing the file, but I'd like to do it all in one go if possible. 🤞
The text was updated successfully, but these errors were encountered: