Skip to content

Commit

Permalink
Make removeNodeProtocol work with shims
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaemami59 committed Jan 29, 2025
1 parent bee8f42 commit 5663611
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 3 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 * as 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
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 * as 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 * as 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 5663611

Please sign in to comment.