rebuild/README.md

127 строки
4.4 KiB
Markdown
Исходник Обычный вид История

2015-05-04 11:44:51 +03:00
## Electron-Rebuild
This executable rebuilds native Node.js modules against the version of Node.js
that your Electron project is using. This allows you to use native Node.js
modules in Electron apps without your system version of Node.js matching exactly
2015-05-05 08:28:55 +03:00
(which is often not the case, and sometimes not even possible).
### How does it work?
Install the package with `--save-dev`:
```sh
npm install --save-dev electron-rebuild
```
Then, whenever you install a new npm package, rerun electron-rebuild:
```sh
./node_modules/.bin/electron-rebuild
```
2015-10-31 18:57:40 +03:00
Or if you're on Windows:
```sh
.\node_modules\.bin\electron-rebuild.cmd
```
2016-08-04 23:03:47 +03:00
If you have a good node-gyp config but you see an error about a missing element on Windows like "Could not load the Visual C++ component "VCBuild.exe" , try to launch electron-rebuild in an npm script:
2016-06-03 13:34:06 +03:00
```json
2016-08-04 23:03:47 +03:00
"scripts": {
"rebuild" : "electron-rebuild -f -w yourmodule"
2016-06-03 13:34:06 +03:00
}
```
2016-08-04 23:03:47 +03:00
2016-06-03 13:34:06 +03:00
and then
2016-08-04 23:03:47 +03:00
2016-06-03 13:34:06 +03:00
```sh
npm run rebuild
```
2016-08-04 23:03:47 +03:00
2015-05-05 08:28:55 +03:00
### How can I integrate this into Grunt / Gulp / Whatever?
electron-rebuild is also a library that you can just require into your app or
build process. It has two main methods:
```js
2015-06-02 01:55:36 +03:00
import { installNodeHeaders, rebuildNativeModules, shouldRebuildNativeModules } from 'electron-rebuild';
// Public: Determines whether we need to rebuild native modules (i.e. if they're
2015-06-02 01:55:36 +03:00
// already compiled for the right version of Electron, no need to rebuild them!)
//
// pathToElectronExecutable - Path to the electron executable that we'll use
2015-06-02 01:55:36 +03:00
// to determine NODE_MODULE_VERSION
// explicitNodeVersion (optional) - If given, use this instead of probing Electron
//
// Returns a Promise that if true, indicates you should build native modules
let shouldBuild = shouldRebuildNativeModules('/path/to/Electron');
2015-05-05 08:28:55 +03:00
// Public: Downloads and installs the header / lib files required to build
// native modules.
//
// nodeVersion - the version of Electron to download headers for
// nodeDistUrl (optional) - the URL to download the distribution from
// headersDir (optional) - where to put the headers
// arch (optional) - The architecture to build against (for building 32-bit apps
2015-06-02 01:55:36 +03:00
// on 64-bit Windows for example)
2015-05-05 08:28:55 +03:00
//
// Returns a Promise indicating whether the operation succeeded or not
let headerResult = installNodeHeaders('1.3.1');
2015-05-05 08:28:55 +03:00
// Public: Rebuilds a node_modules directory with the given Electron version.
//
// nodeVersion - the version of Electron to download headers for
// nodeModulesPath - the path to a node_modules directory
// headersDir (optional) - where to find the headers
// Returns a Promise indicating whether the operation succeeded or not
headerResult.then(() => rebuildNativeModules('1.3.1', './node_modules'));
2015-05-05 08:28:55 +03:00
```
2015-06-02 01:55:36 +03:00
A full build process might look something like:
```js
let childProcess = require('child_process');
let pathToElectron = require('electron-prebuilt');
2015-06-02 01:55:36 +03:00
shouldRebuildNativeModules(pathToElectron)
.then((shouldBuild) => {
if (!shouldBuild) return true;
let electronVersion = childProcess.execSync(`${pathToElectron} --version`, {
encoding: 'utf8',
});
electronVersion = electronVersion.match(/v(\d+\.\d+\.\d+)/)[1];
return installNodeHeaders(electronVersion)
.then(() => rebuildNativeModules(electronVersion, './node_modules'));
2015-06-02 01:55:36 +03:00
})
.catch((e) => {
console.error("Building modules didn't work!");
console.error(e);
});
```
### `node-pre-gyp` workaround
Note that there is a known [issue](https://github.com/mapbox/node-pre-gyp/pull/187) with
`node-pre-gyp` that prevents it from correctly locating the native modules built by
`electron-rebuild`. `node-pre-gyp` is used by some popular NPM packages like `sqlite3`,
and `node-inspector`, so even if your app or package does not have a direct dependency on
`node-pre-gyp` you're bound to run into this issue sooner or later. To work around it call
`preGypFixRun` after the build is complete in order to copy the native modules to a location
where `node-pre-gyp` can find them:
```js
import { preGypFixRun } from 'electron-rebuild';
return installNodeHeaders('1.3.1')
.then(() => rebuildNativeModules('1.3.1', './node_modules'))
.then(() => preGypFixRun('./node_modules', true, pathToElectron));
```
If you're using the CLI to perform the build then use the `-p` or `--pre-gyp-fix` option.
2016-02-11 19:16:11 +03:00
### Alternatives
- [require-rebuild](https://github.com/juliangruber/require-rebuild) patches `require()` to rebuild native node modules on the fly