|
1 | 1 | import fs from 'node:fs' |
2 | | -import { mkdir, readFile, rm, symlink } from 'node:fs/promises' |
| 2 | +import { |
| 3 | + mkdir, |
| 4 | + mkdtemp, |
| 5 | + readFile, |
| 6 | + rm, |
| 7 | + symlink, |
| 8 | + writeFile, |
| 9 | +} from 'node:fs/promises' |
3 | 10 | import module, { createRequire } from 'node:module' |
| 11 | +import { tmpdir } from 'node:os' |
4 | 12 | import path from 'node:path' |
5 | 13 | import url, { fileURLToPath } from 'node:url' |
6 | 14 | import { rolldownBuild } from '@sxzz/test-utils' |
@@ -74,6 +82,45 @@ test.each([ |
74 | 82 | ).rejects.toThrow(message) |
75 | 83 | }) |
76 | 84 |
|
| 85 | +test('loads the API from an aliased moduleUrl next to another typescript', async () => { |
| 86 | + const dir = await mkdtemp(path.join(tmpdir(), 'rolldown-plugin-dts-')) |
| 87 | + try { |
| 88 | + const writePackage = async ( |
| 89 | + name: string, |
| 90 | + files: Record<string, string>, |
| 91 | + ) => { |
| 92 | + for (const [file, content] of Object.entries(files)) { |
| 93 | + const filePath = path.join(dir, 'node_modules', name, file) |
| 94 | + await mkdir(path.dirname(filePath), { recursive: true }) |
| 95 | + await writeFile(filePath, content) |
| 96 | + } |
| 97 | + } |
| 98 | + await writePackage('typescript', { |
| 99 | + 'package.json': JSON.stringify({ name: 'typescript', version: '5.9.3' }), |
| 100 | + 'async.js': 'module.exports = { Program: class {} }', |
| 101 | + }) |
| 102 | + await writePackage('typescript-next', { |
| 103 | + 'package.json': JSON.stringify({ |
| 104 | + name: 'typescript', |
| 105 | + version: '7.1.0', |
| 106 | + exports: { '.': './lib/version.cjs', './async': './async.js' }, |
| 107 | + }), |
| 108 | + 'lib/version.cjs': '', |
| 109 | + 'async.js': |
| 110 | + 'module.exports = { Program: class { getDeclarationEmit() {} } }', |
| 111 | + }) |
| 112 | + |
| 113 | + const aliasedModuleUrl = url.pathToFileURL( |
| 114 | + path.join(dir, 'node_modules/typescript-next/lib/version.cjs'), |
| 115 | + ).href |
| 116 | + expect( |
| 117 | + loadTsgoApi(aliasedModuleUrl).Program.prototype.getDeclarationEmit, |
| 118 | + ).toBeTypeOf('function') |
| 119 | + } finally { |
| 120 | + await rm(dir, { recursive: true, force: true }) |
| 121 | + } |
| 122 | +}) |
| 123 | + |
77 | 124 | test('concurrent disk emits share configured project membership until the next build', async () => { |
78 | 125 | const { Program, Snapshot } = loadTsgoApi(moduleUrl) |
79 | 126 | const getSourceFileNames = vi.spyOn(Program.prototype, 'getSourceFileNames') |
|
0 commit comments