Skip to content

Commit 822e358

Browse files
committed
refactor: reuse semver comparator and document why we only apply inlineConst after 1.0.0-beta.35
1 parent 22a04bc commit 822e358

File tree

3 files changed

+49
-32
lines changed

3 files changed

+49
-32
lines changed

packages/vite-plugin-svelte/__tests__/svelte-version.spec.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { describe, it, expect } from 'vitest';
22
import { gte } from '../src/utils/svelte-version.js';
33

44
describe('gte', () => {
5+
it('returns false for smaller tag', () => {
6+
expect(gte('1.2.3-next.1', '1.2.3-next.2')).toBe(false);
7+
});
58
it('returns false for smaller patch', () => {
69
expect(gte('1.2.2', '1.2.3')).toBe(false);
710
});
@@ -12,7 +15,10 @@ describe('gte', () => {
1215
expect(gte('0.3.4', '1.2.3')).toBe(false);
1316
});
1417
it('returns true for equal', () => {
15-
expect(gte('1.2.3', '1.2.3')).toBe(true);
18+
expect(gte('1.2.3-next.1', '1.2.3-next.1')).toBe(true);
19+
});
20+
it('returns false for larger tag', () => {
21+
expect(gte('1.2.3-next.2', '1.2.3-next.1')).toBe(true);
1622
});
1723
it('returns true for larger patch', () => {
1824
expect(gte('1.2.4', '1.2.3')).toBe(true);

packages/vite-plugin-svelte/src/plugins/configure.js

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from '../utils/options.js';
1313
import { buildIdFilter, buildIdParser } from '../utils/id.js';
1414
import { createCompileSvelte } from '../utils/compile.js';
15+
import { gte } from '../utils/svelte-version.js';
1516

1617
// @ts-ignore rolldownVersion
1718
const { version: viteVersion, rolldownVersion } = vite;
@@ -59,35 +60,33 @@ export function configure(api, inlineOptions) {
5960
// extra vite config
6061
const extraViteConfig = await buildExtraViteConfig(preOptions, config);
6162

62-
if (rolldownVersion && configEnv.command === 'build') {
63-
const [major, minor, patch, tag] = rolldownVersion
64-
.replace(/[^\d.]/g, '')
65-
.split('.')
66-
.map(Number);
67-
if (major > 1 || (major === 1 && (minor > 0 || patch > 0 || tag > 34))) {
68-
extraViteConfig.build ??= {};
69-
// rename rollupOptions to rolldownOptions
70-
//@ts-ignore rolldownOptions only exists in rolldown-vite
71-
extraViteConfig.build.rolldownOptions = extraViteConfig.build.rollupOptions || {};
72-
delete extraViteConfig.build.rollupOptions;
73-
// read user config inlineConst value
74-
const inlineConst =
75-
//@ts-ignore optimization only exists in rolldown-vite
76-
config.build?.rolldownOptions?.optimization?.inlineConst ??
77-
//@ts-ignore optimization only exists in rolldown-vite
78-
config.build?.rollupOptions?.optimization?.inlineConst;
63+
if (
64+
rolldownVersion &&
65+
configEnv.command === 'build' &&
66+
gte(rolldownVersion, '1.0.0-beta.35') // inlineConst received a critical bugfix in 1.0.0-beta.35
67+
) {
68+
extraViteConfig.build ??= {};
69+
// rename rollupOptions to rolldownOptions
70+
//@ts-ignore rolldownOptions only exists in rolldown-vite
71+
extraViteConfig.build.rolldownOptions = extraViteConfig.build.rollupOptions || {};
72+
delete extraViteConfig.build.rollupOptions;
73+
// read user config inlineConst value
74+
const inlineConst =
75+
//@ts-ignore optimization only exists in rolldown-vite
76+
config.build?.rolldownOptions?.optimization?.inlineConst ??
77+
//@ts-ignore optimization only exists in rolldown-vite
78+
config.build?.rollupOptions?.optimization?.inlineConst;
7979

80-
if (inlineConst == null) {
81-
// set inlineConst build optimization for esm-env
82-
//@ts-ignore rolldownOptions only exists in rolldown-vite
83-
extraViteConfig.build.rolldownOptions.optimization ??= {};
84-
//@ts-ignore rolldownOptions only exists in rolldown-vite
85-
extraViteConfig.build.rolldownOptions.optimization.inlineConst = true;
86-
} else if (inlineConst === false) {
87-
log.warn(
88-
'Your rolldown config contains `optimization.inlineConst: false`. This can lead to increased bundle size and leaked server code in client build.'
89-
);
90-
}
80+
if (inlineConst == null) {
81+
// set inlineConst build optimization for esm-env
82+
//@ts-ignore rolldownOptions only exists in rolldown-vite
83+
extraViteConfig.build.rolldownOptions.optimization ??= {};
84+
//@ts-ignore rolldownOptions only exists in rolldown-vite
85+
extraViteConfig.build.rolldownOptions.optimization.inlineConst = true;
86+
} else if (inlineConst === false) {
87+
log.warn(
88+
'Your rolldown config contains `optimization.inlineConst: false`. This can lead to increased bundle size and leaked server code in client build.'
89+
);
9190
}
9291
}
9392

packages/vite-plugin-svelte/src/utils/svelte-version.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,27 @@ import { VERSION } from 'svelte/compiler';
66
export const isSvelteWithAsync = gte(VERSION, '5.36.0');
77

88
/**
9-
* compare semver versions, does not include comparing tags (-next.xy is ignored)
9+
* split semver string and convert to number, ignores non digits in tag
10+
* @param {string} semver
11+
* @return {number[]} [major,minor,patch,tag]
12+
*/
13+
function splitToNumbers(semver) {
14+
return semver
15+
.replace(/[^\d.-]/g, '')
16+
.split(/[.-]+/, 4)
17+
.map(Number);
18+
}
19+
20+
/**
21+
* compare semver versions, tags are compared by their numeric part only
1022
*
1123
* @param {string} a semver version
1224
* @param {string} b semver version
1325
* @return {boolean} true if a is greater or equal to b
1426
*/
1527
export function gte(a, b) {
16-
const aNum = a.split(/[.-]/, 3).map(Number);
17-
const bNum = b.split(/[.-]/, 3).map(Number);
28+
const aNum = splitToNumbers(a);
29+
const bNum = splitToNumbers(b);
1830
for (let i = 0; i < aNum.length; i++) {
1931
if (aNum[i] < bNum[i]) {
2032
return false;

0 commit comments

Comments
 (0)