From d8231b0251d0647417af197de55c4689a36db435 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 10 Feb 2022 09:58:17 +0100 Subject: [PATCH] ensure vm-secific chunks are separate --- packages/jest-jasmine2/src/index.ts | 4 +- scripts/buildUtils.js | 88 +++++++++++++++++++++-------- scripts/bundleTs.js | 11 +--- 3 files changed, 67 insertions(+), 36 deletions(-) diff --git a/packages/jest-jasmine2/src/index.ts b/packages/jest-jasmine2/src/index.ts index 884cadf399b1..52214a319387 100644 --- a/packages/jest-jasmine2/src/index.ts +++ b/packages/jest-jasmine2/src/index.ts @@ -119,7 +119,7 @@ export default async function jasmine2( runtime .requireInternalModule( - path.resolve(__dirname, './jestExpect.js'), + require.resolve('./jestExpect.js'), ) .default({expand: globalConfig.expand}); @@ -140,7 +140,7 @@ export default async function jasmine2( const snapshotState: SnapshotState = await runtime .requireInternalModule( - path.resolve(__dirname, './setup_jest_globals.js'), + require.resolve('./setup_jest_globals.js'), ) .default({ config, diff --git a/scripts/buildUtils.js b/scripts/buildUtils.js index 5cd66661c706..73978e2866e8 100644 --- a/scripts/buildUtils.js +++ b/scripts/buildUtils.js @@ -13,6 +13,7 @@ const path = require('path'); const chalk = require('chalk'); const {sync: readPkg} = require('read-pkg'); const stringLength = require('string-length'); +const webpack = require('webpack'); const nodeExternals = require('webpack-node-externals'); const rootPackage = require('../package.json'); @@ -62,7 +63,9 @@ function getPackages() { Object.assign(mem, {[curr.replace(/\.js$/, '')]: curr}), {}, ), - ...(pkg.name === 'jest-circus' ? {'./runner': './build/runner.js'} : {}), + ...(pkg.name === 'jest-circus' + ? {'./runner': './build/runner.js'} + : {}), ...(pkg.name === 'expect' ? {'./build/matchers': './build/matchers.js'} : {}), @@ -130,6 +133,17 @@ module.exports.PACKAGES_DIR = PACKAGES_DIR; module.exports.INLINE_REQUIRE_EXCLUDE_LIST = INLINE_REQUIRE_EXCLUDE_LIST; +const copyrightSnippet = ` +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +`.trim(); + +module.exports.copyrightSnippet = copyrightSnippet; + function createWebpackConfigs() { const babelConfig = require('../babel.config.js'); const packages = getPackages(); @@ -163,7 +177,7 @@ function createWebpackConfigs() { }); } - const workerEntriesEntries = + const separateChunks = pkg.name === 'jest-worker' ? { processChild: path.resolve( @@ -181,6 +195,27 @@ function createWebpackConfigs() { ? {CoverageWorker: path.resolve(packageDir, './src/CoverageWorker.ts')} : pkg.name === 'jest-runner' ? {testWorker: path.resolve(packageDir, './src/testWorker.ts')} + : pkg.name === 'jest-circus' + ? { + jestAdapterInit: path.resolve( + packageDir, + './src/legacy-code-todo-rewrite/jestAdapterInit.ts', + ), + } + : pkg.name === 'jest-jasmine2' + ? { + 'jasmine/jasmineLight': path.resolve( + packageDir, + './src/jasmine/jasmineLight.ts', + ), + jestExpect: path.resolve(packageDir, './src/jestExpect.ts'), + setup_jest_globals: path.resolve( + packageDir, + './src/setup_jest_globals.ts', + ), + } + : pkg.name === 'jest-repl' + ? {repl: path.resolve(packageDir, './src/cli/repl.ts')} : {}; const extraEntryPoints = @@ -210,10 +245,11 @@ function createWebpackConfigs() { packageDir, pkg, webpackConfig: { + context: packageDir, devtool: false, entry: { index: input, - ...workerEntriesEntries, + ...separateChunks, ...extraEntryPoints, }, externals: nodeExternals(), @@ -229,6 +265,9 @@ function createWebpackConfigs() { }, ], }, + optimization: { + moduleIds: 'named', + }, output: { filename: '[name].js', library: { @@ -236,7 +275,10 @@ function createWebpackConfigs() { }, path: path.resolve(packageDir, 'build'), }, - plugins: [new IgnoreDynamicRequire(workerEntriesEntries)], + plugins: [ + new webpack.BannerPlugin(copyrightSnippet), + new IgnoreDynamicRequire(separateChunks), + ], resolve: { extensions: ['.ts', '.js'], }, @@ -260,30 +302,28 @@ class IgnoreDynamicRequire { .for('javascript/auto') .tap('IgnoreDynamicRequire', parser => { // This is a SyncBailHook, so returning anything stops the parser, and nothing (undefined) allows to continue - const ignoreRequireCallExpression = expression => { - if (expression.arguments.length === 0) { - return undefined; - } - const arg = parser.evaluateExpression(expression.arguments[0]); - if (arg.isString() && !arg.string.startsWith('.')) { - return true; - } - if (!arg.isString() && !arg.isConditional()) { - return true; - } - - if (arg.isString() && this.separateFiles.has(arg.string)) { - return true; - } - return undefined; - }; - parser.hooks.call .for('require') - .tap('IgnoreDynamicRequire', ignoreRequireCallExpression); + .tap('IgnoreDynamicRequire', expression => { + if (expression.arguments.length === 0) { + return undefined; + } + const arg = parser.evaluateExpression(expression.arguments[0]); + if (arg.isString() && !arg.string.startsWith('.')) { + return true; + } + if (!arg.isString() && !arg.isConditional()) { + return true; + } + + if (arg.isString() && this.separateFiles.has(arg.string)) { + return true; + } + return undefined; + }); parser.hooks.call .for('require.resolve') - .tap('IgnoreDynamicRequire', ignoreRequireCallExpression); + .tap('IgnoreDynamicRequire', () => true); }); }); } diff --git a/scripts/bundleTs.js b/scripts/bundleTs.js index bdf20f05cc56..daa53f59673f 100644 --- a/scripts/bundleTs.js +++ b/scripts/bundleTs.js @@ -18,7 +18,7 @@ const chalk = require('chalk'); const {sync: pkgDir} = require('pkg-dir'); const prettier = require('prettier'); const rimraf = require('rimraf'); -const {getPackages} = require('./buildUtils'); +const {copyrightSnippet, getPackages} = require('./buildUtils'); const prettierConfig = prettier.resolveConfig.sync( __filename.replace(/\.js$/, '.d.ts'), @@ -26,15 +26,6 @@ const prettierConfig = prettier.resolveConfig.sync( const typescriptCompilerFolder = pkgDir(require.resolve('typescript')); -const copyrightSnippet = ` -/** - * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ -`.trim(); - (async () => { const packages = getPackages();