Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Nbind binary support #220

Merged
merged 1 commit into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
nbind support
  • Loading branch information
guybedford committed Jan 15, 2019
commit aec49aa68a3b3009e90216bcb351a61b6687b622
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"vue": "^2.5.17",
"vue-server-renderer": "^2.5.17",
"webpack": "5.0.0-alpha.0",
"when": "^3.7.8"
"when": "^3.7.8",
"yoga-layout": "^1.9.3"
}
}
2 changes: 1 addition & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ switch (args._[0]) {
minify: args["--minify"],
externals: args["--external"],
sourceMap: args["--source-map"] || run && args["--minify"],
cacheDirectory: args["--no-cache"] ? false : undefined,
cache: args["--no-cache"] ? false : undefined,
watch: args["--watch"]
}
);
Expand Down
110 changes: 100 additions & 10 deletions src/loaders/relocate-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,70 @@ const pregyp = {
}
};

function getNbind () {
// Adapted from nbind.js
function makeModulePathList(root, name) {
return ([
[root, name],
[root, 'build', name],
[root, 'build', 'Debug', name],
[root, 'build', 'Release', name],
[root, 'out', 'Debug', name],
[root, 'Debug', name],
[root, 'out', 'Release', name],
[root, 'Release', name],
[root, 'build', 'default', name],
[
root,
process.env['NODE_BINDINGS_COMPILED_DIR'] || 'compiled',
process.versions.node,
process.platform,
process.arch,
name
]
]);
}
function findCompiledModule(basePath, specList) {
var resolvedList = [];
var ext = path.extname(basePath);
for (var _i = 0, specList_1 = specList; _i < specList_1.length; _i++) {
var spec = specList_1[_i];
if (ext == spec.ext) {
try {
spec.path = eval('require.resolve(basePath)');
return spec;
}
catch (err) {
resolvedList.push(basePath);
}
}
}
for (var _a = 0, specList_2 = specList; _a < specList_2.length; _a++) {
var spec = specList_2[_a];
for (var _b = 0, _c = makeModulePathList(basePath, spec.name); _b < _c.length; _b++) {
var pathParts = _c[_b];
var resolvedPath = path.resolve.apply(path, pathParts);
try {
spec.path = eval('require.resolve(resolvedPath)');
}
catch (err) {
resolvedList.push(resolvedPath);
continue;
}
return spec;
}
}
return null;
}
function find(basePath = process.cwd()) {
return findCompiledModule(basePath, [
{ ext: '.node', name: 'nbind.node', type: 'node' },
{ ext: '.js', name: 'nbind.js', type: 'emcc' }
]);
}
return { init: find, find: find };
}

function isExpressionReference(node, parent) {
if (parent.type === 'MemberExpression') return parent.computed || node === parent.object;

Expand Down Expand Up @@ -300,7 +364,7 @@ module.exports = function (code) {
let scope = attachScopes(ast, 'scope');

let pathId, pathImportIds = {};
let pregypId, bindingsId;
let pregypId, bindingsId, nbindId;

const shadowDepths = Object.create(null);
shadowDepths.__filename = 0;
Expand Down Expand Up @@ -391,6 +455,10 @@ module.exports = function (code) {
if (shadowDepths[bindingsId] === 0)
vars[bindingsId] = createBindings();
}
if (nbindId) {
if (shadowDepths[nbindId] === 0)
vars[nbindId] = getNbind();
}
for (const pathFn of Object.keys(pathImportIds)) {
if (shadowDepths[pathFn] === 0)
vars[pathFn] = path[pathImportIds[pathFn]];
Expand Down Expand Up @@ -439,15 +507,16 @@ module.exports = function (code) {
// __dirname, __filename, binary only currently as well as require('bindings')(...)
// Can add require.resolve, import.meta.url, even path-like environment variables
if (node.type === 'Identifier' && isExpressionReference(node, parent)) {
if (!shadowDepths[node.name] &&
(node.name === '__dirname' || node.name === '__filename' ||
node.name === pregypId || node.name === bindingsId)) {
staticChildValue = computeStaticValue(node, false);
// if it computes, then we start backtracking
if (staticChildValue) {
staticChildNode = node;
staticChildValueBindingsInstance = staticBindingsInstance;
return this.skip();
if (!shadowDepths[node.name]) {
if (node.name === '__dirname' || node.name === '__filename' ||
node.name === pregypId || node.name === bindingsId) {
staticChildValue = computeStaticValue(node, false);
// if it computes, then we start backtracking
if (staticChildValue) {
staticChildNode = node;
staticChildValueBindingsInstance = staticBindingsInstance;
return this.skip();
}
}
}
}
Expand All @@ -462,6 +531,21 @@ module.exports = function (code) {
return this.skip();
}
}
// nbind.init(...) -> require('./resolved.node')
else if (nbindId && node.type === 'CallExpression' &&
node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'Identifier' &&
node.callee.object.name === nbindId &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name === 'init') {
const bindingInfo = computeStaticValue(node, false);
if (bindingInfo) {
bindingInfo.path = path.relative(path.dirname(id), bindingInfo.path);
transformed = true;
magicString.overwrite(node.start, node.end, `({ bind: require("${bindingInfo.path}").NBind.bind_value, lib: require("${bindingInfo.path}") })`);
return this.skip();
}
}

// require.main -> __non_webpack_require__.main
else if (!isESM && node.type === 'MemberExpression' &&
Expand Down Expand Up @@ -501,6 +585,12 @@ module.exports = function (code) {
shadowDepths[bindingsId] = 0;
return this.skip();
}
// var nbind = require('nbind')
else if (decl.init.arguments[0].value === 'nbind') {
nbindId = decl.id.name;
shadowDepths[nbindId] = 0;
return this.skip();
}
}
// var { join } = path | require('path');
else if (decl.id.type === 'ObjectPattern' && decl.init &&
Expand Down
3 changes: 2 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ for (const integrationTest of fs.readdirSync(__dirname + "/integration")) {
const { code, map, assets } = await ncc(
__dirname + "/integration/" + integrationTest,
{
cache: false
cache: false,
sourceMap: true
}
);
const tmpDir = `${__dirname}/tmp/${integrationTest}/`;
Expand Down
1 change: 1 addition & 0 deletions test/integration/yoga-layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('yoga-layout');
50 changes: 47 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1626,6 +1626,15 @@ auth0@^2.14.0:
rest-facade "^1.10.1"
retry "^0.10.1"

autogypi@^0.2.2:
version "0.2.2"
resolved "https://registry.yarnpkg.com/autogypi/-/autogypi-0.2.2.tgz#258bab5f7857755b09beac6a641fea130ff4622d"
integrity sha1-JYurX3hXdVsJvqxqZB/qEw/0Yi0=
dependencies:
bluebird "^3.4.0"
commander "~2.9.0"
resolve "~1.1.7"

aws-sdk@^2.356.0:
version "2.360.0"
resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.360.0.tgz#f640acbc7de07aa7a381a31b110fc3502fe90288"
Expand Down Expand Up @@ -1986,7 +1995,7 @@ bluebird@^2.10.2:
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-2.11.0.tgz#534b9033c022c9579c56ba3b3e5a5caafbb650e1"
integrity sha1-U0uQM8AiyVecVro7Plpcqvu2UOE=

bluebird@^3.1.1, bluebird@^3.4.1, bluebird@^3.4.6, bluebird@^3.5.0, bluebird@^3.5.1, bluebird@~3.5.1:
bluebird@^3.1.1, bluebird@^3.4.0, bluebird@^3.4.1, bluebird@^3.4.6, bluebird@^3.5.0, bluebird@^3.5.1, bluebird@~3.5.1:
version "3.5.3"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.3.tgz#7d01c6f9616c9a51ab0f8c549a79dfe6ec33efa7"
integrity sha512-/qKPUQlaW1OyR51WeCPBvRnAlnZFUJkCSG5HzGnuIqhgyJtF+T94lFnn33eiazjRm2LAHVy2guNnaq48X9SJuw==
Expand Down Expand Up @@ -2671,6 +2680,13 @@ commander@~2.17.1:
resolved "https://registry.yarnpkg.com/commander/-/commander-2.17.1.tgz#bd77ab7de6de94205ceacc72f1716d29f20a77bf"
integrity sha512-wPMUt6FnH2yzG95SA6mzjQOEKUU3aLaDEmzs1ti+1E9h+CsrZghRlqEM/EJ4KscsQVG8uNN4uVreUeT8+drlgg==

commander@~2.9.0:
version "2.9.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-2.9.0.tgz#9c99094176e12240cb22d6c5146098400fe0f7d4"
integrity sha1-nJkJQXbhIkDLItbFFGCYQA/g99Q=
dependencies:
graceful-readlink ">= 1.0.0"

commondir@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b"
Expand Down Expand Up @@ -3296,6 +3312,11 @@ emojis-list@^2.0.0:
resolved "https://registry.yarnpkg.com/emojis-list/-/emojis-list-2.1.0.tgz#4daa4d9db00f9819880c79fa457ae5b09a1fd389"
integrity sha1-TapNnbAPmBmIDHn6RXrlsJof04k=

emscripten-library-decorator@~0.2.2:
version "0.2.2"
resolved "https://registry.yarnpkg.com/emscripten-library-decorator/-/emscripten-library-decorator-0.2.2.tgz#d035f023e2a84c68305cc842cdeea38e67683c40"
integrity sha1-0DXwI+KoTGgwXMhCze6jjmdoPEA=

encodeurl@~1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/encodeurl/-/encodeurl-1.0.2.tgz#ad3ff4c86ec2d029322f5a02c3a9a606c95b3f59"
Expand Down Expand Up @@ -4467,6 +4488,11 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.15, graceful-fs@^4.1.2, graceful-fs@^4.1.4
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.15.tgz#ffb703e1066e8a0eeaa4c8b80ba9253eeefbfb00"
integrity sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA==

"graceful-readlink@>= 1.0.0":
version "1.0.1"
resolved "https://registry.yarnpkg.com/graceful-readlink/-/graceful-readlink-1.0.1.tgz#4cafad76bc62f02fa039b2f94e9a3dd3a391a725"
integrity sha1-TK+tdrxi8C+gObL5Tpo906ORpyU=

graphql-extensions@0.3.2:
version "0.3.2"
resolved "https://registry.yarnpkg.com/graphql-extensions/-/graphql-extensions-0.3.2.tgz#a19dd62b62d769f4d1b9c4b4781cc353b2174998"
Expand Down Expand Up @@ -7264,6 +7290,15 @@ natural-compare@^1.4.0:
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
integrity sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=

nbind@^0.3.14:
version "0.3.15"
resolved "https://registry.yarnpkg.com/nbind/-/nbind-0.3.15.tgz#20c74d77d54e28627ab8268c2767f7e40aef8c53"
integrity sha512-TrKLNRj5D8wZRJb7XmUNbA1W3iTigAEpm3qaGig5bEWY/iCT2IQBgBc2EUGO59FbRIGhx5hB/McVwqxlSGScVw==
dependencies:
emscripten-library-decorator "~0.2.2"
mkdirp "~0.5.1"
nan "^2.9.2"

needle@^2.2.1:
version "2.2.4"
resolved "https://registry.yarnpkg.com/needle/-/needle-2.2.4.tgz#51931bff82533b1928b7d1d69e01f1b00ffd2a4e"
Expand Down Expand Up @@ -7332,7 +7367,7 @@ node-forge@^0.7.4:
resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.7.6.tgz#fdf3b418aee1f94f0ef642cd63486c77ca9724ac"
integrity sha512-sol30LUpz1jQFBjOKwbjxijiE3b6pjd74YwfD0fJOKPjF+fONKb2Yg8rYgS6+bK6VDl+/wfr4IYpC7jDzLUIfw==

node-gyp@^3.8.0:
node-gyp@^3.6.2, node-gyp@^3.8.0:
version "3.8.0"
resolved "https://registry.yarnpkg.com/node-gyp/-/node-gyp-3.8.0.tgz#540304261c330e80d0d5edce253a68cb3964218c"
integrity sha512-3g8lYefrRRzvGeSowdJKAKyks8oUpLEd/DyPV4eMhVlhJ0aNaZqIrNUIPuEWWTAoPqyFkfGrM67MC69baqn6vA==
Expand Down Expand Up @@ -9168,7 +9203,7 @@ resolve-url@^0.2.1:
resolved "https://registry.yarnpkg.com/resolve-url/-/resolve-url-0.2.1.tgz#2c637fe77c893afd2a663fe21aa9080068e2052a"
integrity sha1-LGN/53yJOv0qZj/iGqkIAGjiBSo=

resolve@1.1.7:
resolve@1.1.7, resolve@~1.1.7:
version "1.1.7"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b"
integrity sha1-IDEU2CrSxe2ejgQRs5ModeiJ6Xs=
Expand Down Expand Up @@ -11438,6 +11473,15 @@ ylru@^1.2.0:
resolved "https://registry.yarnpkg.com/ylru/-/ylru-1.2.1.tgz#f576b63341547989c1de7ba288760923b27fe84f"
integrity sha512-faQrqNMzcPCHGVC2aaOINk13K+aaBDUPjGWl0teOXywElLjyVAB6Oe2jj62jHYtwsU49jXhScYbvPENK+6zAvQ==

yoga-layout@^1.9.3:
version "1.9.3"
resolved "https://registry.yarnpkg.com/yoga-layout/-/yoga-layout-1.9.3.tgz#f851935187f6d2945639b79c57ee0eac2fb7d886"
integrity sha512-DFN0q9IGk/MOkv5/YQurbjWaE2OAzWzM5nKnWFTiJoQEo/7OZ07wtOXuUbZlnSxbwqBhtGc4x2PD4KqBXoVvDg==
dependencies:
autogypi "^0.2.2"
nbind "^0.3.14"
node-gyp "^3.6.2"

zen-observable-ts@^0.8.10:
version "0.8.10"
resolved "https://registry.yarnpkg.com/zen-observable-ts/-/zen-observable-ts-0.8.10.tgz#18e2ce1c89fe026e9621fd83cc05168228fce829"
Expand Down