Skip to content

Commit

Permalink
perf: avoid requiring empty module (#3538)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Sep 24, 2024
1 parent 0c3188d commit ebe1a12
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions packages/core/prebundle.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ import fs from 'node:fs';
*/
import { join } from 'node:path';

// postcss-loader and css-loader use `semver` to compare PostCSS ast version,
// Rsbuild uses the same PostCSS version and do not need the comparison.
const writeEmptySemver = (task) => {
const schemaUtilsPath = join(task.distPath, 'semver.js');
fs.writeFileSync(schemaUtilsPath, 'module.exports.satisfies = () => true;');
};

function replaceFileContent(filePath, replaceFn) {
const content = fs.readFileSync(filePath, 'utf-8');
const newContent = replaceFn(content);
Expand All @@ -20,6 +13,14 @@ function replaceFileContent(filePath, replaceFn) {
}
}

// postcss-loader and css-loader use `semver` to compare PostCSS ast version,
// Rsbuild uses the same PostCSS version and do not need the comparison.
const skipSemver = (task) => {
replaceFileContent(join(task.depPath, 'dist/index.js'), (content) =>
content.replaceAll('require("semver")', '({ satisfies: () => true })'),
);
};

/** @type {import('prebundle').Config} */
export default {
prettier: true,
Expand Down Expand Up @@ -152,16 +153,18 @@ export default {
{
name: 'postcss',
ignoreDts: true,
externals: {
picocolors: '../picocolors',
},
},
{
name: 'css-loader',
ignoreDts: true,
externals: {
semver: './semver',
postcss: '../postcss',
picocolors: '../picocolors',
},
afterBundle: writeEmptySemver,
beforeBundle: skipSemver,
},
{
name: 'postcss-loader',
Expand All @@ -177,8 +180,8 @@ export default {
// the ralevent code will never be executed, so we can replace it with an empty object.
content.replaceAll('require("cosmiconfig")', '{}'),
);
skipSemver(task);
},
afterBundle: writeEmptySemver,
},
{
name: 'postcss-load-config',
Expand Down

0 comments on commit ebe1a12

Please sign in to comment.