Skip to content

Commit 4274510

Browse files
authored
fix(builder): failed to disable html via htmlPlugin: false (#3727)
* fix(builder): failed to disable html via htmlPlugin: false * chore: add test * chore: update implementation
1 parent 925ae2a commit 4274510

File tree

7 files changed

+103
-26
lines changed

7 files changed

+103
-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-shared/src/apply/html.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@ 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+
target === 'node' ||
26+
target === 'web-worker' ||
27+
target === 'service-worker'
28+
);
29+
};

packages/builder/builder-shared/src/mergeBuilderConfig.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ export const mergeBuilderConfig = <T>(...configs: T[]): T =>
1616
return source ?? target;
1717
}
1818

19+
// allow using `htmlPlugin: false` to disable HTML
20+
if (key === 'htmlPlugin' && source === false) {
21+
return false;
22+
}
23+
1924
if (pair.some(_.isArray)) {
2025
return [..._.castArray(target), ..._.castArray(source)];
2126
}

packages/builder/builder-shared/tests/mergeConfig.test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,72 @@ describe('mergeBuilderConfig', () => {
159159
d: { test: [2] },
160160
});
161161
});
162+
163+
test('should merge tools.htmlPlugin correctly', async () => {
164+
expect(
165+
mergeBuilderConfig(
166+
{
167+
tools: {
168+
htmlPlugin: {},
169+
},
170+
},
171+
{
172+
tools: {
173+
htmlPlugin: false,
174+
},
175+
},
176+
),
177+
).toEqual({
178+
tools: {
179+
htmlPlugin: false,
180+
},
181+
});
182+
183+
expect(
184+
mergeBuilderConfig(
185+
{
186+
tools: {
187+
htmlPlugin: false,
188+
},
189+
},
190+
{
191+
tools: {
192+
htmlPlugin: {},
193+
},
194+
},
195+
),
196+
).toEqual({
197+
tools: {
198+
htmlPlugin: {},
199+
},
200+
});
201+
});
202+
203+
const noop = () => {
204+
//
205+
};
206+
207+
expect(
208+
mergeBuilderConfig(
209+
{
210+
tools: {
211+
htmlPlugin: false,
212+
},
213+
},
214+
{
215+
tools: {
216+
htmlPlugin: {},
217+
},
218+
},
219+
{
220+
tools: {
221+
htmlPlugin: noop,
222+
},
223+
},
224+
),
225+
).toEqual({
226+
tools: {
227+
htmlPlugin: [{}, noop],
228+
},
229+
});
162230
});

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)