From 9898d248d2a9682d30237b776bdc1efef51b2bcd Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Fri, 18 Dec 2020 11:53:01 -0800 Subject: [PATCH] Ensure separate asset compilation states in subbuilds (#630) Co-authored-by: Joe Haddad --- package.json | 2 +- scripts/build.js | 5 ++- src/index.js | 96 ++++++++++++++++++++++++++++++------------------ yarn.lock | 8 ++-- 4 files changed, 69 insertions(+), 42 deletions(-) diff --git a/package.json b/package.json index bfb4920c..d4555b51 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/scripts/build.js b/scripts/build.js index c18895ab..5ee2fae3 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -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'))); diff --git a/src/index.js b/src/index.js index a1cba136..e1847216 100644 --- a/src/index.js +++ b/src/index.js @@ -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(); @@ -289,7 +294,10 @@ function ncc ( }); }); }) - .then(finalizeHandler); + .then(finalizeHandler, function (err) { + compilationStack.pop(); + throw err; + }); } else { if (typeof watch === 'object') { @@ -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); @@ -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 }; } } diff --git a/yarn.lock b/yarn.lock index 5826266a..590209ad 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"