Skip to content

Commit

Permalink
ensure vm-secific chunks are separate
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Apr 27, 2022
1 parent 0666f8d commit 610191f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 36 deletions.
4 changes: 2 additions & 2 deletions packages/jest-jasmine2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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});

Expand All @@ -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,
Expand Down
86 changes: 62 additions & 24 deletions scripts/buildUtils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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'}
: {}),
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -159,7 +171,7 @@ export function createWebpackConfigs() {
});
}

const workerEntriesEntries =
const separateChunks =
pkg.name === 'jest-worker'
? {
processChild: path.resolve(
Expand All @@ -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 =
Expand Down Expand Up @@ -206,10 +239,11 @@ export function createWebpackConfigs() {
packageDir,
pkg,
webpackConfig: {
context: packageDir,
devtool: false,
entry: {
index: input,
...workerEntriesEntries,
...separateChunks,
...extraEntryPoints,
},
externals: nodeExternals(),
Expand All @@ -225,14 +259,20 @@ export function createWebpackConfigs() {
},
],
},
optimization: {
moduleIds: 'named',
},
output: {
filename: '[name].js',
library: {
type: 'commonjs2',
},
path: path.resolve(packageDir, 'build'),
},
plugins: [new IgnoreDynamicRequire(workerEntriesEntries)],
plugins: [
new webpack.BannerPlugin(copyrightSnippet),
new IgnoreDynamicRequire(separateChunks),
],
resolve: {
extensions: ['.ts', '.js'],
},
Expand All @@ -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);
});
});
}
Expand Down
11 changes: 1 addition & 10 deletions scripts/bundleTs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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']);
Expand Down

0 comments on commit 610191f

Please sign in to comment.