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

Ensure separate asset compilation states in subbuilds #630

Merged
merged 6 commits into from
Dec 18, 2020
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
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/[email protected]":
version "1.9.0"
Expand Down