Skip to content

Commit ae60c56

Browse files
committed
feat: run DCE even if minify is false
1 parent d3b76d1 commit ae60c56

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
lines changed

packages/vite/src/node/__tests__/plugins/modulePreloadPolyfill/__snapshots__/modulePreloadPolyfill.spec.ts.snap

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,33 @@ exports[`load > doesn't load modulepreload polyfill when format is cjs 1`] = `
77

88
exports[`load > loads modulepreload polyfill 1`] = `
99
"(function polyfill() {
10-
const relList = document.createElement("link").relList;
11-
if (relList && relList.supports && relList.supports("modulepreload")) return;
12-
for (const link of document.querySelectorAll('link[rel="modulepreload"]')) processPreload(link);
13-
new MutationObserver((mutations) => {
14-
for (const mutation of mutations) {
15-
if (mutation.type !== "childList") continue;
16-
for (const node of mutation.addedNodes) if (node.tagName === "LINK" && node.rel === "modulepreload") processPreload(node);
17-
}
18-
}).observe(document, {
19-
childList: true,
20-
subtree: true
21-
});
22-
function getFetchOpts(link) {
23-
const fetchOpts = {};
24-
if (link.integrity) fetchOpts.integrity = link.integrity;
25-
if (link.referrerPolicy) fetchOpts.referrerPolicy = link.referrerPolicy;
26-
if (link.crossOrigin === "use-credentials") fetchOpts.credentials = "include";
27-
else if (link.crossOrigin === "anonymous") fetchOpts.credentials = "omit";
28-
else fetchOpts.credentials = "same-origin";
29-
return fetchOpts;
30-
}
31-
function processPreload(link) {
32-
if (link.ep) return;
33-
link.ep = true;
34-
const fetchOpts = getFetchOpts(link);
35-
fetch(link.href, fetchOpts);
36-
}
10+
const relList = document.createElement("link").relList;
11+
if (relList && relList.supports && relList.supports("modulepreload")) return;
12+
for (const link of document.querySelectorAll("link[rel=\\"modulepreload\\"]")) processPreload(link);
13+
new MutationObserver((mutations) => {
14+
for (const mutation of mutations) {
15+
if (mutation.type !== "childList") continue;
16+
for (const node of mutation.addedNodes) if (node.tagName === "LINK" && node.rel === "modulepreload") processPreload(node);
17+
}
18+
}).observe(document, {
19+
childList: true,
20+
subtree: true
21+
});
22+
function getFetchOpts(link) {
23+
const fetchOpts = {};
24+
if (link.integrity) fetchOpts.integrity = link.integrity;
25+
if (link.referrerPolicy) fetchOpts.referrerPolicy = link.referrerPolicy;
26+
if (link.crossOrigin === "use-credentials") fetchOpts.credentials = "include";
27+
else if (link.crossOrigin === "anonymous") fetchOpts.credentials = "omit";
28+
else fetchOpts.credentials = "same-origin";
29+
return fetchOpts;
30+
}
31+
function processPreload(link) {
32+
if (link.ep) return;
33+
link.ep = true;
34+
const fetchOpts = getFetchOpts(link);
35+
fetch(link.href, fetchOpts);
36+
}
3737
})();
3838
"
3939
`;

packages/vite/src/node/__tests__/plugins/modulePreloadPolyfill/modulePreloadPolyfill.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ const buildProject = ({ format = 'es' as ModuleFormat } = {}) =>
3636
],
3737
}) as Promise<RollupOutput>
3838

39-
// TODO: enable this test after DCE is enabled
40-
describe.skip('load', () => {
39+
describe('load', () => {
4140
it('loads modulepreload polyfill', async ({ expect }) => {
4241
const { output } = await buildProject()
4342
expect(output).toHaveLength(1)

packages/vite/src/node/build.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,12 @@ async function buildEnvironment(
796796
output.format === 'iife' ||
797797
(isSsrTargetWebworkerEnvironment &&
798798
(typeof input === 'string' || Object.keys(input).length === 1)),
799-
minify: options.minify === 'oxc',
799+
minify:
800+
options.minify === 'oxc'
801+
? true
802+
: options.minify === false
803+
? 'dce-only'
804+
: false,
800805
...output,
801806
}
802807
}

0 commit comments

Comments
 (0)