package: CommonJS projects get their own declarations (dist/types-cjs; node16 / nodenext refused an ESM-only .d.ts from a CJS file, TS1541); the package-types test covers ES module and CommonJS projects under bundler / node16 / nodenext / node10 and loads the package with import() and require() under Node; pitfall 69
This commit is contained in:
parent
d717ceb4cd
commit
04647c079e
6 changed files with 42 additions and 14 deletions
|
|
@ -22,17 +22,25 @@ describe('package types', () => {
|
|||
mkdirSync(sb, { recursive: true });
|
||||
writeFileSync(join(sb, 'package.json'), JSON.stringify(pkg));
|
||||
cpSync(dist, join(sb, 'dist'), { recursive: true });
|
||||
expect(pkg.files).toEqual(expect.arrayContaining(['dist/config.js', 'dist/types']));
|
||||
expect(pkg.exports['.'].types).toBe('./dist/types/lib/config.d.ts');
|
||||
expect(pkg.files).toEqual(expect.arrayContaining(['dist/config.js', 'dist/types', 'dist/types-cjs']));
|
||||
symlinkSync(join(ROOT, 'node_modules', 'typescript'), join(dir, 'app', 'node_modules', 'typescript'));
|
||||
cpSync(join(ROOT, 'templates', 'storebound.config.ts'), join(dir, 'app', 'storebound.config.ts'));
|
||||
// an ES module project (node16 / nodenext read the nearest package.json; Next / Vite use bundler resolution)
|
||||
writeFileSync(join(dir, 'app', 'package.json'), JSON.stringify({ name: 'app', private: true, type: 'module' }));
|
||||
for (const [module, moduleResolution] of [['esnext', 'bundler'], ['node16', 'node16'], ['nodenext', 'nodenext']]) {
|
||||
// no @types/node, skipLibCheck off: the strictest project a config can land in
|
||||
writeFileSync(join(dir, 'app', 'tsconfig.json'), JSON.stringify({ compilerOptions: { strict: true, noEmit: true, target: 'es2022', module, moduleResolution, skipLibCheck: false, types: [] }, include: ['*.ts'] }));
|
||||
const r = Bun.spawnSync(['node', TSC, '-p', '.'], { cwd: join(dir, 'app'), stdout: 'pipe', stderr: 'pipe' });
|
||||
expect(`${moduleResolution}: ${r.stdout.toString()}${r.stderr.toString()}`.trim()).toBe(`${moduleResolution}:`);
|
||||
// every project a config lands in: ES module and CommonJS (node16 / nodenext read the nearest package.json),
|
||||
// Next / Vite / Expo (bundler), old tsconfigs (node10 reads "types"); no @types/node, skipLibCheck off
|
||||
const results: string[] = [];
|
||||
for (const type of ['module', 'commonjs']) {
|
||||
writeFileSync(join(dir, 'app', 'package.json'), JSON.stringify({ name: 'app', private: true, type }));
|
||||
for (const [module, moduleResolution] of [['esnext', 'bundler'], ['preserve', 'bundler'], ['node16', 'node16'], ['nodenext', 'nodenext'], ['commonjs', 'node10']]) {
|
||||
writeFileSync(join(dir, 'app', 'tsconfig.json'), JSON.stringify({ compilerOptions: { strict: true, noEmit: true, target: 'es2022', module, moduleResolution, skipLibCheck: false, types: [] }, include: ['*.ts'] }));
|
||||
const r = Bun.spawnSync(['node', TSC, '-p', '.'], { cwd: join(dir, 'app'), stdout: 'pipe', stderr: 'pipe' });
|
||||
results.push(`${type} ${module}/${moduleResolution}: ${`${r.stdout.toString()}${r.stderr.toString()}`.trim() || 'ok'}`);
|
||||
}
|
||||
}
|
||||
expect(results.filter(r => !r.endsWith(': ok'))).toEqual([]);
|
||||
// the runtime entry under Node: import() and require() of 'storebound' load (no TypeScript source, no Bun)
|
||||
for (const code of [`import('storebound').then(m => console.log(typeof m.validateConfig))`, `console.log(typeof require('storebound').validateConfig)`]) {
|
||||
const r = Bun.spawnSync(['node', '-e', code], { cwd: join(dir, 'app'), stdout: 'pipe', stderr: 'pipe' });
|
||||
expect(`${r.stdout.toString().trim()}${r.stderr.toString().split('\n')[0]}`).toBe('function');
|
||||
}
|
||||
} finally { rmSync(dir, { recursive: true, force: true }); }
|
||||
}, 120_000);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue