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 0d5fc17 commit de5718b
Show file tree
Hide file tree
Showing 3 changed files with 67 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
88 changes: 64 additions & 24 deletions scripts/buildUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

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

const workerEntriesEntries =
const separateChunks =
pkg.name === 'jest-worker'
? {
processChild: path.resolve(
Expand All @@ -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 =
Expand Down Expand Up @@ -210,10 +245,11 @@ function createWebpackConfigs() {
packageDir,
pkg,
webpackConfig: {
context: packageDir,
devtool: false,
entry: {
index: input,
...workerEntriesEntries,
...separateChunks,
...extraEntryPoints,
},
externals: nodeExternals(),
Expand All @@ -229,14 +265,20 @@ 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 @@ -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);
});
});
}
Expand Down
11 changes: 1 addition & 10 deletions scripts/bundleTs.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,14 @@ 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'),
);

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();

Expand Down

0 comments on commit de5718b

Please sign in to comment.