node-loader icon indicating copy to clipboard operation
node-loader copied to clipboard

Need to copy and change names of .node files when dependency is built and ready to be used but in package.json as file:

Open jhg opened this issue 4 years ago • 2 comments

  • Operating System: Windows 10
  • Node Version: 15.3.0
  • NPM Version: 7.0.14
  • Yarn Version: 1.22.10
  • webpack Version: 4.46.0
  • node-loader Version: 1.0.2

Expected Behavior

Copy the index.*.node file to webpack_output directory and maybe it is good idea rename that from index (name in some native modules) to other to avoid collision

Actual Behavior

TypeError: Can not find node binding files from kobadonga-win32-x64-gnu, kobadonga-win32-x64-msvc and C:\Users\user\Documents\Repos\example-issue-node-loader\example\webpack_output\main\index.win32-x64-gnu.node, C:\Users\user\Documents\Repos\example-issue-node-loader\example\webpack_output\main\index.win32-x64-msvc.node
    at loadBinding (webpack-internal:///./node_modules/@node-rs/helper/lib/loader.js:27:11)
    at eval (webpack-internal:///./node_modules/kobadonga/index.js:3:18)
    at Object../node_modules/kobadonga/index.js (C:\Users\user\Documents\Repos\example-issue-node-loader\example\webpack_output\main\index.js:143:1)
    at __webpack_require__ (C:\Users\user\Documents\Repos\example-issue-node-loader\example\webpack_output\main\index.js:21:30)
    at eval (webpack-internal:///./src/main.ts:43:35)
    at Object../src/main.ts (C:\Users\user\Documents\Repos\example-issue-node-loader\example\webpack_output\main\index.js:155:1)
    at __webpack_require__ (C:\Users\user\Documents\Repos\example-issue-node-loader\example\webpack_output\main\index.js:21:30)
    at C:\Users\user\Documents\Repos\example-issue-node-loader\example\webpack_output\main\index.js:85:18
    at Object.<anonymous> (C:\Users\user\Documents\Repos\example-issue-node-loader\example\webpack_output\main\index.js:88:10)
    at Module._compile (internal/modules/cjs/loader.js:1152:30)

Code

// webpack.config.js
const path = require('path');
const rules = require('./webpack.rules');
const plugins = require('./webpack.plugins').mainPlugins;

module.exports = {
  entry: './src/main.ts',
  target: 'electron-main',
  node: {
    __dirname: false,
  },
  module: {
    rules,
  },
  plugins,
  resolve: {
    alias: {
      '*': path.resolve(__dirname, 'node_modules'),
    },
    extensions: ['.js', '.ts', '.jsx', '.tsx', '.css', '.json'],
  },
  devtool: 'cheap-module-eval-source-map',
  output: {
    path: path.resolve(__dirname, 'webpack_output/main'),
  },
};
// webpack.rules.js
module.exports = [
  // Add support for native node modules
  {
    test: /\.node$/,
    loader: 'node-loader',
  },
  {
    test: /\.(m?js|node)$/,
    parser: { amd: false },
    use: {
      loader: '@marshallofsound/webpack-asset-relocator-loader',
      options: {
        outputAssetBase: 'native_modules',
      },
    },
  },
  {
    test: /\.tsx?$/,
    exclude: /(node_modules|\.webpack)/,
    use: {
      loader: 'ts-loader',
      options: {
        transpileOnly: true,
      },
    },
  },
];

How Do We Reproduce?

Download the zip and the readme file has instructions to build and reproduce it:

example-issue-node-loader.zip

The native dependency is built first (using NAPI-rs) and it put the index.*.node in the package directory, in package.json files include *.node. The dependency is in the other package.json as a file: dependency, and can find the *.js file but do nothing about the native module file.

Additional information

I realize now about the .node file is not copied inside node_modules subdirectory even it is in package.json files property. Maybe because it is defined as file: dependency instead of a gzip.

Another try

Packing using yarn pack and using that in the file: path the .node file is in node_modules but the error is same.

jhg avatar Feb 04 '21 13:02 jhg

Looking in the code of this repository I see this only load but don't copy native files, is that the expected behaviour and then the issue is in https://github.com/vercel/webpack-asset-relocator-loader or https://github.com/marshallofsound/webpack-asset-relocator-loader loader?

jhg avatar Feb 04 '21 15:02 jhg

I think yes, our logic is simple https://github.com/webpack-contrib/node-loader/blob/master/src/index.js#L25

alexander-akait avatar Jun 02 '21 15:06 alexander-akait