Skip to content

Commit

Permalink
fix: apply specific rules only to current test in bundler
Browse files Browse the repository at this point in the history
  • Loading branch information
ChALkeR committed Nov 9, 2024
1 parent 28b09d3 commit fdc2e8f
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions bundler/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const getPackageFiles = async (dir) => {
}

const loadCache = new Map()
const loadSourceFile = async (filepath) => {
const loadSourceFileBase = async (filepath) => {
if (!loadCache.has(filepath)) {
const load = async () => {
let contents = await readFile(filepath, 'utf8')
Expand All @@ -110,6 +110,13 @@ const loadSourceFile = async (filepath) => {
}

export const build = async (...files) => {
const specificLoadPipeline = []
const loadSourceFile = async (filepath) => {
let contents = await loadSourceFileBase(filepath)
for (const transform of specificLoadPipeline) contents = await transform(contents, filepath)
return contents
}

const input = []
const importSource = async (file) => input.push(await loadSourceFile(resolveRequire(file)))
const importFile = (...args) => input.push(`await import(${JSON.stringify(resolve(...args))});`)
Expand Down Expand Up @@ -166,7 +173,7 @@ export const build = async (...files) => {

const fsfiles = await getPackageFiles(filename ? dirname(resolve(filename)) : process.cwd())
const fsFilesContents = new Map()
loadPipeline.push(async (source) => {
specificLoadPipeline.push(async (source) => {
const cwd = process.cwd()
for (const re of [/readFileSync\('([^'\\]+)'[),]/gu, /readFileSync\("([^"\\]+)"[),]/gu]) {
for (const match of source.matchAll(re)) {
Expand All @@ -189,7 +196,7 @@ export const build = async (...files) => {

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

0 comments on commit fdc2e8f

Please sign in to comment.