|
| 1 | +import { expect, it } from 'bun:test' |
| 2 | +import { copyFileSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' |
| 3 | +import { tmpdir } from 'node:os' |
| 4 | +import { dirname, join } from 'node:path' |
| 5 | + |
| 6 | +const root = join(import.meta.dir, '..') |
| 7 | +const configs = [ |
| 8 | + 'storage/framework/tsconfig.base.json', |
| 9 | + 'storage/framework/tsconfig.framework.json', |
| 10 | + 'storage/framework/core/tsconfig.json', |
| 11 | + 'storage/framework/core/tsconfig.build.json', |
| 12 | +] |
| 13 | + |
| 14 | +it.each([ |
| 15 | + '../tsconfig.build.json', |
| 16 | + '../tsconfig.json', |
| 17 | + '../../tsconfig.framework.json', |
| 18 | +])('prefers installed dependencies with %s and retains the Pantry fallback', async (extendsPath) => { |
| 19 | + const directory = mkdtempSync(join(tmpdir(), 'stacks-dependency-resolution-')) |
| 20 | + const entry = join(directory, 'storage/framework/core/example/src/index.ts') |
| 21 | + function write(path: string, contents: string): void { |
| 22 | + mkdirSync(dirname(path), { recursive: true }) |
| 23 | + writeFileSync(path, contents) |
| 24 | + } |
| 25 | + async function run(...args: string[]): Promise<string> { |
| 26 | + const proc = Bun.spawn([process.execPath, `--config=${join(directory, 'bunfig.toml')}`, ...args], { |
| 27 | + cwd: directory, |
| 28 | + stdout: 'pipe', |
| 29 | + stderr: 'pipe', |
| 30 | + }) |
| 31 | + const [output, errors, code] = await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text(), proc.exited]) |
| 32 | + if (code !== 0) throw new Error(errors) |
| 33 | + return output.trim() |
| 34 | + } |
| 35 | + try { |
| 36 | + for (const config of configs) { |
| 37 | + const destination = join(directory, config) |
| 38 | + mkdirSync(dirname(destination), { recursive: true }) |
| 39 | + copyFileSync(join(root, config), destination) |
| 40 | + } |
| 41 | + write(join(directory, 'bunfig.toml'), '# Isolated dependency-resolution fixture\n') |
| 42 | + write(join(directory, 'storage/framework/core/example/tsconfig.json'), JSON.stringify({ extends: extendsPath })) |
| 43 | + write(entry, "import version from 'fixture-dependency'\nconsole.log(version)\n") |
| 44 | + for (const [tree, version] of [['node_modules', 'installed'], ['pantry', 'pantry']] as const) { |
| 45 | + const dependency = join(directory, tree, 'fixture-dependency') |
| 46 | + write(join(dependency, 'package.json'), JSON.stringify({ name: 'fixture-dependency', type: 'module', exports: './index.ts' })) |
| 47 | + write(join(dependency, 'index.ts'), `export default '${version}'\n`) |
| 48 | + } |
| 49 | + |
| 50 | + for (const expected of ['installed', 'pantry']) { |
| 51 | + expect(await run(entry)).toBe(expected) |
| 52 | + const bundle = join(directory, 'bundle.js') |
| 53 | + await run('build', entry, '--target=bun', '--outfile', bundle) |
| 54 | + expect(await run(bundle)).toBe(expected) |
| 55 | + rmSync(join(directory, 'node_modules'), { recursive: true, force: true }) |
| 56 | + } |
| 57 | + } |
| 58 | + finally { |
| 59 | + rmSync(directory, { recursive: true, force: true }) |
| 60 | + } |
| 61 | +}) |
0 commit comments