Skip to content

Commit

Permalink
Ensure separate asset compilation states in subbuilds (#630)
Browse files Browse the repository at this point in the history
Co-authored-by: Joe Haddad <timer150@gmail.com>
  • Loading branch information
guybedford and Timer authored Dec 18, 2020
1 parent feb89ce commit 9898d24
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@sentry/node": "^4.3.0",
"@slack/web-api": "^5.13.0",
"@tensorflow/tfjs-node": "^0.3.0",
"@vercel/webpack-asset-relocator-loader": "0.9.1",
"@vercel/webpack-asset-relocator-loader": "1.0.0",
"analytics-node": "^3.3.0",
"apollo-server-express": "^2.2.2",
"arg": "^4.1.0",
Expand Down
5 changes: 3 additions & 2 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ async function main() {
{
filename: "ts-loader.js",
minify,
v8cache: true
}
v8cache: true,
noAssetBuilds: true
},
);
checkUnknownAssets('ts-loader', Object.keys(tsLoaderAssets).filter(asset => !asset.startsWith('lib/') && !asset.startsWith('typescript/lib')));

Expand Down
96 changes: 61 additions & 35 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,15 @@ function ncc (

let watcher, watchHandler, rebuildHandler;

const compilationStack = [];

var plugins = [
{
apply(compiler) {
compiler.hooks.compilation.tap("relocate-loader", compilation => relocateLoader.initAssetCache(compilation));
compiler.hooks.compilation.tap("relocate-loader", compilation => {
compilationStack.push(compilation);
relocateLoader.initAssetCache(compilation);
});
compiler.hooks.watchRun.tap("ncc", () => {
if (rebuildHandler)
rebuildHandler();
Expand Down Expand Up @@ -289,7 +294,10 @@ function ncc (
});
});
})
.then(finalizeHandler);
.then(finalizeHandler, function (err) {
compilationStack.pop();
throw err;
});
}
else {
if (typeof watch === 'object') {
Expand All @@ -300,10 +308,14 @@ function ncc (
}
let cachedResult;
watcher = compiler.watch({}, async (err, stats) => {
if (err)
if (err) {
compilationStack.pop();
return watchHandler({ err });
if (stats.hasErrors())
}
if (stats.hasErrors()) {
compilationStack.pop();
return watchHandler({ err: stats.toString() });
}
const returnValue = await finalizeHandler(stats);
if (watchHandler)
watchHandler(returnValue);
Expand Down Expand Up @@ -434,45 +446,59 @@ function ncc (

// for each .js / .mjs / .cjs file in the asset list, build that file with ncc itself
if (!noAssetBuilds) {
let assetNames = Object.keys(assets);
assetNames.push(`${filename}${ext === '.cjs' ? '.js' : ''}`);
for (const asset of assetNames) {
const compilation = compilationStack[compilationStack.length - 1];
let existingAssetNames = Object.keys(assets);
existingAssetNames.push(`${filename}${ext === '.cjs' ? '.js' : ''}`);
const subbuildAssets = [];
for (const asset of Object.keys(assets)) {
if (!asset.endsWith('.js') && !asset.endsWith('.cjs') && !asset.endsWith('.ts') && !asset.endsWith('.mjs') ||
asset.endsWith('.cache.js') || asset.endsWith('.cache.cjs') || asset.endsWith('.cache.ts') || asset.endsWith('.cache.mjs'))
asset.endsWith('.cache.js') || asset.endsWith('.cache.cjs') || asset.endsWith('.cache.ts') || asset.endsWith('.cache.mjs') || asset.endsWith('.d.ts')) {
existingAssetNames.push(asset);
continue;
const assetMeta = relocateLoader.getAssetMeta(asset);
if (!assetMeta)
}
const assetMeta = relocateLoader.getAssetMeta(asset, compilation);
if (!assetMeta || !assetMeta.path) {
existingAssetNames.push(asset);
continue;
}
subbuildAssets.push(asset);
}
for (const asset of subbuildAssets) {
const assetMeta = relocateLoader.getAssetMeta(asset, compilation);
const path = assetMeta.path;
if (path) {
const { code, assets: subbuildAssets, symlinks: subbuildSymlinks, stats: subbuildStats } = await ncc(path, {
cache,
externals,
filename: asset,
minify,
sourceMap,
sourceMapRegister,
sourceMapBasePrefix,
// dont recursively asset build
// could be supported with seen tracking
noAssetBuilds: true,
v8cache,
filterAssetBase,
existingAssetNames: assetNames,
quiet,
debugLog,
transpileOnly,
license,
target
});
Object.assign(symlinks, subbuildSymlinks);
Object.assign(stats, subbuildStats);
assets[asset] = { source: code, permissions: assetMeta.permissions };
Object.assign(assets, subbuildAssets);
const { code, assets: subbuildAssets, symlinks: subbuildSymlinks, stats: subbuildStats } = await ncc(path, {
cache,
externals,
filename: asset,
minify,
sourceMap,
sourceMapRegister,
sourceMapBasePrefix,
// dont recursively asset build
// could be supported with seen tracking
noAssetBuilds: true,
v8cache,
filterAssetBase,
existingAssetNames,
quiet,
debugLog,
transpileOnly,
license,
target
});
Object.assign(symlinks, subbuildSymlinks);
Object.assign(stats, subbuildStats);
for (const subasset of Object.keys(subbuildAssets)) {
assets[subasset] = subbuildAssets[subasset];
if (!existingAssetNames.includes(subasset))
existingAssetNames.push(subasset);
}
assets[asset] = { source: code, permissions: assetMeta.permissions };
}
}

compilationStack.pop();

return { code, map: map ? JSON.stringify(map) : undefined, assets, symlinks, stats };
}
}
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1902,10 +1902,10 @@
dependencies:
"@types/yargs-parser" "*"

"@vercel/webpack-asset-relocator-loader@0.9.1":
version "0.9.1"
resolved "https://registry.yarnpkg.com/@vercel/webpack-asset-relocator-loader/-/webpack-asset-relocator-loader-0.9.1.tgz#b9cd56e7b7de002830410aca708b76eea930a2cd"
integrity sha512-TJXZZa94a3mDB0jdHCDFaWzrFGwsNqpYNqqKeUieznpToN//9XDjQgwkgpvAhG04wwGBFIPvBBfZZdBsvGhLqQ==
"@vercel/webpack-asset-relocator-loader@1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@vercel/webpack-asset-relocator-loader/-/webpack-asset-relocator-loader-1.0.0.tgz#cc3cae3b3cc8f3f631552e1ca97e566f597d5e5b"
integrity sha512-JZGVEFBOR0I7ccwewsWSSOB/ke6wYV4e09qQHtTBLTe/zgWrsjes7SX1Xt9M1UgiMTkhZ/0jyJpdxEqrpaYRMA==

"@webassemblyjs/ast@1.9.0":
version "1.9.0"
Expand Down

0 comments on commit 9898d24

Please sign in to comment.