evanw / esbuild

An extremely fast bundler for the web
https://esbuild.github.io/
MIT License
37.58k stars 1.11k forks source link

esm import cjs package with .node file require return a path string #3809

Closed wszgrcy closed 1 week ago

wszgrcy commented 1 week ago

like sharp package. I use config

  loader: {
      '.node': 'file',
    },

when I import (Imported by other packages),after build ,The file sharp-linux-x64-DQNVFNDI.node has been successfully extracted in sharp package

  module.exports = require(`../build/Release/sharp-${platformAndArch}.node`);

but module2.exports return a path string../sharp-linux-x64-DQNVFNDI.node

module2.exports = globRequire_build_Release_sharp_node(`../build/Release/sharp-${platformAndArch}.node`);

I try to use https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487 but not work Is it because 'require' was not captured in the ESM?

evanw commented 1 week ago

Please read the “getting started” instructions in the documentation: https://esbuild.github.io/getting-started/#bundling-for-node:

You also may not want to bundle your dependencies with esbuild. There are many node-specific features that esbuild doesn't support while bundling such as __dirname, import.meta.url, fs.readFileSync, and *.node native binary modules. You can exclude all of your dependencies from the bundle by setting packages to external:

esbuild app.jsx --bundle --platform=node --packages=external
wszgrcy commented 1 week ago

Please read the “getting started” instructions in the documentation: https://esbuild.github.io/getting-started/#bundling-for-node:

You also may not want to bundle your dependencies with esbuild. There are many node-specific features that esbuild doesn't support while bundling such as __dirname, import.meta.url, fs.readFileSync, and *.node native binary modules. You can exclude all of your dependencies from the bundle by setting packages to external:

esbuild app.jsx --bundle --platform=node --packages=external

Can 'dynamic require' be supported in 'onResolve' to allow users to set their own parsing method?

evanw commented 1 week ago

The sharp package is not compatible with bundlers. You should use --packages=external so you don't bundle the sharp package. You don't need to use onResolve to solve this problem.

wszgrcy commented 1 week ago

The sharp package is not compatible with bundlers. You should use --packages=external so you don't bundle the sharp package. You don't need to use onResolve to solve this problem.

thanks