diff --git a/src/runtimes/node/bundlers/esbuild/bundler.ts b/src/runtimes/node/bundlers/esbuild/bundler.ts index e367c4070..2f4e319dd 100644 --- a/src/runtimes/node/bundlers/esbuild/bundler.ts +++ b/src/runtimes/node/bundlers/esbuild/bundler.ts @@ -11,6 +11,7 @@ import type { RuntimeName } from '../../../runtime' import { getBundlerTarget } from './bundler_target' import { getDynamicImportsPlugin } from './plugin_dynamic_imports' import { getNativeModulesPlugin } from './plugin_native_modules' +import { getNodeBuiltinPlugin } from './plugin_node_builtin' // Maximum number of log messages that an esbuild instance will produce. This // limit is important to avoid out-of-memory errors due to too much data being @@ -63,6 +64,7 @@ const bundleJsFile = async function ({ // The list of esbuild plugins to enable for this build. const plugins = [ + getNodeBuiltinPlugin(), getNativeModulesPlugin(nativeNodeModules), getDynamicImportsPlugin({ basePath, diff --git a/src/runtimes/node/bundlers/esbuild/plugin_node_builtin.ts b/src/runtimes/node/bundlers/esbuild/plugin_node_builtin.ts new file mode 100644 index 000000000..477b4e6b6 --- /dev/null +++ b/src/runtimes/node/bundlers/esbuild/plugin_node_builtin.ts @@ -0,0 +1,10 @@ +import type { Plugin } from '@netlify/esbuild' + +const getNodeBuiltinPlugin = (): Plugin => ({ + name: 'builtin-modules', + setup(build) { + build.onResolve({ filter: /^node:/ }, () => ({ external: true })) + }, +}) + +export { getNodeBuiltinPlugin } diff --git a/tests/fixtures/node-force-builtin-esm/function.js b/tests/fixtures/node-force-builtin-esm/function.js new file mode 100644 index 000000000..548ab4fb9 --- /dev/null +++ b/tests/fixtures/node-force-builtin-esm/function.js @@ -0,0 +1,5 @@ +import console from 'node:console' + +export const handler = () => { + console.log('hello world') +} diff --git a/tests/main.js b/tests/main.js index cfe82ae31..372e0482a 100644 --- a/tests/main.js +++ b/tests/main.js @@ -2251,7 +2251,7 @@ testMany( ) testMany( - 'Handles built-in modules imported with the `node:` prefix', + 'Handles built-in modules required with the `node:` prefix', ['bundler_default', 'bundler_default_nft', 'bundler_nft', 'bundler_esbuild', 'bundler_esbuild_zisi'], async (options, t) => { t.plan(3) @@ -2279,6 +2279,16 @@ testMany( }, ) +testMany( + 'Handles built-in modules imported with the `node:` prefix', + ['bundler_default', 'bundler_default_nft', 'bundler_nft', 'bundler_esbuild', 'bundler_esbuild_zisi'], + async (options, t) => { + await zipFixture(t, 'node-force-builtin-esm', { + opts: options, + }) + }, +) + testMany( 'Returns a `size` property with the size of each generated archive', ['bundler_default', 'bundler_esbuild', 'bundler_nft'],