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

fix: compile away node:-namespaced imports #806

Merged
merged 8 commits into from
Nov 12, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Plugin } from '@netlify/esbuild'
const getNodeBuiltinPlugin = (): Plugin => ({
name: 'builtin-modules',
setup(build) {
build.onResolve({ filter: /^node:/ }, () => ({ external: true }))
build.onResolve({ filter: /^node:/ }, (args) => ({ path: args.path.slice('node:'.length), external: true }))
},
})

Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/node-force-builtin-cjs/function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const console = require('node:console')

exports.handler = () => {
console.log('hello world')
}
20 changes: 17 additions & 3 deletions tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2251,11 +2251,11 @@ testMany(
)

testMany(
'Handles built-in modules required with the `node:` prefix',
'Handles built-in module added in v16 required with the `node:` prefix',
['bundler_default', 'bundler_default_nft', 'bundler_nft', 'bundler_esbuild', 'bundler_esbuild_zisi'],
async (options, t) => {
t.plan(3)
const { tmpDir, files } = await zipFixture(t, 'node-force-builtin', {
const { tmpDir, files } = await zipFixture(t, 'node-force-builtin-v16', {
opts: { config: { '*': { ...options } } },
})

Expand All @@ -2282,8 +2282,22 @@ 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, bundler) => {
const importSyntaxIsCompiledAway = bundler.includes('esbuild')
const zip = importSyntaxIsCompiledAway ? zipNode : zipFixture
await zip(t, 'node-force-builtin-esm', {
opts: options,
})
},
)

testMany(
'Handles built-in modules required 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', {
const nodePrefixIsUnderstood = semver.gte(nodeVersion, '14.18.0')
const zip = nodePrefixIsUnderstood ? zipNode : zipFixture
await zip(t, 'node-force-builtin-cjs', {
opts: options,
})
},
Expand Down