Skip to content

Commit

Permalink
feat: (require.main === module) support in bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
ChALkeR committed Nov 9, 2024
1 parent 9cfd1e1 commit 28b09d3
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions bundler/bundle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from 'node:assert/strict'
import { readFile } from 'node:fs/promises'
import { existsSync, readFileSync } from 'node:fs'
import { existsSync } from 'node:fs'
import { fileURLToPath, pathToFileURL } from 'node:url'
import { basename, dirname, extname, resolve, join } from 'node:path'
import { createRequire } from 'node:module'
Expand Down Expand Up @@ -174,7 +174,7 @@ export const build = async (...files) => {
if (file && /^[a-z0-9@_./-]+$/iu.test(file)) {
file = resolve(file)
if (!file.startsWith(`${cwd}/`)) continue
const data = readFileSync(file, 'base64')
const data = await readFile(file, 'base64')
if (fsFilesContents.has(file)) {
assert(fsFilesContents.get(file) === data)
} else {
Expand All @@ -187,6 +187,13 @@ export const build = async (...files) => {
return source
})

if (files.length === 1) {
const main = resolve(files[0])
loadPipeline.push((source, filepath) => {
return source.replaceAll('(require.main === module)', `(${filepath === main})`)
})
}

const hasBuffer = ['node', 'bun'].includes(options.platform)
const api = (f) => resolveRequire(`./modules/${f}`)
const nodeUnprefixed = {
Expand Down

0 comments on commit 28b09d3

Please sign in to comment.