node-loader
A Node loader module for enhanced-require
Last updated 14 days ago by evilebottnawi .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install node-loader 
SYNC missed versions from official npm registry.

npm node deps tests coverage chat size

node-loader

A Node.js add-ons loader.

Allows to connect native node modules with .node extension.

node-loader only works on the node/electron-main/electron-main targets.

Getting Started

To begin, you'll need to install node-loader:

$ npm install node-loader --save-dev

Setup the target option to node/electron-main/electron-main value and do not mock the __dirname global variable.

webpack.config.js

module.exports = {
  target: 'node',
  node: {
    __dirname: false,
  },
  module: {
    rules: [
      {
        test: /\.node$/,
        loader: 'node-loader',
      },
    ],
  },
};

Inline

index.js

import node from 'node-loader!./file.node';

And run webpack via your preferred method.

Configuration

index.js

import node from 'file.node';

Then add the loader to your webpack config. For example:

webpack.config.js

module.exports = {
  target: 'node',
  node: {
    __dirname: false,
  },
  module: {
    rules: [
      {
        test: /\.node$/,
        loader: 'node-loader',
      },
    ],
  },
};

And run webpack via your preferred method.

Options

Name Type Default Description
flags {Number} undefined Enables/Disables url/image-set functions handling
name {String\|Function} '[contenthash].[ext]' Specifies a custom filename template for the target file(s).

flags

Type: Number Default: undefined

The flags argument is an integer that allows to specify dlopen behavior. See the [process.dlopen][https://nodejs.org/api/process.html#process_process_dlopen_module_filename_flags] documentation for details.

index.js

import node from 'file.node';

webpack.config.js

const os = require('os');

module.exports = {
  target: 'node',
  node: {
    __dirname: false,
  },
  module: {
    rules: [
      {
        test: /\.node$/,
        loader: 'node-loader',
        options: {
          flags: os.constants.dlopen.RTLD_NOW,
        },
      },
    ],
  },
};

name

Type: String|Function Default: '[contenthash].[ext]'

Specifies a custom filename template for the target file(s).

String

webpack.config.js

module.exports = {
  target: 'node',
  node: {
    __dirname: false,
  },
  module: {
    rules: [
      {
        test: /\.node$/,
        loader: 'node-loader',
        options: {
          name: '[path][name].[ext]',
        },
      },
    ],
  },
};

Function

webpack.config.js

module.exports = {
  target: 'node',
  node: {
    __dirname: false,
  },
  module: {
    rules: [
      {
        test: /\.node$/,
        loader: 'node-loader',
        options: {
          name(resourcePath, resourceQuery) {
            // `resourcePath` - `/absolute/path/to/file.js`
            // `resourceQuery` - `?foo=bar`

            if (process.env.NODE_ENV === 'development') {
              return '[path][name].[ext]';
            }

            return '[contenthash].[ext]';
          },
        },
      },
    ],
  },
};

Contributing

Please take a moment to read our contributing guidelines if you haven't yet done so.

CONTRIBUTING

License

MIT

Current Tags

  • 1.0.2                                ...           latest (14 days ago)

5 Versions

  • 1.0.2                                ...           14 days ago
  • 1.0.1                                ...           3 months ago
  • 1.0.0                                ...           4 months ago
  • 0.6.0                                ...           4 years ago
  • 0.5.0                                ...           8 years ago
Downloads
Today 296
This Week 1,749
This Month 5,658
Last Day 341
Last Week 2,244
Last Month 9,413
Dependencies (2)
Dev Dependencies (23)

Copyright 2014 - 2017 © taobao.org |