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

Loader option appear to match any part of the filename #1061

Closed
parkertomatoes opened this issue Mar 26, 2021 · 4 comments
Closed

Loader option appear to match any part of the filename #1061

parkertomatoes opened this issue Mar 26, 2021 · 4 comments

Comments

@parkertomatoes
Copy link

parkertomatoes commented Mar 26, 2021

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:

 > node-file:<PATH>\node_modules\threads\dist\master\implementation.node.js:2:25: error: Could not resolve "<PATH>\\node_modules\\threads\\dist\\master\\implementation.node.js" (the plugin "native-node-modules" didn't set a resolve directory)
    2 │         import path from "<PATH>\\node_modules\\threads\\dist\\master\\implementation.node.js"

This line which sets the default loader appears to have matched a file in that library named implementation.node.js:

    opts.loader['.node'] = 'file'

Meanwhile the plug-in API allows regular expressions and appears to have not matched it, causing an error:

    build.onResolve({ filter: /\.node$/, namespace: 'file' }, args => ({

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

@evanw
Copy link
Owner

evanw commented Mar 26, 2021

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 esbuild and threads before running):

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?

@parkertomatoes
Copy link
Author

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.

@ibilux
Copy link

ibilux commented Aug 31, 2022

@evanw how to use this code with vite and js to load .node file ?

@evanw
Copy link
Owner

evanw commented Aug 31, 2022

I don’t use Vite at all. You are welcome to ask that question on their forum yourself.

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

3 participants