Skip to content

Commit c59e797

Browse files
committed
feat(migrate): migrate external/noExternal to deps namespace
1 parent ed3f62e commit c59e797

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed

packages/migrate/src/helpers/__snapshots__/tsup-config.test.ts.snap

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,43 @@ exports[`default values > should not add format when present > code 1`] = `
7474
}"
7575
`;
7676

77+
exports[`deps namespace migrations > external should move to deps.neverBundle > code 1`] = `
78+
"export default {
79+
deps: {
80+
neverBundle: ['foo', 'bar'],
81+
},
82+
format: 'cjs',
83+
clean: false,
84+
dts: false,
85+
target: false,
86+
}"
87+
`;
88+
89+
exports[`deps namespace migrations > multiple deps options should merge into single deps object > code 1`] = `
90+
"export default {
91+
deps: {
92+
neverBundle: ['foo'],
93+
alwaysBundle: ['bar'],
94+
},
95+
format: 'cjs',
96+
clean: false,
97+
dts: false,
98+
target: false,
99+
}"
100+
`;
101+
102+
exports[`deps namespace migrations > noExternal should move to deps.alwaysBundle > code 1`] = `
103+
"export default {
104+
deps: {
105+
alwaysBundle: ['foo'],
106+
},
107+
format: 'cjs',
108+
clean: false,
109+
dts: false,
110+
target: false,
111+
}"
112+
`;
113+
77114
exports[`option transformations > bundle: false should transform to unbundle: true > code 1`] = `
78115
"export default {
79116
unbundle: true,

packages/migrate/src/helpers/tsup-config.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,47 @@ describe('option transformations', () => {
119119
})
120120
})
121121

122+
describe('deps namespace migrations', () => {
123+
test('external should move to deps.neverBundle', () => {
124+
const input = `
125+
export default {
126+
external: ['foo', 'bar'],
127+
}
128+
`
129+
const { code } = transform(input, 'tsup.config.ts')
130+
expect(code).toContain('deps:')
131+
expect(code).toContain('neverBundle:')
132+
expect(code).not.toContain('external:')
133+
})
134+
135+
test('noExternal should move to deps.alwaysBundle', () => {
136+
const input = `
137+
export default {
138+
noExternal: ['foo'],
139+
}
140+
`
141+
const { code } = transform(input, 'tsup.config.ts')
142+
expect(code).toContain('deps:')
143+
expect(code).toContain('alwaysBundle:')
144+
expect(code).not.toContain('noExternal:')
145+
})
146+
147+
test('multiple deps options should merge into single deps object', () => {
148+
const input = `
149+
export default {
150+
external: ['foo'],
151+
noExternal: ['bar'],
152+
}
153+
`
154+
const { code } = transform(input, 'tsup.config.ts')
155+
expect(code).toContain('deps:')
156+
expect(code).toContain('neverBundle:')
157+
expect(code).toContain('alwaysBundle:')
158+
expect(code).not.toContain('external:')
159+
expect(code).not.toContain('noExternal:')
160+
})
161+
})
162+
122163
describe('warning options', () => {
123164
test('metafile should emit warning (use Vite DevTools)', () => {
124165
const input = `

packages/migrate/src/helpers/tsup-config.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ const PROPERTY_RENAMES: Record<string, string> = {
3636
cjsInterop: 'cjsDefault',
3737
}
3838

39+
// Properties to move under `deps` namespace: oldName -> newNameUnderDeps
40+
const DEPS_NAMESPACE_RENAMES: Record<string, string> = {
41+
external: 'neverBundle',
42+
noExternal: 'alwaysBundle',
43+
}
44+
3945
/**
4046
* Transform tsup config code to tsdown config code.
4147
* This function applies all migration rules and returns the transformed code
@@ -194,7 +200,20 @@ export function transformTsupConfig(
194200
edits.push(node.replace("nodeProtocol: 'strip'"))
195201
}
196202

197-
// 7. Transform tsup/TSUP identifiers
203+
// 7. Move properties into deps namespace
204+
const depsProperties: { name: string; value: string }[] = []
205+
for (const [oldName, newName] of Object.entries(DEPS_NAMESPACE_RENAMES)) {
206+
const pair = findPropertyPair(oldName)
207+
if (pair) {
208+
const valueNode = pair.field('value')
209+
if (valueNode) {
210+
depsProperties.push({ name: newName, value: valueNode.text() })
211+
}
212+
edits.push(pair.replace(''))
213+
}
214+
}
215+
216+
// 8. Transform tsup/TSUP identifiers
198217
const tsupIdentifiers = root.findAll({
199218
rule: {
200219
kind: 'identifier',
@@ -287,6 +306,13 @@ export function transformTsupConfig(
287306

288307
const missingDefaults: string[] = []
289308
if (configObjectNode) {
309+
if (depsProperties.length > 0) {
310+
const depsEntries = depsProperties
311+
.map((p) => `${p.name}: ${p.value}`)
312+
.join(',\n ')
313+
missingDefaults.push(`deps: {\n ${depsEntries},\n }`)
314+
}
315+
290316
if (!hasOption('format')) missingDefaults.push("format: 'cjs'")
291317
if (!hasOption('clean')) missingDefaults.push('clean: false')
292318
if (!hasOption('dts')) missingDefaults.push('dts: false')

0 commit comments

Comments
 (0)