Skip to content

Commit

Permalink
fix: handle non node: prefixed Node.js builtins when no npm specifier…
Browse files Browse the repository at this point in the history
…s were found
  • Loading branch information
pieh committed Jul 29, 2024
1 parent 6907065 commit 22ace7f
Showing 1 changed file with 44 additions and 41 deletions.
85 changes: 44 additions & 41 deletions packages/edge-bundler/node/npm_dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,6 @@ export const vendorNPMSpecifiers = async ({
rootPath,
})

// If we found no specifiers, there's nothing left to do here.
if (Object.keys(npmSpecifiers).length === 0) {
return
}

// To bundle an entire module and all its dependencies, create a entrypoint file
// where we re-export everything from that specifier. We do this for every
// specifier, and each of these files will become entry points to esbuild.
Expand All @@ -257,42 +252,50 @@ export const vendorNPMSpecifiers = async ({
return { filePath, specifier, types }
}),
)
const entryPoints = ops.map(({ filePath }) => filePath)
// Bundle each of the entrypoints we created. We'll end up with a compiled
// version of each, plus any chunks of shared code
// between them (such that a common module isn't bundled twice).
const { outputFiles } = await build({
allowOverwrite: true,
banner,
bundle: true,
entryPoints,
format: 'esm',
mainFields: ['module', 'browser', 'main'],
logLevel: 'error',
nodePaths,
outdir: temporaryDirectory.path,
platform: 'node',
splitting: true,
target: 'es2020',
write: false,
define:
environment === 'production'
? {
'process.env.NODE_ENV': '"production"',
}
: undefined,
})

await Promise.all(
outputFiles.map(async (file) => {
const types = ops.find((op) => path.basename(file.path) === path.basename(op.filePath))?.types
let content = file.text
if (types) {
content = `/// <reference types="${path.relative(path.dirname(file.path), types)}" />\n${content}`
}
await fs.writeFile(file.path, content)
}),
)
const outputFiles: string[] = []

// If we found specifiers, we need to
if (ops.length !== 0) {
const entryPoints = ops.map(({ filePath }) => filePath)
// Bundle each of the entrypoints we created. We'll end up with a compiled
// version of each, plus any chunks of shared code
// between them (such that a common module isn't bundled twice).
const { outputFiles: outputFilesFromEsBuild } = await build({
allowOverwrite: true,
banner,
bundle: true,
entryPoints,
format: 'esm',
mainFields: ['module', 'browser', 'main'],
logLevel: 'error',
nodePaths,
outdir: temporaryDirectory.path,
platform: 'node',
splitting: true,
target: 'es2020',
write: false,
define:
environment === 'production'
? {
'process.env.NODE_ENV': '"production"',
}
: undefined,
})

outputFiles.push(...outputFilesFromEsBuild.map((file) => file.path))

await Promise.all(
outputFilesFromEsBuild.map(async (file) => {
const types = ops.find((op) => path.basename(file.path) === path.basename(op.filePath))?.types
let content = file.text
if (types) {
content = `/// <reference types="${path.relative(path.dirname(file.path), types)}" />\n${content}`
}
await fs.writeFile(file.path, content)
}),
)
}

// Add all Node.js built-ins to the import map, so any unprefixed specifiers
// (e.g. `process`) resolve to the prefixed versions (e.g. `node:prefix`),
Expand Down Expand Up @@ -340,6 +343,6 @@ export const vendorNPMSpecifiers = async ({
directory: temporaryDirectory.path,
importMap: newImportMap,
npmSpecifiersWithExtraneousFiles,
outputFiles: outputFiles.map((file) => file.path),
outputFiles,
}
}

0 comments on commit 22ace7f

Please sign in to comment.