Skip to content

Commit

Permalink
fix: make removeNodeProtocol work with shims
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaemami59 authored Feb 27, 2025
1 parent c654e5f commit 769aa49
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 4 deletions.
4 changes: 2 additions & 2 deletions assets/esm_shims.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Shim globals in esm bundle
import { fileURLToPath } from 'url'
import path from 'path'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

const getFilename = () => fileURLToPath(import.meta.url)
const getDirname = () => path.dirname(getFilename())
Expand Down
2 changes: 1 addition & 1 deletion src/esbuild/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export async function runEsbuild(

await pluginContainer.buildStarted()
const esbuildPlugins: Array<EsbuildPlugin | false | undefined> = [
format === 'cjs' && options.removeNodeProtocol && nodeProtocolPlugin(),
options.removeNodeProtocol && nodeProtocolPlugin(),
{
name: 'modify-options',
setup(build) {
Expand Down
4 changes: 3 additions & 1 deletion src/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ const getRollupConfig = async (
entryFileNames: `[name]${outputExtension}`,
chunkFileNames: `[name]-[hash]${outputExtension}`,
plugins: [
format === 'cjs' && options.cjsInterop && FixDtsDefaultCjsExportsPlugin(),
format === 'cjs' &&
options.cjsInterop &&
FixDtsDefaultCjsExportsPlugin(),
].filter(Boolean),
}
}),
Expand Down
134 changes: 134 additions & 0 deletions test/shims.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { test } from 'vitest'
import type { Options } from '../src/index.js'
import { getTestName, run } from './utils.js'

test('removeNodeProtocol works on shims', async ({ expect, task }) => {
const { getFileContent, outFiles } = await run(
getTestName(),
{
'src/index.ts': 'export const foo = __dirname',
'tsup.config.ts': `export default ${JSON.stringify(
{
name: task.name,
entry: { index: 'src/index.ts' },
format: ['esm'],
shims: true,
removeNodeProtocol: true,
} satisfies Options,
null,
2,
)}`,
'package.json': JSON.stringify(
{
name: 'remove-node-protocol-works-on-shims',
description: task.name,
type: 'commonjs',
sideEffects: false,
},
null,
2,
),
'tsconfig.json': JSON.stringify(
{
compilerOptions: {
outDir: './dist',
rootDir: './src',
skipLibCheck: true,
strict: true,
},
include: ['src'],
},
null,
2,
),
},
{
entry: [],
},
)

expect(outFiles).toStrictEqual(['index.mjs'])

const indexMjsContent = `// ../../../assets/esm_shims.js
import path from "path";
import { fileURLToPath } from "url";
var getFilename = () => fileURLToPath(import.meta.url);
var getDirname = () => path.dirname(getFilename());
var __dirname = /* @__PURE__ */ getDirname();
// src/index.ts
var foo = __dirname;
export {
foo
};
`

expect(await getFileContent('dist/index.mjs')).toStrictEqual(indexMjsContent)
})

test('disabling removeNodeProtocol retains node protocol in shims', async ({
expect,
task,
}) => {
const { getFileContent, outFiles } = await run(
getTestName(),
{
'src/index.ts': `export const foo = __dirname`,
'tsup.config.ts': `export default ${JSON.stringify(
{
name: task.name,
entry: { index: 'src/index.ts' },
format: ['esm'],
shims: true,
removeNodeProtocol: false,
} satisfies Options,
null,
2,
)}`,
'package.json': JSON.stringify(
{
name: 'disabling-remove-node-protocol-retains-node-protocol-in-shims',
description: task.name,
type: 'commonjs',
sideEffects: false,
},
null,
2,
),
'tsconfig.json': JSON.stringify(
{
compilerOptions: {
outDir: './dist',
rootDir: './src',
skipLibCheck: true,
strict: true,
},
include: ['src'],
},
null,
2,
),
},
{
entry: [],
},
)

expect(outFiles).toStrictEqual(['index.mjs'])

const indexMjsContent = `// ../../../assets/esm_shims.js
import path from "node:path";
import { fileURLToPath } from "node:url";
var getFilename = () => fileURLToPath(import.meta.url);
var getDirname = () => path.dirname(getFilename());
var __dirname = /* @__PURE__ */ getDirname();
// src/index.ts
var foo = __dirname;
export {
foo
};
`

expect(await getFileContent('dist/index.mjs')).toStrictEqual(indexMjsContent)
})

0 comments on commit 769aa49

Please sign in to comment.