Skip to content

Commit 11746b6

Browse files
committed
fix(builder): failed to disable html via htmlPlugin: false
1 parent 0a86b31 commit 11746b6

File tree

6 files changed

+51
-26
lines changed

6 files changed

+51
-26
lines changed

.changeset/sour-eels-nail.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@modern-js/builder-rspack-provider': patch
3+
'@modern-js/builder-shared': patch
4+
'@modern-js/app-tools': patch
5+
---
6+
7+
fix(builder): failed to disable html via htmlPlugin: false
8+
9+
fix(builder): 修复通过 htmlPlugin: false 无法禁用 html 的问题

packages/builder/builder-rspack-provider/tests/plugins/html.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,24 @@ describe('plugins/html', () => {
205205

206206
expect(matchPlugin(bundlerConfigs[0], 'HtmlRspackPlugin')).toBeNull();
207207
});
208+
209+
it('should disable html plugin when htmlPlugin is an array and contains false', async () => {
210+
const builder = await createBuilder({
211+
plugins: [builderPluginEntry(), builderPluginHtml()],
212+
entry: {
213+
main: './src/main.ts',
214+
},
215+
builderConfig: {
216+
tools: {
217+
htmlPlugin: [{}, false],
218+
},
219+
},
220+
});
221+
222+
const {
223+
origin: { bundlerConfigs },
224+
} = await builder.inspectConfig();
225+
226+
expect(matchPlugin(bundlerConfigs[0], 'HtmlRspackPlugin')).toBeNull();
227+
});
208228
});

packages/builder/builder-shared/src/apply/html.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@ export function getTemplatePath(
1616
export const isHtmlDisabled = (
1717
config: SharedNormalizedConfig,
1818
target: BuilderTarget,
19-
) =>
20-
(config.tools as { htmlPlugin: boolean }).htmlPlugin === false ||
21-
target === 'node' ||
22-
target === 'web-worker' ||
23-
target === 'service-worker';
19+
) => {
20+
const { htmlPlugin } = config.tools as {
21+
htmlPlugin: boolean | Array<unknown>;
22+
};
23+
return (
24+
htmlPlugin === false ||
25+
(Array.isArray(htmlPlugin) && htmlPlugin.includes(false)) ||
26+
target === 'node' ||
27+
target === 'web-worker' ||
28+
target === 'service-worker'
29+
);
30+
};

packages/solutions/app-tools/src/builder/shared/builderPlugins/adapterHtml.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
2+
isHtmlDisabled,
23
BuilderPlugin,
3-
BuilderTarget,
44
BundlerChain,
55
createVirtualModule,
66
} from '@modern-js/builder-shared';
@@ -12,23 +12,7 @@ import {
1212
import { template as lodashTemplate } from '@modern-js/utils/lodash';
1313
import { Bundler } from '../../../types';
1414
import { BottomTemplatePlugin } from '../bundlerPlugins';
15-
import type {
16-
BuilderNormalizedConfig,
17-
BuilderOptions,
18-
BuilderPluginAPI,
19-
} from '../types';
20-
21-
export function isHtmlEnabled(
22-
config: BuilderNormalizedConfig,
23-
target: BuilderTarget,
24-
) {
25-
return (
26-
config.tools?.htmlPlugin !== false &&
27-
target !== 'node' &&
28-
target !== 'service-worker' &&
29-
target !== 'web-worker'
30-
);
31-
}
15+
import type { BuilderOptions, BuilderPluginAPI } from '../types';
3216

3317
export const builderPluginAdapterHtml = <B extends Bundler>(
3418
options: BuilderOptions<B>,
@@ -39,7 +23,7 @@ export const builderPluginAdapterHtml = <B extends Bundler>(
3923
async (chain, { CHAIN_ID, target, HtmlPlugin: HtmlBundlerPlugin }) => {
4024
const builderConfig = api.getNormalizedConfig();
4125

42-
if (isHtmlEnabled(builderConfig, target)) {
26+
if (!isHtmlDisabled(builderConfig, target)) {
4327
applyBottomHtmlPlugin({
4428
api,
4529
options,

packages/solutions/app-tools/src/builder/shared/builderPlugins/adapterSSR.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as path from 'path';
22
import {
3+
isHtmlDisabled,
34
BuilderPlugin,
45
BundlerChain,
56
mergeBuilderConfig,
@@ -15,7 +16,6 @@ import type {
1516
import { HtmlAsyncChunkPlugin, RouterPlugin } from '../bundlerPlugins';
1617
import type { BuilderOptions, BuilderPluginAPI } from '../types';
1718
import { getServerCombinedModueFile } from '../../../analyze/utils';
18-
import { isHtmlEnabled } from './adapterHtml';
1919

2020
export const builderPluginAdapterSSR = <B extends Bundler>(
2121
options: BuilderOptions<B>,
@@ -57,7 +57,7 @@ export const builderPluginAdapterSSR = <B extends Bundler>(
5757
});
5858
}
5959

60-
if (isHtmlEnabled(builderConfig, target)) {
60+
if (!isHtmlDisabled(builderConfig, target)) {
6161
applyAsyncChunkHtmlPlugin({
6262
chain,
6363
modernConfig: options.normalizedConfig,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
chrome >= 51
2+
edge >= 15
3+
firefox >= 54
4+
safari >= 10
5+
ios_saf >= 10

0 commit comments

Comments
 (0)