From 610191f768c505a3774fd57cec0d570e272c3467 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus <sbekkhus91@gmail.com> 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.mjs | 86 +++++++++++++++++++++-------- scripts/bundleTs.mjs | 11 +--- 3 files changed, 65 insertions(+), 36 deletions(-) diff --git a/packages/jest-jasmine2/src/index.ts b/packages/jest-jasmine2/src/index.ts index cf7c8bf5b4b1..05adc58d7e49 100644 --- a/packages/jest-jasmine2/src/index.ts +++ b/packages/jest-jasmine2/src/index.ts @@ -125,7 +125,7 @@ export default async function jasmine2( runtime .requireInternalModule<typeof import('./jestExpect')>( - path.resolve(__dirname, './jestExpect.js'), + require.resolve('./jestExpect.js'), ) .default({expand: globalConfig.expand}); @@ -146,7 +146,7 @@ export default async function jasmine2( const snapshotState: SnapshotState = await runtime .requireInternalModule<typeof import('./setup_jest_globals')>( - path.resolve(__dirname, './setup_jest_globals.js'), + require.resolve('./setup_jest_globals.js'), ) .default({ config, diff --git a/scripts/buildUtils.mjs b/scripts/buildUtils.mjs index 65cb8d7f290d..b30fd81df2c7 100644 --- a/scripts/buildUtils.mjs +++ b/scripts/buildUtils.mjs @@ -13,6 +13,7 @@ import chalk from 'chalk'; import fs from 'graceful-fs'; import {sync as readPkg} from 'read-pkg'; import stringLength from 'string-length'; +import webpack from 'webpack'; import nodeExternals from 'webpack-node-externals'; import babelConfig from '../babel.config.js'; @@ -67,7 +68,9 @@ export 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'} : {}), @@ -127,6 +130,15 @@ export function adjustToTerminalWidth(str) { export const INLINE_REQUIRE_EXCLUDE_LIST = /packages\/expect|(jest-(circus|diff|get-type|jasmine2|matcher-utils|message-util|regex-util|snapshot))|pretty-format\//; +export 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(); + export function createWebpackConfigs() { const packages = getPackages(); @@ -159,7 +171,7 @@ export function createWebpackConfigs() { }); } - const workerEntriesEntries = + const separateChunks = pkg.name === 'jest-worker' ? { processChild: path.resolve( @@ -177,6 +189,27 @@ export 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 = @@ -206,10 +239,11 @@ export function createWebpackConfigs() { packageDir, pkg, webpackConfig: { + context: packageDir, devtool: false, entry: { index: input, - ...workerEntriesEntries, + ...separateChunks, ...extraEntryPoints, }, externals: nodeExternals(), @@ -225,6 +259,9 @@ export function createWebpackConfigs() { }, ], }, + optimization: { + moduleIds: 'named', + }, output: { filename: '[name].js', library: { @@ -232,7 +269,10 @@ export function createWebpackConfigs() { }, path: path.resolve(packageDir, 'build'), }, - plugins: [new IgnoreDynamicRequire(workerEntriesEntries)], + plugins: [ + new webpack.BannerPlugin(copyrightSnippet), + new IgnoreDynamicRequire(separateChunks), + ], resolve: { extensions: ['.ts', '.js'], }, @@ -256,30 +296,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.mjs b/scripts/bundleTs.mjs index 4961d41ad2c6..55c433b54a9d 100644 --- a/scripts/bundleTs.mjs +++ b/scripts/bundleTs.mjs @@ -18,7 +18,7 @@ import fs from 'graceful-fs'; import {sync as pkgDir} from 'pkg-dir'; import prettier from 'prettier'; import rimraf from 'rimraf'; -import {getPackages} from './buildUtils.mjs'; +import {copyrightSnippet, getPackages} from './buildUtils.mjs'; const prettierConfig = prettier.resolveConfig.sync( fileURLToPath(import.meta.url).replace(/\.js$/, '.d.ts'), @@ -27,15 +27,6 @@ const prettierConfig = prettier.resolveConfig.sync( const require = createRequire(import.meta.url); 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(); - const typesNodeReferenceDirective = '/// <reference types="node" />'; const excludedPackages = new Set(['@jest/globals']);