├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── index.js ├── package-lock.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # 2.3.0 2 | 3 | - Allow skipping native file emition (cea2ff2d29) 4 | 5 | # 2.2.0 6 | 7 | - Allow adjusting base path for relative locations (d073559673, 64505798ee) 8 | 9 | # 2.1.0 10 | 11 | - Handle the relative "current directory" path when using `rewritePath` 12 | 13 | # 2.0.0 14 | 15 | - Remove `loader-utils` dependency. (5156da688769f861cf657ee5f7087310f7ec6819) 16 | - Remove configurable `name` option. (5156da688769f861cf657ee5f7087310f7ec6819) 17 | - Assume that custom options are a valid hash object. (5156da688769f861cf657ee5f7087310f7ec6819) 18 | 19 | # 1.1.0 20 | 21 | - Add a support for Webpack 4.0.x. (#1 by @matsnow) 22 | 23 | # 1.0.0 24 | 25 | The first version of working loader for native extenstions that supports building a relative path in the runtime. 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Maciej Malecki 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Node Native Loader 2 | 3 | Package for loading native files in Node and Electron applications. The project is inspired by the [node-addon-loader](https://github.com/ushu/node-addon-loader). It works in the similar way but **allows to build path at runtime**. 4 | 5 | ## Installation 6 | 7 | Add the package to the development dependencies: 8 | 9 | ```bash 10 | # using npm: 11 | $ npm install native-ext-loader --save-dev 12 | 13 | # using yarn: 14 | $ yarn add --dev native-ext-loader 15 | ``` 16 | 17 | ## Usage 18 | 19 | Update rules entry in the Webpack configuration file: 20 | 21 | ```js 22 | module: { 23 | rules: [ 24 | { 25 | test: /\.node$/, 26 | loader: "native-ext-loader" 27 | } 28 | ]; 29 | } 30 | ``` 31 | 32 | ## Options 33 | 34 | Options are configurable using `options` hash: 35 | 36 | ```js 37 | module: { 38 | rules: [ 39 | { 40 | test: /\.node$/, 41 | loader: "native-ext-loader", 42 | options: { 43 | rewritePath: path.resolve(__dirname, "dist") 44 | } 45 | } 46 | ]; 47 | } 48 | ``` 49 | 50 | ### `basePath` (default: `[]`) 51 | 52 | It allows adjusting path to the native module. The array will be concatenated with the resource name and then used in the runtime. For example, when the compile application lives inside `app.asar/renderer` subdirectory (Electron package), the path to the native module can be adjusted by using `basePath: ['app.asar', 'renderer']`. 53 | 54 | Note that `basePath` is ignored when `rewritePath` option is used. 55 | 56 | ### `rewritePath` (default: `undefined`) 57 | 58 | It allows to set an absolute paths to native files. 59 | 60 | Note that it needs to remain `undefined` if you are building a package with embedded files. This way, the compiled application will work no matter of its location. This is important when building Electron applications that can be placed in any directory by the end user. 61 | 62 | ### `emit` (default: `true`) 63 | 64 | Specifies whether the imported `.node` file will be copied to the output directory. 65 | 66 | ## Releasing a new version 67 | 68 | 1. Bump version number in the `package.json` and `CHANGELOG.md` files. 69 | 1. Run `npm install` to update `package-lock.json` file. 70 | 1. Commit changes (include changes) 71 | 1. Add a new tag (use `-a` and include changes) 72 | 1. Push commits and tag 73 | 1. Run `npm publish` 74 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var path = require("path"); 2 | 3 | module.exports = function(content) { 4 | const defaultConfig = { 5 | basePath: [], 6 | rewritePath: undefined, 7 | emit: true 8 | }; 9 | 10 | const config = Object.assign(defaultConfig, this.query); 11 | const fileName = path.basename(this.resourcePath); 12 | 13 | if (config.emit) { 14 | if (this.emitFile) { 15 | this.emitFile(fileName, content, false); 16 | } else { 17 | throw new Error("emitFile function is not available"); 18 | } 19 | } 20 | 21 | this.addDependency(this.resourcePath); 22 | 23 | if (config.rewritePath) { 24 | let filePath; 25 | 26 | if (config.rewritePath === "./" || config.rewritePath === ".\\") { 27 | filePath = JSON.stringify(config.rewritePath + fileName); 28 | } else { 29 | filePath = JSON.stringify(path.join(config.rewritePath, fileName)); 30 | } 31 | 32 | return ( 33 | "try { global.process.dlopen(module, " + 34 | filePath + 35 | "); } " + 36 | "catch(exception) { throw new Error('Cannot open ' + " + 37 | filePath + 38 | " + ': ' + exception); };" 39 | ); 40 | } else { 41 | const filePathArray = config.basePath.concat(fileName); 42 | const filePath = JSON.stringify(filePathArray).slice(1, -1); 43 | 44 | return ( 45 | "const path = require('path');" + 46 | "const filePath = path.resolve(__dirname, " + 47 | filePath + 48 | ");" + 49 | "try { global.process.dlopen(module, filePath); } " + 50 | "catch(exception) { throw new Error('Cannot open ' + filePath + ': ' + exception); };" 51 | ); 52 | } 53 | }; 54 | 55 | module.exports.raw = true; 56 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "native-ext-loader", 3 | "version": "2.3.0", 4 | "lockfileVersion": 1 5 | } 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": "Maciej Małecki ", 3 | "dependencies": {}, 4 | "description": "Loader for Node native extensions", 5 | "keywords": [ 6 | "electron", 7 | "loader", 8 | "native", 9 | "node", 10 | "webpack" 11 | ], 12 | "license": "MIT", 13 | "main": "index.js", 14 | "name": "native-ext-loader", 15 | "repository": "https://github.com/smt116/node-native-loader", 16 | "scripts": {}, 17 | "version": "2.3.0" 18 | } 19 | --------------------------------------------------------------------------------