Skip to content

Commit

Permalink
Separate asset relocator (#264)
Browse files Browse the repository at this point in the history
* separate asset relocator

* update to 0.1.4
  • Loading branch information
guybedford authored and rauchg committed Feb 7, 2019
1 parent 82336fa commit 8abb4ba
Show file tree
Hide file tree
Showing 95 changed files with 68 additions and 4,161 deletions.
3 changes: 2 additions & 1 deletion dist/ncc/loaders/readme.md
Expand Up @@ -2,8 +2,9 @@

This directory will contain:

- `node-loader.js` the ncc loader for supporting Node binary requires
- `relocate-loader.js` the ncc loader for handling CommonJS asset and reference relocations
- `shebang-loader.js` the ncc loader to ensure proper hash bang support in Node.js CLI files
- `ts-loader.js` the ncc loader for handling TypeScript

These are generated by the `build` step defined in `../../../package.json`.

Expand Down
1 change: 1 addition & 0 deletions dist/ncc/readme.md
Expand Up @@ -4,6 +4,7 @@ This directory will contain:

- `index.js` the main ncc bundle
- `cli.js` the CLI bundle, excluding the main ncc bundle
- `typescript.js` the TypeScript detection file

These are generated by the `build` step defined in `../../package.json`.

Expand Down
10 changes: 1 addition & 9 deletions package.json
Expand Up @@ -21,23 +21,21 @@
"@google-cloud/bigquery": "^2.0.1",
"@google-cloud/firestore": "^0.19.0",
"@sentry/node": "^4.3.0",
"acorn": "^6.0.4",
"@zeit/webpack-asset-relocator-loader": "^0.1.4",
"analytics-node": "^3.3.0",
"apollo-server-express": "^2.2.2",
"arg": "^2.0.0",
"auth0": "^2.14.0",
"aws-sdk": "^2.356.0",
"axios": "^0.18.0",
"azure-storage": "^2.10.2",
"bindings": "^1.3.1",
"bytes": "^3.0.0",
"canvas": "^2.2.0",
"chromeless": "^1.5.2",
"codecov": "^3.1.0",
"copy": "^0.3.2",
"core-js": "^2.5.7",
"cowsay": "^1.3.1",
"estree-walker": "^0.5.2",
"express": "^4.16.4",
"fetch-h2": "^1.0.2",
"firebase": "^5.5.8",
Expand All @@ -58,18 +56,14 @@
"jugglingdb": "^2.0.0-rc8",
"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",
"memcached": "^2.2.2",
"mkdirp": "^0.5.1",
"mongoose": "^5.3.12",
"mysql": "^2.16.0",
"node-gyp": "^3.8.0",
"node-native-loader": "^1.1.1",
"node-pre-gyp": "^0.12.0",
"passport": "^0.4.0",
"passport-google-oauth": "^1.0.0",
"path-platform": "^0.11.15",
Expand All @@ -79,8 +73,6 @@
"react-dom": "^16.6.3",
"redis": "^2.8.0",
"request": "^2.88.0",
"resolve": "^1.8.1",
"rollup-pluginutils": "^2.3.3",
"rxjs": "^6.3.3",
"saslprep": "^1.0.2",
"sharp": "^0.21.1",
Expand Down
13 changes: 0 additions & 13 deletions scripts/build.js
Expand Up @@ -35,15 +35,6 @@ async function main() {
}
);

const { code: nodeLoader, assets: nodeLoaderAssets } = await ncc(
__dirname + "/../src/loaders/node-loader",
{
filename: "node-loader.js",
minify: true,
v8cache: true
}
);

const { code: relocateLoader, assets: relocateLoaderAssets } = await ncc(
__dirname + "/../src/loaders/relocate-loader",
{ filename: "relocate-loader.js", minify: true, v8cache: true }
Expand Down Expand Up @@ -72,7 +63,6 @@ async function main() {
const unknownAssets = [
...Object.keys(cliAssets),
...Object.keys(indexAssets).filter(asset => !asset.startsWith('locales/')),
...Object.keys(nodeLoaderAssets),
...Object.keys(relocateLoaderAssets),
...Object.keys(shebangLoaderAssets),
...Object.keys(tsLoaderAssets).filter(asset => !asset.startsWith('lib/')),
Expand All @@ -87,15 +77,13 @@ async function main() {
writeFileSync(__dirname + "/../dist/ncc/cli.js.cache", cliAssets["cli.js.cache"]);
writeFileSync(__dirname + "/../dist/ncc/index.js.cache", indexAssets["index.js.cache"]);
writeFileSync(__dirname + "/../dist/ncc/sourcemap-register.js.cache", sourcemapAssets["sourcemap-register.js.cache"]);
writeFileSync(__dirname + "/../dist/ncc/loaders/node-loader.js.cache", nodeLoaderAssets["node-loader.js.cache"]);
writeFileSync(__dirname + "/../dist/ncc/loaders/relocate-loader.js.cache", relocateLoaderAssets["relocate-loader.js.cache"]);
writeFileSync(__dirname + "/../dist/ncc/loaders/shebang-loader.js.cache", shebangLoaderAssets["shebang-loader.js.cache"]);
writeFileSync(__dirname + "/../dist/ncc/loaders/ts-loader.js.cache", tsLoaderAssets["ts-loader.js.cache"]);

writeFileSync(__dirname + "/../dist/ncc/cli.js.cache.js", cliAssets["cli.js.cache.js"]);
writeFileSync(__dirname + "/../dist/ncc/index.js.cache.js", indexAssets["index.js.cache.js"]);
writeFileSync(__dirname + "/../dist/ncc/sourcemap-register.js.cache.js", sourcemapAssets["sourcemap-register.js.cache.js"]);
writeFileSync(__dirname + "/../dist/ncc/loaders/node-loader.js.cache.js", nodeLoaderAssets["node-loader.js.cache.js"]);
writeFileSync(__dirname + "/../dist/ncc/loaders/relocate-loader.js.cache.js", relocateLoaderAssets["relocate-loader.js.cache.js"]);
writeFileSync(__dirname + "/../dist/ncc/loaders/shebang-loader.js.cache.js", shebangLoaderAssets["shebang-loader.js.cache.js"]);
writeFileSync(__dirname + "/../dist/ncc/loaders/ts-loader.js.cache.js", tsLoaderAssets["ts-loader.js.cache.js"]);
Expand All @@ -118,7 +106,6 @@ catch (e) {
module.exports = typescript;
`);
writeFileSync(__dirname + "/../dist/ncc/sourcemap-register.js", sourcemapSupport);
writeFileSync(__dirname + "/../dist/ncc/loaders/node-loader.js", nodeLoader);
writeFileSync(__dirname + "/../dist/ncc/loaders/relocate-loader.js", relocateLoader);
writeFileSync(__dirname + "/../dist/ncc/loaders/shebang-loader.js", shebangLoader);
writeFileSync(__dirname + "/../dist/ncc/loaders/ts-loader.js", tsLoader);
Expand Down
68 changes: 20 additions & 48 deletions src/index.js
@@ -1,7 +1,7 @@
const resolve = require("resolve");
const fs = require("graceful-fs");
const crypto = require("crypto");
const { sep, dirname } = require("path");
const { sep } = require("path");
const webpack = require("webpack");
const MemoryFS = require("memory-fs");
const terser = require("terser");
Expand All @@ -26,7 +26,6 @@ const hashOf = name => {
.slice(0, 10);
}

const nodeLoader = eval('require(__dirname + "/loaders/node-loader.js")');
const relocateLoader = eval('require(__dirname + "/loaders/relocate-loader.js")');

module.exports = (
Expand All @@ -45,26 +44,18 @@ module.exports = (
process.env.TYPESCRIPT_LOOKUP_PATH = resolvedEntry;
const shebangMatch = fs.readFileSync(resolvedEntry).toString().match(shebangRegEx);
const mfs = new MemoryFS();
const assetNames = Object.create(null);
assetNames[filename] = true;
if (sourceMap)
assetNames[filename + '.map'] = true;
if (v8cache)
assetNames[filename + '.cache'] = assetNames[filename + '.cache.js'] = true;
const resolvePlugins = [];
let tsconfigMatchPath;
const assetState = {
assets: Object.create(null),
assetNames,
assetPermissions: undefined
};
assetState.assetNames[filename] = true;

const existingAssetNames = [filename];
if (sourceMap) {
assetState.assetNames[filename + '.map'] = true;
assetState.assetNames['sourcemap-register.js'] = true;
existingAssetNames.push(filename + '.map');
existingAssetNames.push('sourcemap-register.js');
}
if (v8cache) {
existingAssetNames.push(filename + '.cache');
existingAssetNames.push(filename + '.cache.js');
}
nodeLoader.setAssetState(assetState);
relocateLoader.setAssetState(assetState);
const resolvePlugins = [];
let tsconfigMatchPath;
// add TsconfigPathsPlugin to support `paths` resolution in tsconfig
// we need to catch here because the plugin will
// error if there's no tsconfig in the working directory
Expand Down Expand Up @@ -137,18 +128,13 @@ module.exports = (
module: {
rules: [
{
test: /\.node$/,
use: [{
loader: eval('__dirname + "/loaders/node-loader.js"')
}]
},
{
test: /\.(js|mjs|tsx?)$/,
test: /\.(js|mjs|tsx?|node)$/,
use: [{
loader: eval('__dirname + "/loaders/relocate-loader.js"'),
options: {
cwd: dirname(resolvedEntry),
entryId: resolvedEntry
existingAssetNames,
escapeNonAnalyzableRequires: true,
wrapperCompatibility: true
}
}]
},
Expand Down Expand Up @@ -180,24 +166,10 @@ module.exports = (
plugins: [
{
apply(compiler) {
/* compiler.hooks.afterCompile.tap("ncc", compilation => {
compilation.cache.store('/NccPlugin/' + resolvedEntry, null, JSON.stringify(assetState.assetPermissions.permissions), (err) => {
if (err) console.error(err);
});
}); */
compiler.hooks.watchRun.tap("ncc", () => {
if (rebuildHandler)
rebuildHandler();
});
// override "not found" context to try built require first
compiler.hooks.compilation.tap("ncc", compilation => {
assetState.assetPermissions = Object.create(null);
/* compilation.cache.get('/NccPlugin/' + resolvedEntry, null, (err, _assetPermissions) => {
if (err) console.error(err);
assetState.assetPermissions = JSON.parse(_assetPermissions || 'null') || Object.create(null);
}); */
});

compiler.hooks.normalModuleFactory.tap("ncc", NormalModuleFactory => {
function handler(parser) {
parser.hooks.assign.for("require").intercept({
Expand Down Expand Up @@ -283,7 +255,7 @@ module.exports = (

function finalizeHandler () {
const assets = Object.create(null);
getFlatFiles(mfs.data, assets, assetState.assetPermissions);
getFlatFiles(mfs.data, assets, relocateLoader.getAssetPermissions);
delete assets[filename];
delete assets[filename + ".map"];
let code = mfs.readFileSync(`/${filename}`, "utf8");
Expand Down Expand Up @@ -356,18 +328,18 @@ module.exports = (
};

// this could be rewritten with actual FS apis / globs, but this is simpler
function getFlatFiles(mfsData, output, assetPermissions, curBase = "") {
function getFlatFiles(mfsData, output, getAssetPermissions, curBase = "") {
for (const path of Object.keys(mfsData)) {
const item = mfsData[path];
const curPath = curBase + "/" + path;
// directory
if (item[""] === true) getFlatFiles(item, output, assetPermissions, curPath);
if (item[""] === true) getFlatFiles(item, output, getAssetPermissions, curPath);
// file
else if (!curPath.endsWith("/")) {
output[curPath.substr(1)] = {
source: mfsData[path],
permissions: assetPermissions[curPath.substr(1)]
}
permissions: getAssetPermissions(curPath.substr(1))
};
}
}
}
31 changes: 0 additions & 31 deletions src/loaders/node-loader.js

This file was deleted.

0 comments on commit 8abb4ba

Please sign in to comment.