From ebe1a124a111e4af49a0f5f2abc1a54f93919708 Mon Sep 17 00:00:00 2001 From: neverland Date: Tue, 24 Sep 2024 04:18:07 +0200 Subject: [PATCH] perf: avoid requiring empty module (#3538) --- packages/core/prebundle.config.mjs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/packages/core/prebundle.config.mjs b/packages/core/prebundle.config.mjs index 1a2df84408..2f705f7496 100644 --- a/packages/core/prebundle.config.mjs +++ b/packages/core/prebundle.config.mjs @@ -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); @@ -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, @@ -152,6 +153,9 @@ export default { { name: 'postcss', ignoreDts: true, + externals: { + picocolors: '../picocolors', + }, }, { name: 'css-loader', @@ -159,9 +163,8 @@ export default { externals: { semver: './semver', postcss: '../postcss', - picocolors: '../picocolors', }, - afterBundle: writeEmptySemver, + beforeBundle: skipSemver, }, { name: 'postcss-loader', @@ -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',