Skip to content

Commit

Permalink
feat: add option to retain node protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Jul 17, 2024
1 parent 809c57a commit e7ced34
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
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' && nodeProtocolPlugin(),
format === 'cjs' && options.removeNodeProtocol && nodeProtocolPlugin(),
{
name: 'modify-options',
setup(build) {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const normalizeOptions = async (

const options: Partial<NormalizedOptions> = {
outDir: 'dist',
removeNodeProtocol: true,
..._options,
format:
typeof _options.format === 'string'
Expand Down
8 changes: 8 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ export type Options = {
* @default false
*/
cjsInterop?: boolean

/**
* Remove `node:` protocol from imports
*
* The default value will be flipped to `false` in the next major release
* @default true
*/
removeNodeProtocol?: boolean
}

export interface NormalizedExperimentalDtsConfig {
Expand Down
31 changes: 31 additions & 0 deletions test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,37 @@ module.exports = 123;
"
`;

exports[`don't remove node protocol 1`] = `
""use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
// input.ts
var import_node_fs = __toESM(require("node:fs"));
console.log(import_node_fs.default);
"
`;

exports[`external 1`] = `
""use strict";
var __defProp = Object.defineProperty;
Expand Down
13 changes: 13 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,19 @@ test('node protocol', async () => {
'input.ts': `import fs from 'node:fs'; console.log(fs)`,
})
expect(output).toMatchSnapshot()
expect(output).not.contain('node:fs')
})

test("don't remove node protocol", async () => {
const { output } = await run(getTestName(), {
'input.ts': `import fs from 'node:fs'; console.log(fs)`,
'tsup.config.ts': `
export default {
removeNodeProtocol: false,
}`,
})
expect(output).toMatchSnapshot()
expect(output).contain('node:fs')
})

test('external', async () => {
Expand Down

0 comments on commit e7ced34

Please sign in to comment.