Skip to content

Commit 4cfe425

Browse files
authored
fix: html minify error when tools.htmlPlugin false (#6225)
1 parent 80825c4 commit 4cfe425

File tree

5 files changed

+112
-14
lines changed

5 files changed

+112
-14
lines changed

.changeset/good-lobsters-glow.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@modern-js/runtime': patch
3+
'@modern-js/uni-builder': patch
4+
---
5+
6+
fix: html minify error when tools.htmlPlugin false

packages/cli/uni-builder/src/shared/parseCommonConfig.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -294,21 +294,23 @@ export async function parseCommonConfig(
294294

295295
extraConfig.tools ??= {};
296296

297-
// compat template title and meta params
298-
extraConfig.tools.htmlPlugin = config => {
299-
if (typeof config.templateParameters === 'function') {
300-
const originFn = config.templateParameters;
301-
302-
config.templateParameters = (...args) => {
303-
const res = originFn(...args);
304-
return {
305-
title: config.title,
306-
meta: undefined,
307-
...res,
297+
if (htmlPlugin !== false) {
298+
// compat template title and meta params
299+
extraConfig.tools.htmlPlugin = config => {
300+
if (typeof config.templateParameters === 'function') {
301+
const originFn = config.templateParameters;
302+
303+
config.templateParameters = (...args) => {
304+
const res = originFn(...args);
305+
return {
306+
title: config.title,
307+
meta: undefined,
308+
...res,
309+
};
308310
};
309-
};
310-
}
311-
};
311+
}
312+
};
313+
}
312314

313315
const { dev: RsbuildDev, server } = transformToRsbuildServerOptions(
314316
dev || {},

packages/cli/uni-builder/tests/__snapshots__/minimize.test.ts.snap

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,70 @@ exports[`html minify > should not apply html minify in production when disableMi
9797
]
9898
`;
9999

100+
exports[`html minify > should not apply html plugin when htmlPlugin false 1`] = `
101+
[
102+
RsbuildCorePlugin {},
103+
MiniCssExtractPlugin {
104+
"_sortedModulesCache": WeakMap {},
105+
"options": {
106+
"chunkFilename": "static/css/async/[name].[contenthash:8].css",
107+
"experimentalUseImportModule": undefined,
108+
"filename": "static/css/[name].[contenthash:8].css",
109+
"ignoreOrder": true,
110+
"runtime": true,
111+
},
112+
"runtimeOptions": {
113+
"attributes": undefined,
114+
"insert": undefined,
115+
"linkType": "text/css",
116+
},
117+
},
118+
DefinePlugin {
119+
"definitions": {
120+
"import.meta.env.DEV": false,
121+
"import.meta.env.MODE": "\\"production\\"",
122+
"import.meta.env.PROD": true,
123+
"process.env.ASSET_PREFIX": "\\"\\"",
124+
},
125+
},
126+
ProgressPlugin {
127+
"compileTime": null,
128+
"dependenciesCount": 10000,
129+
"handler": [Function],
130+
"hasCompileErrors": false,
131+
"id": "web",
132+
"modulesCount": 5000,
133+
"name": "ProgressPlugin",
134+
"percentBy": null,
135+
"profile": false,
136+
"showActiveModules": false,
137+
"showDependencies": true,
138+
"showEntries": true,
139+
"showModules": true,
140+
},
141+
ForkTsCheckerWebpackPlugin {
142+
"options": {
143+
"issue": {
144+
"exclude": [
145+
[Function],
146+
],
147+
},
148+
"logger": {
149+
"error": [Function],
150+
"log": [Function],
151+
},
152+
"typescript": {
153+
"build": false,
154+
"configFile": "tsconfig.json",
155+
"memoryLimit": 8192,
156+
"mode": "readonly",
157+
"typescriptPath": "<WORKSPACE>/node_modules/<PNPM_INNER>/typescript/lib/typescript.js",
158+
},
159+
},
160+
},
161+
]
162+
`;
163+
100164
exports[`plugin-minimize > Terser and SWC minimizer should not coexist 1`] = `
101165
[
102166
SwcMinimizerPlugin {

packages/cli/uni-builder/tests/minimize.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,24 @@ describe('html minify', () => {
149149

150150
process.env.NODE_ENV = 'test';
151151
});
152+
153+
it('should not apply html plugin when htmlPlugin false', async () => {
154+
process.env.NODE_ENV = 'production';
155+
156+
const rsbuild = await createUniBuilder({
157+
bundlerType: 'webpack',
158+
cwd: '',
159+
config: {
160+
tools: {
161+
htmlPlugin: false,
162+
},
163+
},
164+
});
165+
166+
const config = await unwrapConfig(rsbuild);
167+
168+
expect(config.plugins).toMatchSnapshot();
169+
170+
process.env.NODE_ENV = 'test';
171+
});
152172
});

packages/runtime/plugin-runtime/src/document/cli/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,12 @@ export const documentPlugin = (): CliPlugin<AppTools> => ({
299299
};
300300
return {
301301
config: () => {
302+
const userConfig = api.useConfigContext();
303+
304+
if (userConfig.tools?.htmlPlugin === false) {
305+
return {};
306+
}
307+
302308
return {
303309
tools: {
304310
htmlPlugin: (options, entry) => {

0 commit comments

Comments
 (0)