Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

fix: esm imports with node: prefix on esbuild #802

Merged
merged 4 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/runtimes/node/bundlers/esbuild/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions src/runtimes/node/bundlers/esbuild/plugin_node_builtin.ts
Original file line number Diff line number Diff line change
@@ -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 }
5 changes: 5 additions & 0 deletions tests/fixtures/node-force-builtin-esm/function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import console from 'node:console'

export const handler = () => {
console.log('hello world')
}
12 changes: 11 additions & 1 deletion tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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'],
Expand Down