Skip to content

Commit

Permalink
Loopback PR redux (#122)
Browse files Browse the repository at this point in the history
* failing loopback test

* fix and expand directory emissions

* cowsay test works

* remove unused regex
  • Loading branch information
guybedford authored and rauchg committed Dec 7, 2018
1 parent f9c8a61 commit b042bd8
Show file tree
Hide file tree
Showing 10 changed files with 491 additions and 64 deletions.
File renamed without changes.
3 changes: 2 additions & 1 deletion jest.config.js
@@ -1,5 +1,6 @@
module.exports = {
collectCoverageFrom: ["src/**/*.js"],
coverageReporters: ["html", "lcov"],
testEnvironment: "node"
testEnvironment: "node",
testMatch: ["<rootDir>/test/?(*.)+(spec|test).js?(x)"]
};
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -55,6 +55,7 @@
"koa": "^2.6.2",
"leveldown": "^4.0.1",
"loader-utils": "^1.1.0",
"loopback": "^3.24.0",
"magic-string": "^0.25.1",
"mailgun": "^0.5.0",
"mariadb": "^2.0.1-beta",
Expand All @@ -81,7 +82,6 @@
"shebang-loader": "^0.0.1",
"source-map-support": "^0.5.9",
"stripe": "^6.15.0",
"strong-globalize": "^4.1.2",
"terser": "^3.11.0",
"the-answer": "^1.0.0",
"ts-loader": "^5.3.1",
Expand Down
2 changes: 1 addition & 1 deletion scripts/build.js
Expand Up @@ -69,7 +69,7 @@ async function main() {
// copy webpack buildin
await copy(
__dirname + "/../node_modules/webpack/buildin/*.js",
__dirname + "/../dist/buildin/"
__dirname + "/../dist/ncc/buildin/"
);

for (const file of await glob(__dirname + "/../dist/**/*.js")) {
Expand Down
67 changes: 32 additions & 35 deletions src/loaders/relocate-loader.js
Expand Up @@ -9,6 +9,7 @@ const bindings = require('bindings');
const getUniqueAssetName = require('../utils/dedupe-names');
const { getOptions } = require('loader-utils');
const sharedlibEmit = require('../utils/sharedlib-emit');
const glob = require('glob');

// binary support for inlining logic from - node-pre-gyp/lib/pre-binding.js
function isPregypId (id) {
Expand Down Expand Up @@ -207,9 +208,9 @@ function handleWrappers (ast, scope, magicString, len) {
}

const relocateRegEx = /_\_dirname|_\_filename|require\.main|node-pre-gyp|bindings|define/;
const pkgNameRegEx = /(@[^\\\/]+[\\\/])?[^\\\/]+/;

module.exports = function (code) {
this.async();
const id = this.resourcePath;

if (id.endsWith('.json') || !code.match(relocateRegEx))
Expand All @@ -234,8 +235,12 @@ module.exports = function (code) {
options.assets[assetPath] = name;

// console.log('Emitting ' + assetPath + ' for module ' + id);

this.emitFile(name, fs.readFileSync(assetPath));
assetEmissionPromises = assetEmissionPromises.then(async () => {
const source = await new Promise((resolve, reject) =>
fs.readFile(assetPath, (err, source) => err ? reject(err) : resolve(source))
);
this.emitFile(name, source);
});
return "__dirname + '/" + JSON.stringify(name).slice(1, -1) + "'";
};
const emitAssetDirectory = (assetDirPath) => {
Expand All @@ -245,33 +250,26 @@ module.exports = function (code) {
const dirName = path.basename(assetDirPath);
const name = getUniqueAssetName(dirName, options.assetNames);
options.assets[assetDirPath] = name;

console.log('Emitting directory ' + assetDirPath + " as " + name);
for (const file of fs.readdirSync(assetDirPath)) {
let source;
try {
source = fs.readFileSync(assetDirPath + '/' + file);
}
catch (e) {
// nested directories not yet supported
continue;
}
this.emitFile(name + '/' + file, source);
}

return "__dirname + '/" + JSON.stringify(options.assets[assetDirPath]).slice(1, -1) + "'";
assetEmissionPromises = assetEmissionPromises.then(async () => {
const files = await new Promise((resolve, reject) =>
glob(assetDirPath + '/**/*', { mark: true }, (err, files) => err ? reject(err) : resolve(files))
);
await Promise.all(files.map(async file => {
// dont emit empty directories
if (file.endsWith('/'))
return;
const source = await new Promise((resolve, reject) =>
fs.readFile(file, (err, source) => err ? reject(err) : resolve(source))
);
this.emitFile(name + file.substr(assetDirPath.length), source);
}));
});

return "__dirname + '/" + JSON.stringify(name).slice(1, -1) + "'";
};

// determined the node_modules package folder as pkgBase
let pkgBase = '';
const pkgIndex = id.lastIndexOf('node_modules');
if (pkgIndex !== -1 &&
(id[pkgIndex - 1] === '/' || id[pkgIndex - 1] === '\\') &&
(id[pkgIndex + 12] === '/' || id[pkgIndex + 12] === '\\')) {
const pkgNameMatch = id.substr(pkgIndex + 13).match(pkgNameRegEx);
if (pkgNameMatch)
pkgBase = id.substr(0, pkgIndex + 13 + pkgNameMatch[0].length);
}
let assetEmissionPromises = Promise.resolve();

const magicString = new MagicString(code);

Expand Down Expand Up @@ -559,10 +557,7 @@ module.exports = function (code) {
magicString.overwrite(staticChildNode.start, staticChildNode.end, replacement);
}
}
else if (stats && stats.isDirectory() &&
// dont emit __dirname or package base
staticChildValue !== path.dirname(id) &&
staticChildValue !== pkgBase) {
else if (stats && stats.isDirectory()) {
let replacement = emitAssetDirectory(path.resolve(staticChildValue));
if (replacement) {
transformed = true;
Expand All @@ -577,8 +572,10 @@ module.exports = function (code) {
if (!transformed)
return this.callback(null, code);

code = magicString.toString();
const map = magicString.generateMap();

this.callback(null, code, map);
assetEmissionPromises.then(() => {
code = magicString.toString();
const map = magicString.generateMap();

this.callback(null, code, map);
});
};
2 changes: 1 addition & 1 deletion test/integration/cowsay.js
@@ -1,4 +1,4 @@
const { say } = require("cowsay/build/cowsay.umd.js");
const { say } = require("cowsay");

const nate = say({ text: "nate" });
if (!(nate.indexOf("nate") > 0)) {
Expand Down
1 change: 1 addition & 0 deletions test/integration/loopback.js
@@ -0,0 +1 @@
require("loopback");
1 change: 0 additions & 1 deletion test/integration/strong-globalize.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/unit/asset-fs-inline-path-shadow/output.js
Expand Up @@ -96,7 +96,7 @@ console.log(fs.readFileSync(__dirname + '/asset.txt', 'utf8'));

(function () {
var join = () => 'nope';
console.log(fs.readFileSync(join(__dirname, 'asset.txt'), 'utf8'));
console.log(fs.readFileSync(join(__dirname + '/asset-fs-inline-path-shadow', 'asset.txt'), 'utf8'));
})();

/***/ }),
Expand Down

0 comments on commit b042bd8

Please sign in to comment.