feat: add option to retain node protocol

This commit is contained in:
三咲智子 Kevin Deng 2024-07-18 04:21:47 +08:00
parent 809c57abb1
commit e7ced3474a
No known key found for this signature in database
5 changed files with 54 additions and 1 deletions

View File

@ -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) {

View File

@ -79,6 +79,7 @@ const normalizeOptions = async (
const options: Partial<NormalizedOptions> = {
outDir: 'dist',
removeNodeProtocol: true,
..._options,
format:
typeof _options.format === 'string'

View File

@ -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 {

View File

@ -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;

View File

@ -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 () => {