Skip to content

Commit 769aa49

Browse files
authored
fix: make removeNodeProtocol work with shims
1 parent c654e5f commit 769aa49

File tree

4 files changed

+140
-4
lines changed

4 files changed

+140
-4
lines changed

assets/esm_shims.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Shim globals in esm bundle
2-
import { fileURLToPath } from 'url'
3-
import path from 'path'
2+
import path from 'node:path'
3+
import { fileURLToPath } from 'node:url'
44

55
const getFilename = () => fileURLToPath(import.meta.url)
66
const getDirname = () => path.dirname(getFilename())

src/esbuild/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export async function runEsbuild(
119119

120120
await pluginContainer.buildStarted()
121121
const esbuildPlugins: Array<EsbuildPlugin | false | undefined> = [
122-
format === 'cjs' && options.removeNodeProtocol && nodeProtocolPlugin(),
122+
options.removeNodeProtocol && nodeProtocolPlugin(),
123123
{
124124
name: 'modify-options',
125125
setup(build) {

src/rollup.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ const getRollupConfig = async (
149149
entryFileNames: `[name]${outputExtension}`,
150150
chunkFileNames: `[name]-[hash]${outputExtension}`,
151151
plugins: [
152-
format === 'cjs' && options.cjsInterop && FixDtsDefaultCjsExportsPlugin(),
152+
format === 'cjs' &&
153+
options.cjsInterop &&
154+
FixDtsDefaultCjsExportsPlugin(),
153155
].filter(Boolean),
154156
}
155157
}),

test/shims.test.ts

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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

Comments
 (0)