Skip to content

Commit

Permalink
bundle other worker stuff [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Apr 27, 2022
1 parent 0e44a5b commit 0737ce4
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions scripts/buildUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,25 +163,34 @@ function createWebpackConfigs() {
});
}

const extraEntries =
pkg.name === 'jest-worker'
? {
processChild: path.resolve(
packageDir,
'./src/workers/processChild.ts',
),
threadChild: path.resolve(
packageDir,
'./src/workers/threadChild.ts',
),
}
: pkg.name === 'jest-haste-map'
? {worker: path.resolve(packageDir, './src/worker.ts')}
: pkg.name === '@jest/reporters'
? {CoverageWorker: path.resolve(packageDir, './src/CoverageWorker.ts')}
: pkg.name === 'jest-runner'
? {testWorker: path.resolve(packageDir, './src/testWorker.ts')}
: {};

return {
packageDir,
pkg,
webpackConfig: {
devtool: false,
entry: {
index: input,
...(pkg.name === 'jest-worker'
? {
processChild: path.resolve(
path.dirname(input),
'./workers/processChild.ts',
),
threadChild: path.resolve(
path.dirname(input),
'./workers/threadChild.ts',
),
}
: {}),
...extraEntries,
},
externals: nodeExternals(),
mode: 'production',
Expand All @@ -203,7 +212,7 @@ function createWebpackConfigs() {
},
path: path.resolve(packageDir, 'build'),
},
plugins: [new IgnoreDynamicRequire()],
plugins: [new IgnoreDynamicRequire(extraEntries)],
resolve: {
extensions: ['.ts', '.js'],
},
Expand All @@ -215,13 +224,19 @@ function createWebpackConfigs() {

// inspired by https://framagit.org/Glandos/webpack-ignore-dynamic-require
class IgnoreDynamicRequire {
constructor(extraEntries) {
this.separateFiles = new Set(
Object.keys(extraEntries).map(entry => `./${entry}`),
);
}

apply(compiler) {
compiler.hooks.normalModuleFactory.tap('IgnoreDynamicRequire', factory => {
factory.hooks.parser
.for('javascript/auto')
.tap('IgnoreDynamicRequire', parser => {
// This is a SyncBailHook, so returning anything stops the parser, and nothing (undefined) allows to continue
function ignoreRequireCallExpression(expression) {
const ignoreRequireCallExpression = expression => {
if (expression.arguments.length === 0) {
return undefined;
}
Expand All @@ -233,15 +248,11 @@ class IgnoreDynamicRequire {
return true;
}

if (
arg.isString() &&
(arg.string === './threadChild' ||
arg.string === './processChild')
) {
if (arg.isString() && this.separateFiles.has(arg.string)) {
return true;
}
return undefined;
}
};

parser.hooks.call
.for('require')
Expand Down

0 comments on commit 0737ce4

Please sign in to comment.