-
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
node's module.register
+ bundling
#4016
Comments
This was referenced Dec 23, 2024
import loaderJS from '@nodejs-loaders/alias'
// source.js
import loaderJS from '@nodejs-loaders/alias' with { loader: 'bundle-inline' }
import module from 'node:module'
module.register(loaderJS)
// build.js
import {build} from 'esbuild'
build({
entryPoints: ['source.js'],
platform: 'node',
bundle: true,
format: 'esm',
outdir: 'dist',
plugins: [{
name: 'bundle-inline',
setup({ onResolve, onLoad }) {
onResolve({ filter: /(?:)/ }, args => {
if (args.with.loader == 'bundle-inline') {
// Use '.js' extension so that the 'dataurl' loader will prepend correct mime type.
let path = args.path + '.js'
// TODO: pluginData.path = resolve(args.path, args.args.resolveDir)
return { namespace: 'bundle-inline', path: path, pluginData: args }
}
})
onLoad({ namespace: 'bundle-inline', filter: /(?:)/ }, async args => {
let {outputFiles: [{contents}]} = await build({
entryPoints: [args.pluginData.path],
platform: 'node',
bundle: true,
format: 'esm',
minify: true,
write: false,
})
return { contents, loader: 'dataurl' }
})
}
}]
}).catch(() => process.exit()) Note that I'm using import attributes for plugins to pick up as the hint, you can use other ways like query strings in import paths. |
Excellent, thank you! Just what I was looking for 🙂 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
esbuild understandably does not realise the target of
module.register
is a dependency, which needs to be bundled and then referenced (possibly inlined). Is there a way to explicitly tell esbuild to do this?For instance
would be updated to something like
The text was updated successfully, but these errors were encountered: