diff --git a/src/esbuild/external.ts b/src/esbuild/external.ts index 94d3478f..5a58d873 100644 --- a/src/esbuild/external.ts +++ b/src/esbuild/external.ts @@ -6,18 +6,14 @@ const NON_NODE_MODULE_RE = /^[^.\/]|^\.[^.\/]|^\.\.[^\/]/ export const externalPlugin = ({ patterns, skipNodeModulesBundle, - disabled, }: { patterns?: (string | RegExp)[] skipNodeModulesBundle?: boolean - disabled?: boolean }): Plugin => { return { name: `external`, setup(build) { - if (disabled) return - if (skipNodeModulesBundle) { build.onResolve({ filter: NON_NODE_MODULE_RE }, (args) => ({ path: args.path, diff --git a/src/esbuild/index.ts b/src/esbuild/index.ts index c13447f3..f19c88e3 100644 --- a/src/esbuild/index.ts +++ b/src/esbuild/index.ts @@ -12,6 +12,7 @@ import { import { NormalizedOptions, Format } from '..' import { getDeps, loadPkg } from '../load' import { log } from '../log' +import { nodeProtocolPlugin } from './node-protocol' import { externalPlugin } from './external' import { postcssPlugin } from './postcss' import { sveltePlugin } from './svelte' @@ -94,6 +95,7 @@ export async function runEsbuild( ? ['module', 'main'] : ['browser', 'module', 'main'], plugins: [ + ...(format === 'cjs' ? [nodeProtocolPlugin()] : []), { name: 'modify-options', setup(build) { @@ -104,12 +106,10 @@ export async function runEsbuild( }, // esbuild's `external` option doesn't support RegExp // So here we use a custom plugin to implement it - externalPlugin({ - // everything should be bundled for iife format - disabled: format === 'iife', + ...(format !== 'iife' ? [externalPlugin({ patterns: external, skipNodeModulesBundle: options.skipNodeModulesBundle, - }), + })] : []), postcssPlugin({ css }), sveltePlugin({ css }), ...(options.esbuildPlugins || []), diff --git a/src/esbuild/node-protocol.ts b/src/esbuild/node-protocol.ts new file mode 100644 index 00000000..1bf66228 --- /dev/null +++ b/src/esbuild/node-protocol.ts @@ -0,0 +1,21 @@ +import { Plugin } from 'esbuild' + +/** + * The node: protocol was added to require in Node v14.18.0 + * https://nodejs.org/api/esm.html#node-imports + */ +export const nodeProtocolPlugin = (): Plugin => { + const nodeProtocol = 'node:' + + return { + name: 'node-protocol-plugin', + setup({ onResolve }) { + onResolve({ + filter: /^node:/, + }, ({ path }) => ({ + path: path.slice(nodeProtocol.length), + external: true + })) + } + } +} diff --git a/test/index.test.ts b/test/index.test.ts index 1249fc9d..96a6ee39 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -556,6 +556,37 @@ test('import css in --dts', async () => { `) }) +test('node protocol', async () => { + const { output } = await run(getTestName(), { + 'input.ts': `import fs from 'node:fs'; console.log(fs)`, + }) + expect(output).toMatchInlineSnapshot(` + "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 __markAsModule = (target) => __defProp(target, \\"__esModule\\", { value: true }); + var __reExport = (target, module2, desc) => { + if (module2 && typeof module2 === \\"object\\" || typeof module2 === \\"function\\") { + for (let key of __getOwnPropNames(module2)) + if (!__hasOwnProp.call(target, key) && key !== \\"default\\") + __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable }); + } + return target; + }; + var __toModule = (module2) => { + return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, \\"default\\", module2 && module2.__esModule && \\"default\\" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2); + }; + + // input.ts + var import_node_fs = __toModule(require(\\"fs\\")); + console.log(import_node_fs.default); + " + `) +}) + test('external', async () => { const { output } = await run(getTestName(), { 'input.ts': `export {foo} from 'foo'