Skip to content

Commit 8ddcfc4

Browse files
authoredAug 17, 2021
perf: parallelize lwc build based on rollup input (#2441)
* perf: parallelize lwc build based on rollup input * fix: respond to PR comments
1 parent fc18c8c commit 8ddcfc4

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed
 

‎packages/lwc/scripts/utils/child_worker.js

+18-9
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,25 @@
44
* SPDX-License-Identifier: MIT
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
66
*/
7+
const { rollup } = require('rollup');
78
const { rollupConfig, generateTarget } = require('./rollup');
89

9-
module.exports = function (config, callback) {
10-
const targetConfig = rollupConfig(config);
10+
module.exports = async function (targets, callback) {
11+
const targetConfigs = targets.map((config) => rollupConfig(config));
1112

12-
generateTarget(targetConfig)
13-
.then(() => {
14-
callback(null, { config, pid: `${process.pid}` });
15-
})
16-
.catch((err) => {
17-
callback(err);
18-
});
13+
try {
14+
const bundle = await rollup(targetConfigs[0].inputOptions); // inputOptions are all the same
15+
await Promise.all(
16+
targetConfigs.map(async ({ outputOptions, display }) => {
17+
await generateTarget({
18+
bundle,
19+
outputOptions,
20+
display,
21+
});
22+
})
23+
);
24+
callback();
25+
} catch (err) {
26+
callback(err);
27+
}
1928
};

‎packages/lwc/scripts/utils/generate_targets.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,31 @@ const DEFAULT_FARM_OPTS = {
1313
maxConcurrentCallsPerWorker: 2,
1414
};
1515

16+
// Group the targets based on their input configuration, which allows us to run
17+
// rollup.rollup() once per unique input combination, and then bundle.generate
18+
// multiple times for each unique output combination.
19+
function groupByInputOptions(configs) {
20+
const keysToConfigs = {};
21+
for (const config of configs) {
22+
const { input, prod, target } = config;
23+
// These are the only input options that matter for rollup.rollup()
24+
const key = [input, !!prod, target].join('-');
25+
keysToConfigs[key] = keysToConfigs[key] || [];
26+
keysToConfigs[key].push(config);
27+
}
28+
return Object.values(keysToConfigs);
29+
}
30+
1631
module.exports = async function generateTargets(targets, opts = {}) {
1732
const workers = workerFarm(
1833
{ ...DEFAULT_FARM_OPTS, ...opts },
1934
require.resolve('./child_worker')
2035
);
2136

37+
const targetGroups = groupByInputOptions(targets);
38+
2239
try {
23-
await Promise.all(targets.map((config) => promisify(workers)(config)));
40+
await Promise.all(targetGroups.map((targetGroup) => promisify(workers)(targetGroup)));
2441
} finally {
2542
workerFarm.end(workers);
2643
}

‎packages/lwc/scripts/utils/rollup.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
66
*/
77
const path = require('path');
8-
const { rollup } = require('rollup');
98
const rollupReplace = require('@rollup/plugin-replace');
109
const { terser: rollupTerser } = require('rollup-plugin-terser');
1110
const babel = require('@babel/core');
@@ -61,19 +60,19 @@ function rollupConfig(config) {
6160
}),
6261
rollupFeaturesPlugin(prod),
6362
compatMode && babelCompatPlugin(),
64-
prod && !debug && rollupTerser(),
6563
],
6664
},
6765
outputOptions: {
6866
name,
6967
file: path.join(targetDirectory, target, generateTargetName(config)),
7068
format,
69+
plugins: [prod && !debug && rollupTerser()],
7170
},
7271
display: { name, dir, format, target, prod, debug },
7372
};
7473
}
7574

76-
async function generateTarget({ inputOptions, outputOptions, display }) {
75+
async function generateTarget({ bundle, outputOptions, display }) {
7776
const msg = [
7877
`module: ${path.basename(display.dir)}`.padEnd(25, ' '),
7978
`format: ${display.format}`.padEnd(12, ' '),
@@ -83,8 +82,8 @@ async function generateTarget({ inputOptions, outputOptions, display }) {
8382
`pid: ${process.pid}`.padEnd(10, ' '),
8483
].join(' | ');
8584

86-
const bundle = await rollup(inputOptions);
8785
await bundle.write(outputOptions);
86+
8887
process.stdout.write(`${msg} ✓\n`);
8988
}
9089

0 commit comments

Comments
 (0)