Skip to content

Commit

Permalink
Upgrade Webpack from 5.0.0-alpha.17 to 5.0.0-beta.28 (#579)
Browse files Browse the repository at this point in the history
* wip

* fixups

* fixes

* remove unit test skip

* update coverage fixtures

* 5.0.0-alpha.28

* test update

* 5.0.0-alpha.32

* fixes

* webpack-5.0.0-beta.1

* 5.0.0-beta.8

* disable webpack warnings

* fixup cli test

* lower webpack logging level

* source maps fixes

* fixup source maps

* webpack-5.0.0-beta.28

* update lockfile

* fixup lockfile

* update to [email protected]

* reenable treeshaking, remove chokidar external
  • Loading branch information
guybedford authored Aug 26, 2020
1 parent 8177ceb commit c7b461c
Show file tree
Hide file tree
Showing 30 changed files with 1,331 additions and 1,327 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@google-cloud/firestore": "^2.2.0",
"@sentry/node": "^4.3.0",
"@tensorflow/tfjs-node": "^0.3.0",
"@zeit/webpack-asset-relocator-loader": "0.7.2",
"@zeit/webpack-asset-relocator-loader": "0.8.0",
"analytics-node": "^3.3.0",
"apollo-server-express": "^2.2.2",
"arg": "^4.1.0",
Expand Down Expand Up @@ -106,7 +106,7 @@
"vm2": "^3.6.6",
"vue": "^2.5.17",
"vue-server-renderer": "^2.5.17",
"webpack": "5.0.0-alpha.17",
"webpack": "5.0.0-beta.28",
"when": "^3.7.8"
}
}
23 changes: 11 additions & 12 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const copy = promisify(require("copy"));
const glob = promisify(require("glob"));
const bytes = require("bytes");

const minify = true;

async function main() {
for (const file of await glob(__dirname + "/../dist/**/*.@(js|cache|ts)")) {
unlinkSync(file);
Expand All @@ -17,7 +19,7 @@ async function main() {
filename: "cli.js",
externals: ["./index.js"],
license: 'LICENSES.txt',
minify: true,
minify,
v8cache: true
}
);
Expand All @@ -26,35 +28,32 @@ async function main() {
const { code: index, assets: indexAssets } = await ncc(
__dirname + "/../src/index",
{
// we dont care about watching, so we don't want
// to bundle it. even if we did want watching and a bigger
// bundle, webpack (and therefore ncc) cannot currently bundle
// chokidar, which is quite convenient
externals: ["chokidar"],
filename: "index.js",
minify: true,
minify,
v8cache: true
}
);
checkUnknownAssets('index', Object.keys(indexAssets).filter(asset => !asset.startsWith('locales/') && asset !== 'worker.js' && asset !== 'index1.js'));
checkUnknownAssets('index', Object.keys(indexAssets).filter(asset =>
!asset.startsWith('locales/') && asset !== 'worker.js' && asset !== 'index1.js' && asset !== 'minify.js'
));

const { code: relocateLoader, assets: relocateLoaderAssets } = await ncc(
__dirname + "/../src/loaders/relocate-loader",
{ filename: "relocate-loader.js", minify: true, v8cache: true }
{ filename: "relocate-loader.js", minify, v8cache: true }
);
checkUnknownAssets('relocate-loader', Object.keys(relocateLoaderAssets));

const { code: shebangLoader, assets: shebangLoaderAssets } = await ncc(
__dirname + "/../src/loaders/shebang-loader",
{ filename: "shebang-loader.js", minify: true, v8cache: true }
{ filename: "shebang-loader.js", minify, v8cache: true }
);
checkUnknownAssets('shebang-loader', Object.keys(shebangLoaderAssets));

const { code: tsLoader, assets: tsLoaderAssets } = await ncc(
__dirname + "/../src/loaders/ts-loader",
{
filename: "ts-loader.js",
minify: true,
minify,
v8cache: true
}
);
Expand All @@ -68,7 +67,7 @@ async function main() {

// detect unexpected asset emissions from core build
function checkUnknownAssets (buildName, assets) {
assets = assets.filter(name => !name.endsWith('.cache') && !name.endsWith('.cache.js') && name !== 'LICENSES.txt');
assets = assets.filter(name => !name.endsWith('.cache') && !name.endsWith('.cache.js') && !name.endsWith('LICENSES.txt') && name !== 'processChild.js');
if (!assets.length) return;
console.error(`New assets are being emitted by the ${buildName} build`);
console.log(assets);
Expand Down
6 changes: 5 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const { writeFileSync, unlink, existsSync, symlinkSync } = require("fs");
const mkdirp = require("mkdirp");
const { version: nccVersion } = require('../package.json');

// License and TypeScript plugins have Webpack deprecation warnings
// we don't want these on when running as a CLI utility
process.noDeprecation = true;

const usage = `Usage: ncc <cmd> <opts>
Commands:
Expand Down Expand Up @@ -246,7 +250,7 @@ async function runCmd (argv, stdout, stderr) {
return;
}

outDir = outDir || resolve("dist");
outDir = outDir || resolve(eval("'dist'"));
mkdirp.sync(outDir);
// remove all existing ".js" and ".cjs" files in the out directory
await Promise.all(
Expand Down
81 changes: 35 additions & 46 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const resolve = require("resolve");
const fs = require("graceful-fs");
const crypto = require("crypto");
const { join, dirname, extname } = require("path");
const { join, dirname, extname, relative } = require("path");
const webpack = require("webpack");
const MemoryFS = require("memory-fs");
const terser = require("terser");
Expand Down Expand Up @@ -91,21 +91,23 @@ module.exports = (
apply(resolver) {
const resolve = resolver.resolve;
resolver.resolve = function (context, path, request, resolveContext, callback) {
resolve.call(resolver, context, path, request, resolveContext, function (err, result) {
if (!err) return callback(null, result);
if (!err.missing || !err.missing.length)
const self = this;
resolve.call(self, context, path, request, resolveContext, function (err, innerPath, result) {
if (result) return callback(null, innerPath, result);
if (err && !err.message.startsWith('Can\'t resolve'))
return callback(err);
// Allow .js resolutions to .tsx? from .tsx?
if (request.endsWith('.js') && context.issuer && (context.issuer.endsWith('.ts') || context.issuer.endsWith('.tsx')))
return resolve.call(resolver, context, path, request.slice(0, -3), resolveContext, function (err, result) {
if (!err) return callback(null, result);
if (!err.missing || !err.missing.length)
if (request.endsWith('.js') && context.issuer && (context.issuer.endsWith('.ts') || context.issuer.endsWith('.tsx'))) {
return resolve.call(self, context, path, request.slice(0, -3), resolveContext, function (err, innerPath, result) {
if (result) return callback(null, innerPath, result);
if (err && !err.message.startsWith('Can\'t resolve'))
return callback(err);
// make not found errors runtime errors
callback(null, __dirname + '/@@notfound.js' + '?' + (externalMap.get(request) || request));
callback(null, __dirname + '/@@notfound.js?' + (externalMap.get(request) || request), request);
});
}
// make not found errors runtime errors
callback(null, __dirname + '/@@notfound.js' + '?' + (externalMap.get(request) || request));
callback(null, __dirname + '/@@notfound.js?' + (externalMap.get(request) || request), request);
});
};
}
Expand All @@ -123,36 +125,6 @@ module.exports = (
var plugins = [
{
apply(compiler) {
// override "not found" context to try built require first
compiler.hooks.compilation.tap("ncc", compilation => {
compilation.moduleTemplates.javascript.hooks.render.tap(
"ncc",
(
moduleSourcePostModule,
module,
options,
dependencyTemplates
) => {
if (
module._contextDependencies &&
moduleSourcePostModule._value.match(
/webpackEmptyAsyncContext|webpackEmptyContext/
)
) {
// ensure __webpack_require__ is added to wrapper
module.type = 'custom';
return moduleSourcePostModule._value.replace(
"var e = new Error",
`if (typeof req === 'number' && __webpack_require__.m[req])\n` +
` return __webpack_require__(req);\n` +
`try { return require(req) }\n` +
`catch (e) { if (e.code !== 'MODULE_NOT_FOUND') throw e }\n` +
`var e = new Error`
);
}
}
);
});
compiler.hooks.compilation.tap("relocate-loader", compilation => relocateLoader.initAssetCache(compilation));
compiler.hooks.watchRun.tap("ncc", () => {
if (rebuildHandler)
Expand Down Expand Up @@ -204,11 +176,20 @@ module.exports = (
minimize: false,
moduleIds: 'deterministic',
chunkIds: 'deterministic',
mangleExports: false
mangleExports: true,
concatenateModules: true,
innerGraph: true,
sideEffects: true
},
devtool: sourceMap ? "source-map" : false,
devtool: sourceMap ? "cheap-module-source-map" : false,
mode: "production",
target: "node",
stats: {
logging: 'error'
},
infrastructureLogging: {
level: 'error'
},
output: {
path: "/",
// Webpack only emits sourcemaps for files ending in .js
Expand Down Expand Up @@ -271,7 +252,7 @@ module.exports = (
},
{
parser: { amd: false },
exclude: /\.node$/,
exclude: /\.(node|json)$/,
use: [{
loader: eval('__dirname + "/loaders/shebang-loader.js"')
}]
Expand Down Expand Up @@ -366,11 +347,19 @@ module.exports = (
map.sources = map.sources.map(source => {
// webpack:///webpack:/// happens too for some reason
while (source.startsWith('webpack:///'))
source = source.substr(11);
source = source.slice(11);
if (source.startsWith('//'))
source = source.slice(1);
if (source.startsWith('/'))
source = relative(process.cwd(), source).replace(/\\/g, '/');
if (source.startsWith('external '))
source = 'node:' + source.slice(9);
if (source.startsWith('./'))
source = source.substr(2);
source = source.slice(2);
if (source.startsWith('(webpack)'))
source = 'webpack' + source.slice(9);
if (source.startsWith('webpack/'))
return '/webpack/' + source.substr(8);
return '/webpack/' + source.slice(8);
return sourceMapBasePrefix + source;
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/loaders/empty-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = function (input, map) {
if (baseParts[baseParts.length - 2] === 'node_modules') {
const pkgName = baseParts[baseParts.length - 1];
if (pkgName in emptyModules) {
console.error(`ncc: Ignoring build of ${pkgName}, as it is not statically analyzable. Build with "--external ${pkgName}" if this package is needed.`);
console.warn(`ncc: Ignoring build of ${pkgName}, as it is not statically analyzable. Build with "--external ${pkgName}" if this package is needed.`);
return '';
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
{
args: ["run", "test/fixtures/ts-error2/ts-error.ts"],
expect (code, stdout, stderr) {
return code === 1 && stderr.toString().indexOf('ts-error.ts(3,16)') !== -1 && stderr.toString().split('\n').length < 10;
return code === 1 && stderr.toString().indexOf('ts-error.ts(3,16)') !== -1 && stderr.toString().split('\n').length < 11;
}
},
{
Expand Down
19 changes: 10 additions & 9 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
const fs = require("fs");
const { fork } = require("child_process");
const ncc = global.coverage ? require("../src/index") : require("../");
const coverage = global.coverage;
const ncc = coverage ? require("../src/index") : require("../");

for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) {
it(`should generate correct output for ${unitTest}`, async () => {
const testDir = `${__dirname}/unit/${unitTest}`;
const expected = fs
.readFileSync(`${testDir}/output${global.coverage ? '-coverage' : ''}.js`)
.readFileSync(`${testDir}/output${coverage ? '-coverage' : ''}.js`)
.toString()
.trim()
// Windows support
.replace(/\r/g, "");
let expectedSoureMap;
let expectedSourceMap;
try {
expectedSoureMap = fs
.readFileSync(`${testDir}/output${global.coverage ? '-coverage' : ''}.js.map`)
expectedSourceMap = fs
.readFileSync(`${testDir}/output${coverage ? '-coverage' : ''}.js.map`)
.toString()
.trim()
// Windows support
Expand Down Expand Up @@ -55,20 +56,20 @@ for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) {
// Windows support
.replace(/\r/g, "");
try {
expect(actualSourceMap).toBe(expectedSoureMap);
expect(actualSourceMap).toBe(expectedSourceMap);
} catch (e) {
// useful for updating fixtures
fs.writeFileSync(`${testDir}/actual.js.map`, actualSourceMap);
throw e;
}
}
}
);
)
});
}
for (const cliTest of eval(fs.readFileSync(__dirname + "/cli.js").toString())) {
it(`should execute "ncc ${(cliTest.args || []).join(" ")}"`, async () => {
const ps = fork(__dirname + (global.coverage ? "/../src/cli.js" : "/../dist/ncc/cli.js"), cliTest.args || [], {
const ps = fork(__dirname + (coverage ? "/../src/cli.js" : "/../dist/ncc/cli.js"), cliTest.args || [], {
stdio: "pipe"
});
let stderr = "", stdout = "";
Expand Down Expand Up @@ -97,7 +98,7 @@ for (const cliTest of eval(fs.readFileSync(__dirname + "/cli.js").toString())) {
jest.setTimeout(200000);

let nccRun;
if (global.coverage) {
if (coverage) {
nccRun = require(__dirname + "/../src/cli.js");
}
else {
Expand Down
1 change: 1 addition & 0 deletions test/integration/twilio.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ try {
}
}


Loading

0 comments on commit c7b461c

Please sign in to comment.