From 15c9c7466ed610a0371096b76d67ca25b050866a Mon Sep 17 00:00:00 2001 From: Nikita Skovoroda Date: Sun, 10 Nov 2024 00:47:45 +0400 Subject: [PATCH] feat: add process.cwd() in bundles --- bundler/bundle.js | 7 +++++-- bundler/modules/fs.cjs | 2 ++ bundler/modules/globals.cjs | 5 +++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/bundler/bundle.js b/bundler/bundle.js index 70c99c8..db83b33 100644 --- a/bundler/bundle.js +++ b/bundler/bundle.js @@ -170,9 +170,10 @@ export const build = async (...files) => { const cwd = process.cwd() for (const re of [/readFileSync\('([^'\\]+)'[),]/gu, /readFileSync\("([^"\\]+)"[),]/gu]) { for (const match of source.matchAll(re)) { - const file = match[1] + let file = match[1] if (file && /^[a-z0-9@_./-]+$/iu.test(file)) { - if (!resolve(file).startsWith(`${cwd}/`)) continue + file = resolve(file) + if (!file.startsWith(`${cwd}/`)) continue const data = readFileSync(file, 'base64') if (fsFilesContents.has(file)) { assert(fsFilesContents.get(file) === data) @@ -246,6 +247,8 @@ export const build = async (...files) => { 'process.type': 'undefined', 'process.version': stringify('v22.5.1'), // shouldn't depend on currently used Node.js version 'process.versions.node': stringify('22.5.1'), // see line above + 'process.cwd': 'EXODUS_TEST_PROCESS.cwd', + EXODUS_TEST_PROCESS_CWD: stringify(process.cwd()), EXODUS_TEST_FILES: stringify(files.map((f) => [dirname(f), basename(f)])), EXODUS_TEST_SNAPSHOTS: stringify(EXODUS_TEST_SNAPSHOTS), EXODUS_TEST_RECORDINGS: stringify(EXODUS_TEST_RECORDINGS), diff --git a/bundler/modules/fs.cjs b/bundler/modules/fs.cjs index 362937c..7a056d9 100644 --- a/bundler/modules/fs.cjs +++ b/bundler/modules/fs.cjs @@ -1,4 +1,5 @@ const constants = require('constants-browserify') +const { resolve } = require('path') const { F_OK, R_OK, W_OK, X_OK } = constants // promises, sync, callbacks @@ -97,6 +98,7 @@ const readFileSync = (file, options) => { } if (typeof file !== 'string') throw new Error('file argument should be string') + file = resolve(process.cwd(), file) if (fsFilesContents?.has(file)) { const data = Buffer.from(fsFilesContents.get(file), 'base64') if (encoding?.toLowerCase().replace('-', '') === 'utf8') return data.toString('utf8') diff --git a/bundler/modules/globals.cjs b/bundler/modules/globals.cjs index c5c7d44..9356ca2 100644 --- a/bundler/modules/globals.cjs +++ b/bundler/modules/globals.cjs @@ -70,6 +70,11 @@ if (typeof process === 'undefined') { }, 0) } }, + cwd: () => { + // eslint-disable-next-line no-undef + if (typeof EXODUS_TEST_PROCESS_CWD === 'string') return EXODUS_TEST_PROCESS_CWD + throw new Error('Can not determine cwd, no process available') + }, } globalThis.EXODUS_TEST_PROCESS = process