Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Emit shared libraries along with binary assets #104

Merged
merged 2 commits into from
Dec 4, 2018
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"azure-storage": "^2.10.2",
"bindings": "^1.3.1",
"bytes": "^3.0.0",
"canvas": "^2.2.0",
"codecov": "^3.1.0",
"copy": "^0.3.2",
"core-js": "^2.5.7",
Expand Down
2 changes: 2 additions & 0 deletions src/loaders/node-loader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { getOptions } = require('loader-utils');
const getUniqueAssetName = require('../utils/dedupe-names');
const sharedlibEmit = require('../utils/sharedlib-emit');

module.exports = function (content) {
if (this.cacheable)
Expand All @@ -9,6 +10,7 @@ module.exports = function (content) {
const options = getOptions(this);

const name = getUniqueAssetName(id, options.assetNames);
sharedlibEmit(id, this.emitFile);
this.emitFile(name, content);

return 'module.exports = __non_webpack_require__("./' + name + '")';
Expand Down
6 changes: 6 additions & 0 deletions src/loaders/relocate-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const acorn = require('acorn');
const bindings = require('bindings');
const getUniqueAssetName = require('../utils/dedupe-names');
const { getOptions } = require('loader-utils');
const sharedlibEmit = require('../utils/sharedlib-emit');

// binary support for inlining logic from - node-pre-gyp/lib/pre-binding.js
function isPregypId (id) {
Expand Down Expand Up @@ -143,6 +144,11 @@ module.exports = function (code) {
if (options.assets[assetPath])
return "__dirname + '/" + JSON.stringify(options.assets[assetPath]).slice(1, -1) + "'";

// If the asset is a ".node" binary, then glob for possible shared
// libraries that should also be included
if (assetPath.endsWith('.node'))
sharedlibEmit(assetPath, this.emitFile);

const name = getUniqueAssetName(assetPath, options.assetNames);
options.assets[assetPath] = name;

Expand Down
27 changes: 27 additions & 0 deletions src/utils/sharedlib-emit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const os = require('os');
const path = require('path');
const fs = require('fs');

let libRegEx;
switch (os.platform()) {
case 'darwin':
libRegEx = /\.dylib$/;
break;
case 'win32':
libRegEx = /\.dll$/;
break;
default:
libRegEx = /\.so(\.\d+)?$/;
}

// helper for emitting the associated shared libraries when a binary is emitted
module.exports = function (binaryPath, emitFile) {
const dir = path.dirname(binaryPath);
const files = fs.readdirSync(dir);

const sharedLibs = files.filter(name => name.match(libRegEx));
sharedLibs.forEach(name => {
const libPath = path.resolve(dir, name);
emitFile(name, fs.readFileSync(libPath));
});
};
6 changes: 6 additions & 0 deletions test/integration/canvas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const canvas = require('canvas');
module.exports = () => {
const { createCanvas } = canvas;
const c = createCanvas(200, 200);
const ctx = c.getContext('2d');
};
Loading