Skip to content

Commit

Permalink
Incorporate webpack compatibility fix (mapbox#622)
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Perry committed Jul 30, 2024
1 parent d5b7a13 commit 6df32bd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
7 changes: 7 additions & 0 deletions lib/node-pre-gyp.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ Object.defineProperty(exports, 'find', {
enumerable: true
});

Object.defineProperty(exports, 'findUsingPackageJson', {
get: function() {
return require('./pre-binding').findUsingPackageJson;
},
enumerable: true
});

// in the following, "my_module" is using node-pre-gyp to
// prebuild and install pre-built binaries. "main_module"
// is using "my_module".
Expand Down
20 changes: 11 additions & 9 deletions lib/pre-binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ exports.validate = function(package_json, opts) {
versioning.validate_config(package_json, opts);
};

exports.findUsingPackageJson = function(package_json, opts) {
versioning.validate_config(package_json, opts);
let napi_build_version;
if (napi.get_napi_build_versions(package_json, opts)) {
napi_build_version = napi.get_best_napi_build_version(package_json, opts);
}

return versioning.evaluate(package_json, opts, napi_build_version);
};

exports.find = function(package_json_path, opts) {
if (!existsSync(package_json_path)) {
throw new Error(package_json_path + 'does not exist');
Expand All @@ -22,13 +32,5 @@ exports.find = function(package_json_path, opts) {
prog.setBinaryHostProperty();
const package_json = prog.package_json;

versioning.validate_config(package_json, opts);
let napi_build_version;
if (napi.get_napi_build_versions(package_json, opts)) {
napi_build_version = napi.get_best_napi_build_version(package_json, opts);
}
opts = opts || {};
if (!opts.module_root) opts.module_root = path.dirname(package_json_path);
const meta = versioning.evaluate(package_json, opts, napi_build_version);
return meta.module;
return exports.findUsingPackageJson(package_json, opts);
};
6 changes: 3 additions & 3 deletions lib/util/versioning.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,14 @@ module.exports.evaluate = function(package_json, options, napi_build_version) {
const validModuleName = opts.module_name.replace('-', '_');
const host = process.env['npm_config_' + validModuleName + '_binary_host_mirror'] || package_json.binary.host;
opts.host = fix_slashes(eval_template(host, opts));
opts.module_path = eval_template(package_json.binary.module_path, opts);
opts.module_relative_path = eval_template(package_json.binary.module_path, opts);
// now we resolve the module_path to ensure it is absolute so that binding.gyp variables work predictably
if (options.module_root) {
// resolve relative to known module root: works for pre-binding require
opts.module_path = path.join(options.module_root, opts.module_path);
opts.module_path = path.join(options.module_root, opts.module_relative_path);
} else {
// resolve relative to current working directory: works for node-pre-gyp commands
opts.module_path = path.resolve(opts.module_path);
opts.module_path = path.resolve(opts.module_relative_path);
}
opts.module = path.join(opts.module_path, opts.module_name + '.node');
opts.remote_path = package_json.binary.remote_path ? drop_double_slashes(fix_slashes(eval_template(package_json.binary.remote_path, opts))) : default_remote_path;
Expand Down
10 changes: 9 additions & 1 deletion test/app1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@ var path = require('path')
var binding_path = binary.find(path.resolve(path.join(__dirname,'./package.json')));
var binding = require(binding_path);

require('assert').equal(binding.hello(),"hello");
require('assert').equal(binding.hello(),"hello");

var binding_path_using_package_json = binary.findUsingPackageJson(require('./package.json'));
var binding_using_package_json = require('./' + path.join(
binding_path_using_package_json.module_relative_path,
binding_path_using_package_json.module_name
) + '.node');

require('assert').equal(binding_using_package_json.hello(),"hello");

0 comments on commit 6df32bd

Please sign in to comment.