Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make removeNodeProtocol work with shims #1242

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})