-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Loader option appear to match any part of the filename #1061
Comments
I think there might be something else going on. Doing what you described above works fine for me on both macOS and Windows. Here's what I tried (install const nativeNodeModulesPlugin = {
name: 'native-node-modules',
setup(build) {
// If a ".node" file is imported within a module in the "file" namespace, resolve
// it to an absolute path and put it into the "node-file" virtual namespace.
build.onResolve({ filter: /\.node$/, namespace: 'file' }, args => ({
path: require.resolve(args.path, { paths: [args.resolveDir] }),
namespace: 'node-file',
}))
// Files in the "node-file" virtual namespace call "require()" on the
// path from esbuild of the ".node" file in the output directory.
build.onLoad({ filter: /.*/, namespace: 'node-file' }, args => ({
contents: `
import path from ${JSON.stringify(args.path)}
try { module.exports = require(path) }
catch {}
`,
}))
// If a ".node" file is imported within a module in the "node-file" namespace, put
// it in the "file" namespace where esbuild's default loading behavior will handle
// it. It is already an absolute path since we resolved it to one above.
build.onResolve({ filter: /\.node$/, namespace: 'node-file' }, args => ({
path: args.path,
namespace: 'file',
}))
// Tell esbuild's default loading behavior to use the "file" loader for
// these ".node" files.
let opts = build.initialOptions
opts.loader = opts.loader || {}
opts.loader['.node'] = 'file'
},
}
require('esbuild').build({
entryPoints: ['threads'],
bundle: true,
outdir: 'out',
plugins: [nativeNodeModulesPlugin],
}) The plugin code was copied verbatim from #1051 (comment). Is there anything about this code that is different from what you're doing? Or can you provide code code to reproduce the issue? |
You're right that it's probably something else. I can consistently reproduce inside the project I found it in, but your code works for me and I couldn't reproduce it outside. |
@evanw how to use this code with vite and js to load .node file ? |
I don’t use Vite at all. You are welcome to ask that question on their forum yourself. |
I tried to use the work-around from this issue, which adds a plugin for ".node", to load a native extension:
#1051
This worked great, but failed after I tried to add the "threads" npm package and build my application:
This line which sets the default loader appears to have matched a file in that library named
implementation.node.js
:Meanwhile the plug-in API allows regular expressions and appears to have not matched it, causing an error:
Removing the
$
from the regular expressions prevents the error (but still breaks my application because that file is not assigned to the correct loader)Is there a way to make the "loader" option only match files ending with that extension?
I'm using version 0.10.0
The text was updated successfully, but these errors were encountered: