From e7ced3474a559e35fc5b9cbd4cfd4aa39d44e0c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Thu, 18 Jul 2024 04:21:47 +0800 Subject: [PATCH] feat: add option to retain node protocol --- src/esbuild/index.ts | 2 +- src/index.ts | 1 + src/options.ts | 8 +++++++ test/__snapshots__/index.test.ts.snap | 31 +++++++++++++++++++++++++++ test/index.test.ts | 13 +++++++++++ 5 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/esbuild/index.ts b/src/esbuild/index.ts index 84e7f5d30..9b07f24e9 100644 --- a/src/esbuild/index.ts +++ b/src/esbuild/index.ts @@ -119,7 +119,7 @@ export async function runEsbuild( await pluginContainer.buildStarted() const esbuildPlugins: Array = [ - format === 'cjs' && nodeProtocolPlugin(), + format === 'cjs' && options.removeNodeProtocol && nodeProtocolPlugin(), { name: 'modify-options', setup(build) { diff --git a/src/index.ts b/src/index.ts index b4cfe2778..e3ec00721 100644 --- a/src/index.ts +++ b/src/index.ts @@ -79,6 +79,7 @@ const normalizeOptions = async ( const options: Partial = { outDir: 'dist', + removeNodeProtocol: true, ..._options, format: typeof _options.format === 'string' diff --git a/src/options.ts b/src/options.ts index f6f07d51f..68c8d3ff4 100644 --- a/src/options.ts +++ b/src/options.ts @@ -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 { diff --git a/test/__snapshots__/index.test.ts.snap b/test/__snapshots__/index.test.ts.snap index 308b91015..b37ae17dd 100644 --- a/test/__snapshots__/index.test.ts.snap +++ b/test/__snapshots__/index.test.ts.snap @@ -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; diff --git a/test/index.test.ts b/test/index.test.ts index 0a95026fd..5b07554fe 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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 () => {