Skip to content

Commit bfe7177

Browse files
kunalspathakxzyfer
authored andcommitted
Enable building for node-chakracore
When building node-sass and libsass using `npm build .` or `node scripts/build -f`, script uses `node-gyp` version installed as part of `npm install` step inside `node_modules` folder. However this version of `node-gyp` is not compatible with [node-chakracore](https://github.com/nodejs/node-chakracore). It will become partially compatible with node-chakracore once nodejs/node-gyp#873 is merged. However in order to fully work with node-chakracore build need to use the `node-gyp` version that is installed with node-chakracore. This change detects if `npm build` was ran from `Node.js chakracore command prompt` and if yes, use the `node-gyp` that comes with node-chakracore instead of using from `node_modules`.
1 parent 61d7e1c commit bfe7177

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

scripts/build.js

+48-8
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,20 @@ function afterBuild(options) {
5656
*/
5757

5858
function build(options) {
59-
var args = [require.resolve(path.join('node-gyp', 'bin', 'node-gyp.js')), 'rebuild', '--verbose'].concat(
60-
['libsass_ext', 'libsass_cflags', 'libsass_ldflags', 'libsass_library'].map(function(subject) {
61-
return ['--', subject, '=', process.env[subject.toUpperCase()] || ''].join('');
62-
})).concat(options.args);
63-
64-
console.log('Building:', [process.execPath].concat(args).join(' '));
65-
66-
var proc = spawn(process.execPath, args, {
59+
var nodeGyp = resolveNodeGyp();
60+
var nodeGypArgs = nodeGyp.args.concat([
61+
'rebuild',
62+
'--verbose',
63+
'--libsass_ext=' + (process.env['LIBSASS_EXT'] || ''),
64+
'--libsass_cflags=' + (process.env['LIBSASS_CFLAGS'] || ''),
65+
'--libsass_ldflags=' + (process.env['LIBSASS_LDFLAGS'] || ''),
66+
'--libsass_library=' + (process.env['LIBSASS_LIBRARY'] || ''),
67+
])
68+
.concat(options.args);
69+
70+
console.log(['Building:', nodeGyp.exeName].concat(nodeGypArgs).join(' '));
71+
72+
var proc = spawn(nodeGyp.exeName, nodeGypArgs, {
6773
stdio: [0, 1, 2]
6874
});
6975

@@ -83,6 +89,40 @@ function build(options) {
8389
});
8490
}
8591

92+
/**
93+
* Resolve node-gyp command to invoke
94+
*
95+
* @api private
96+
*/
97+
function resolveNodeGyp() {
98+
if (process.jsEngine === 'chakracore') {
99+
// For node-chakracore, check if node-gyp is in the path.
100+
// If yes, use it instead of using node-gyp directly from
101+
// node_modules because the one in node_modules is not
102+
// compatible with node-chakracore.
103+
var nodePath = path.dirname(process.execPath);
104+
var nodeGypName = process.platform === 'win32' ? 'node-gyp.cmd' : 'node-gyp';
105+
var globalNodeGypBin = process.env.Path.split(';').filter(function(envPath) {
106+
return envPath.startsWith(nodePath) &&
107+
envPath.endsWith('node-gyp-bin') &&
108+
fs.existsSync(path.resolve(envPath, nodeGypName));
109+
});
110+
111+
if (globalNodeGypBin.length !== 0) {
112+
return { exeName: 'node-gyp', args: [] };
113+
}
114+
115+
console.error('node-gyp incompatible with node-chakracore! Please use node-gyp installed with node-chakracore.');
116+
process.exit(1);
117+
}
118+
119+
var localNodeGypBin = require.resolve(path.join('node-gyp', 'bin', 'node-gyp.js'));
120+
121+
return {
122+
exeName: process.execPath,
123+
args: [localNodeGypBin],
124+
};
125+
}
86126
/**
87127
* Parse arguments
88128
*

0 commit comments

Comments
 (0)