|
| 1 | +import { test } from 'vitest' |
| 2 | +import type { Options } from '../src/index.js' |
| 3 | +import { getTestName, run } from './utils.js' |
| 4 | + |
| 5 | +test('removeNodeProtocol works on shims', async ({ expect, task }) => { |
| 6 | + const { getFileContent, outFiles } = await run( |
| 7 | + getTestName(), |
| 8 | + { |
| 9 | + 'src/index.ts': 'export const foo = __dirname', |
| 10 | + 'tsup.config.ts': `export default ${JSON.stringify( |
| 11 | + { |
| 12 | + name: task.name, |
| 13 | + entry: { index: 'src/index.ts' }, |
| 14 | + format: ['esm'], |
| 15 | + shims: true, |
| 16 | + removeNodeProtocol: true, |
| 17 | + } satisfies Options, |
| 18 | + null, |
| 19 | + 2, |
| 20 | + )}`, |
| 21 | + 'package.json': JSON.stringify( |
| 22 | + { |
| 23 | + name: 'remove-node-protocol-works-on-shims', |
| 24 | + description: task.name, |
| 25 | + type: 'commonjs', |
| 26 | + sideEffects: false, |
| 27 | + }, |
| 28 | + null, |
| 29 | + 2, |
| 30 | + ), |
| 31 | + 'tsconfig.json': JSON.stringify( |
| 32 | + { |
| 33 | + compilerOptions: { |
| 34 | + outDir: './dist', |
| 35 | + rootDir: './src', |
| 36 | + skipLibCheck: true, |
| 37 | + strict: true, |
| 38 | + }, |
| 39 | + include: ['src'], |
| 40 | + }, |
| 41 | + null, |
| 42 | + 2, |
| 43 | + ), |
| 44 | + }, |
| 45 | + { |
| 46 | + entry: [], |
| 47 | + }, |
| 48 | + ) |
| 49 | + |
| 50 | + expect(outFiles).toStrictEqual(['index.mjs']) |
| 51 | + |
| 52 | + const indexMjsContent = `// ../../../assets/esm_shims.js |
| 53 | +import path from "path"; |
| 54 | +import { fileURLToPath } from "url"; |
| 55 | +var getFilename = () => fileURLToPath(import.meta.url); |
| 56 | +var getDirname = () => path.dirname(getFilename()); |
| 57 | +var __dirname = /* @__PURE__ */ getDirname(); |
| 58 | +
|
| 59 | +// src/index.ts |
| 60 | +var foo = __dirname; |
| 61 | +export { |
| 62 | + foo |
| 63 | +}; |
| 64 | +` |
| 65 | + |
| 66 | + expect(await getFileContent('dist/index.mjs')).toStrictEqual(indexMjsContent) |
| 67 | +}) |
| 68 | + |
| 69 | +test('disabling removeNodeProtocol retains node protocol in shims', async ({ |
| 70 | + expect, |
| 71 | + task, |
| 72 | +}) => { |
| 73 | + const { getFileContent, outFiles } = await run( |
| 74 | + getTestName(), |
| 75 | + { |
| 76 | + 'src/index.ts': `export const foo = __dirname`, |
| 77 | + 'tsup.config.ts': `export default ${JSON.stringify( |
| 78 | + { |
| 79 | + name: task.name, |
| 80 | + entry: { index: 'src/index.ts' }, |
| 81 | + format: ['esm'], |
| 82 | + shims: true, |
| 83 | + removeNodeProtocol: false, |
| 84 | + } satisfies Options, |
| 85 | + null, |
| 86 | + 2, |
| 87 | + )}`, |
| 88 | + 'package.json': JSON.stringify( |
| 89 | + { |
| 90 | + name: 'disabling-remove-node-protocol-retains-node-protocol-in-shims', |
| 91 | + description: task.name, |
| 92 | + type: 'commonjs', |
| 93 | + sideEffects: false, |
| 94 | + }, |
| 95 | + null, |
| 96 | + 2, |
| 97 | + ), |
| 98 | + 'tsconfig.json': JSON.stringify( |
| 99 | + { |
| 100 | + compilerOptions: { |
| 101 | + outDir: './dist', |
| 102 | + rootDir: './src', |
| 103 | + skipLibCheck: true, |
| 104 | + strict: true, |
| 105 | + }, |
| 106 | + include: ['src'], |
| 107 | + }, |
| 108 | + null, |
| 109 | + 2, |
| 110 | + ), |
| 111 | + }, |
| 112 | + { |
| 113 | + entry: [], |
| 114 | + }, |
| 115 | + ) |
| 116 | + |
| 117 | + expect(outFiles).toStrictEqual(['index.mjs']) |
| 118 | + |
| 119 | + const indexMjsContent = `// ../../../assets/esm_shims.js |
| 120 | +import path from "node:path"; |
| 121 | +import { fileURLToPath } from "node:url"; |
| 122 | +var getFilename = () => fileURLToPath(import.meta.url); |
| 123 | +var getDirname = () => path.dirname(getFilename()); |
| 124 | +var __dirname = /* @__PURE__ */ getDirname(); |
| 125 | +
|
| 126 | +// src/index.ts |
| 127 | +var foo = __dirname; |
| 128 | +export { |
| 129 | + foo |
| 130 | +}; |
| 131 | +` |
| 132 | + |
| 133 | + expect(await getFileContent('dist/index.mjs')).toStrictEqual(indexMjsContent) |
| 134 | +}) |
0 commit comments