|
| 1 | +import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises' |
| 2 | +import { tmpdir } from 'node:os' |
1 | 3 | import path from 'node:path' |
2 | 4 | import { fileURLToPath } from 'node:url' |
3 | | -import { rolldownBuild } from '@sxzz/test-utils' |
| 5 | +import { normalizePath, rolldownBuild } from '@sxzz/test-utils' |
4 | 6 | import { describe, expect, test } from 'vitest' |
5 | 7 | import { dts } from '../src/index.ts' |
6 | 8 | import { getTsgoPathFromNodeModules } from '../src/tsgo.ts' |
@@ -157,6 +159,81 @@ describe('dts input', () => { |
157 | 159 | expect(snapshot).toMatchSnapshot() |
158 | 160 | }) |
159 | 161 |
|
| 162 | + test('warns for CommonJS dts input syntax', async () => { |
| 163 | + const warnings: string[] = [] |
| 164 | + |
| 165 | + await rolldownBuild( |
| 166 | + [ |
| 167 | + path.resolve( |
| 168 | + dirname, |
| 169 | + 'rollup-plugin-dts/issue-89-import-equals/index.d.ts', |
| 170 | + ), |
| 171 | + ], |
| 172 | + [dts({ dtsInput: true })], |
| 173 | + { |
| 174 | + onwarn(warning) { |
| 175 | + warnings.push(warning.message) |
| 176 | + }, |
| 177 | + }, |
| 178 | + ) |
| 179 | + |
| 180 | + expect(warnings).toHaveLength(2) |
| 181 | + expect(warnings).toEqual( |
| 182 | + expect.arrayContaining([ |
| 183 | + expect.stringContaining('index.d.ts uses CommonJS dts syntax'), |
| 184 | + expect.stringContaining('bar.d.ts uses CommonJS dts syntax'), |
| 185 | + ]), |
| 186 | + ) |
| 187 | + expect(warnings.join('\n')).toContain( |
| 188 | + 'rolldown-plugin-dts does not support reliably bundling CommonJS dts input', |
| 189 | + ) |
| 190 | + }) |
| 191 | + |
| 192 | + test('warns to externalize CommonJS dts dependencies', async () => { |
| 193 | + const root = await mkdtemp(path.join(tmpdir(), 'rolldown-plugin-dts-')) |
| 194 | + const dep = path.join(root, 'node_modules/cjs-dts-dep') |
| 195 | + const warnings: string[] = [] |
| 196 | + |
| 197 | + try { |
| 198 | + await mkdir(dep, { recursive: true }) |
| 199 | + await Promise.all([ |
| 200 | + writeFile( |
| 201 | + path.join(root, 'index.d.ts'), |
| 202 | + `export interface Wrapped {\n value: import("cjs-dts-dep").Value\n}\n`, |
| 203 | + ), |
| 204 | + writeFile( |
| 205 | + path.join(dep, 'package.json'), |
| 206 | + `{"name":"cjs-dts-dep","types":"index.d.ts"}`, |
| 207 | + ), |
| 208 | + writeFile( |
| 209 | + path.join(dep, 'index.d.ts'), |
| 210 | + `declare namespace CjsDtsDep {\n export interface Value {\n foo: string\n }\n}\nexport = CjsDtsDep\n`, |
| 211 | + ), |
| 212 | + ]) |
| 213 | + |
| 214 | + await rolldownBuild( |
| 215 | + [path.join(root, 'index.d.ts')], |
| 216 | + [dts({ dtsInput: true })], |
| 217 | + { |
| 218 | + cwd: root, |
| 219 | + onwarn(warning) { |
| 220 | + warnings.push(warning.message) |
| 221 | + }, |
| 222 | + }, |
| 223 | + ) |
| 224 | + } finally { |
| 225 | + await rm(root, { force: true, recursive: true }) |
| 226 | + } |
| 227 | + |
| 228 | + const cjsWarning = |
| 229 | + warnings.find((warning) => |
| 230 | + normalizePath(warning).includes('node_modules/cjs-dts-dep/index.d.ts'), |
| 231 | + ) ?? '' |
| 232 | + |
| 233 | + expect(cjsWarning).toContain('uses CommonJS dts syntax') |
| 234 | + expect(cjsWarning).toContain('Please mark this module as external') |
| 235 | + }) |
| 236 | + |
160 | 237 | test('input object', async () => { |
161 | 238 | const { snapshot, chunks } = await rolldownBuild( |
162 | 239 | { index: path.resolve(dirname, 'fixtures/dts-input.d.ts') }, |
|
0 commit comments