From 199f6ed228e8648712d100fc150771f762535c41 Mon Sep 17 00:00:00 2001 From: Netlify Team Account 1 <90322326+netlify-team-account-1@users.noreply.github.com> Date: Thu, 11 Nov 2021 12:09:06 +0100 Subject: [PATCH] fix: esm imports with node: prefix on esbuild (#802) * chore: add reproduction for esbuild and node-prefixed imports * fix: mark node:-prefixed modules as external for esbuild * chore: move builtin-logic into its own plugin * chore: run nodebuiltin plugin first Co-authored-by: Netlify Team Account 1 --- src/runtimes/node/bundlers/esbuild/bundler.ts | 2 ++ .../node/bundlers/esbuild/plugin_node_builtin.ts | 10 ++++++++++ tests/fixtures/node-force-builtin-esm/function.js | 5 +++++ tests/main.js | 12 +++++++++++- 4 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 src/runtimes/node/bundlers/esbuild/plugin_node_builtin.ts create mode 100644 tests/fixtures/node-force-builtin-esm/function.js 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'],