Skip to content

Commit a1a1ea9

Browse files
fix(migrate): preserve existing tsdown pack wiring
1 parent c337665 commit a1a1ea9

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

packages/cli/src/migration/__tests__/migrator.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8891,6 +8891,45 @@ export default defineConfig({
88918891
tsdownConfig: false,
88928892
});
88938893
});
8894+
8895+
it('preserves a tsdown config already wired to pack under a different import name', () => {
8896+
fs.writeFileSync(
8897+
path.join(tmpDir, 'package.json'),
8898+
JSON.stringify({ name: 'test', devDependencies: { 'vite-plus': 'latest' } }, null, 2),
8899+
);
8900+
fs.writeFileSync(
8901+
path.join(tmpDir, 'vite.config.ts'),
8902+
`import packConfig from './tsdown.config.js';
8903+
8904+
export default { pack: packConfig({}) };
8905+
`,
8906+
);
8907+
fs.writeFileSync(
8908+
path.join(tmpDir, 'tsdown.config.ts'),
8909+
`import { defineConfig } from 'tsdown';
8910+
8911+
export default defineConfig({ entry: 'src/index.ts' });
8912+
`,
8913+
);
8914+
8915+
const originalViteConfig = fs.readFileSync(path.join(tmpDir, 'vite.config.ts'), 'utf8');
8916+
const report = createMigrationReport();
8917+
const result = finalizeCoreMigrationForExistingVitePlus(
8918+
makeWorkspaceInfo(tmpDir, PackageManager.pnpm),
8919+
true,
8920+
report,
8921+
);
8922+
8923+
expect(result).toEqual({
8924+
scripts: false,
8925+
tsconfigTypes: false,
8926+
imports: true,
8927+
tsdownConfig: false,
8928+
});
8929+
expect(fs.readFileSync(path.join(tmpDir, 'vite.config.ts'), 'utf8')).toBe(originalViteConfig);
8930+
expect(report.tsdownImportCount).toBe(0);
8931+
expect(report.manualSteps).toEqual([]);
8932+
});
88948933
});
88958934

88968935
// Regression: templates such as `create-fate` ship a populated vite.config.ts

packages/cli/src/migration/migrator/vite-config.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,29 @@ export function mergeTsdownConfigFile(
193193

194194
// For TS/JS files, import the config file
195195
const tsdownRelativePath = `./${configs.tsdownConfig}`;
196+
// Do not prepend a second `pack` key when the config is already wired under
197+
// a different local import name. If a pack config exists but does not import
198+
// this tsdown config, leave both files untouched and keep the manual follow-up.
199+
if (hasConfigKey(fullViteConfigPath, 'pack')) {
200+
const viteConfigContent = fs.readFileSync(fullViteConfigPath, 'utf8');
201+
const runtimeImportPath = tsdownRelativePath
202+
.replace(/\.mts$/, '.mjs')
203+
.replace(/\.cts$/, '.cjs')
204+
.replace(/\.ts$/, '.js');
205+
const importsTsdownConfig = [tsdownRelativePath, runtimeImportPath].some(
206+
(importPath) =>
207+
viteConfigContent.includes(`from '${importPath}'`) ||
208+
viteConfigContent.includes(`from "${importPath}"`),
209+
);
210+
if (!importsTsdownConfig) {
211+
infoMigration(
212+
`Please manually merge ${displayRelative(fullTsdownConfigPath)} into ${displayRelative(fullViteConfigPath)}, see https://viteplus.dev/guide/migrate#tsdown`,
213+
report,
214+
);
215+
}
216+
return false;
217+
}
218+
196219
const result = mergeTsdownConfig(fullViteConfigPath, tsdownRelativePath);
197220
if (result.updated) {
198221
fs.writeFileSync(fullViteConfigPath, result.content);

0 commit comments

Comments
 (0)