Skip to content

Commit 7081cb3

Browse files
committed
fix(core): rewrite rolldown native packages
1 parent d7d86b2 commit 7081cb3

3 files changed

Lines changed: 19 additions & 13 deletions

File tree

packages/core/__tests__/build-artifacts.spec.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,14 @@ describe('build artifacts', () => {
4646
]);
4747
});
4848

49-
it('declares Vite+ native packages as core optional dependencies', () => {
50-
for (const packageName of getNativePlatformPackageNames(cliPkgJson.napi.targets)) {
49+
it('declares only generated Vite+ native packages as core optional dependencies', () => {
50+
const packageNames = getNativePlatformPackageNames(cliPkgJson.napi.targets);
51+
const nativeOptionalDependencyNames = Object.keys(corePkgJson.optionalDependencies).filter((name) =>
52+
name.startsWith('@voidzero-dev/vite-plus-'),
53+
);
54+
55+
expect(nativeOptionalDependencyNames.toSorted()).toEqual(packageNames.toSorted());
56+
for (const packageName of packageNames) {
5157
expect(corePkgJson.optionalDependencies).toHaveProperty(packageName, corePkgJson.version);
5258
}
5359

packages/core/build-support/native-platform-packages.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const archMap: Record<string, string> = {
33
x86_64: 'x64',
44
};
55

6-
function getNativePlatformPackageSuffix(target: string): string {
6+
export function getNativePlatformPackageName(target: string): string {
77
const [archTarget, ...platformParts] = target.split('-');
88
const arch = archMap[archTarget];
99
const platform = platformParts.join('-');
@@ -13,25 +13,21 @@ function getNativePlatformPackageSuffix(target: string): string {
1313
}
1414

1515
if (platform === 'apple-darwin') {
16-
return `darwin-${arch}`;
16+
return `@voidzero-dev/vite-plus-darwin-${arch}`;
1717
}
1818
if (platform === 'unknown-linux-gnu') {
19-
return `linux-${arch}-gnu`;
19+
return `@voidzero-dev/vite-plus-linux-${arch}-gnu`;
2020
}
2121
if (platform === 'unknown-linux-musl') {
22-
return `linux-${arch}-musl`;
22+
return `@voidzero-dev/vite-plus-linux-${arch}-musl`;
2323
}
2424
if (platform === 'pc-windows-msvc') {
25-
return `win32-${arch}-msvc`;
25+
return `@voidzero-dev/vite-plus-win32-${arch}-msvc`;
2626
}
2727

2828
throw new Error(`Unsupported NAPI target platform: ${target}`);
2929
}
3030

31-
export function getNativePlatformPackageName(target: string): string {
32-
return `@voidzero-dev/vite-plus-${getNativePlatformPackageSuffix(target)}`;
33-
}
34-
3531
export function getNativePlatformPackageNames(targets: string[]): string[] {
3632
return targets.map(getNativePlatformPackageName);
3733
}

packages/core/build.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,8 @@ async function bundleRolldown() {
372372
},
373373
});
374374

375+
const vitePlusNativePackages = new Set(getNativePlatformPackageNames(cliPkgJson.napi.targets));
376+
375377
// Rewrite @rolldown/pluginutils imports in JS and type declaration files
376378
for (const file of rolldownFiles) {
377379
if (
@@ -388,8 +390,10 @@ async function bundleRolldown() {
388390
with: { type: 'json' },
389391
})
390392
).default.version;
391-
// @rolldown/binding-darwin-arm64 → @voidzero-dev/vite-plus-darwin-arm64/binding
392-
source = source.replace(/@rolldown\/binding-([a-z0-9-]+)/g, 'vite-plus/binding');
393+
source = source.replace(/@rolldown\/binding-([a-z0-9-]+)/g, (specifier, suffix) => {
394+
const packageName = `@voidzero-dev/vite-plus-${suffix}`;
395+
return vitePlusNativePackages.has(packageName) ? packageName : specifier;
396+
});
393397
source = source.replaceAll(`${rolldownBindingVersion}`, pkgJson.version);
394398
}
395399
const newSource = rewriteModuleSpecifiers(source, file, { rules });

0 commit comments

Comments
 (0)