Skip to content

Commit

Permalink
fix: no such file or directory with cwd option (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
chuyuandu authored Jul 29, 2024
1 parent 5071f8e commit d22004b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/MockLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export class MockLoader extends EventEmitter {
const { define, alias } = this.options
const { code, deps } = await transformWithEsbuild(
filepath,
{ isESM, define, alias },
{ isESM, define, alias, cwd: this.cwd },
)

try {
Expand Down
7 changes: 5 additions & 2 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs, { promises as fsp } from 'node:fs'
import { createRequire } from 'node:module'
import path from 'node:path'
import { pathToFileURL } from 'node:url'
import process from 'node:process'
import type { Metafile, Plugin } from 'esbuild'
import { build } from 'esbuild'
import JSON5 from 'json5'
Expand Down Expand Up @@ -93,6 +94,7 @@ export interface TransformWithEsbuildOptions {
isESM?: boolean
define: Record<string, string>
alias: ResolvedConfig['resolve']['alias']
cwd?: string
}

export type TransformWithEsbuildResult = Promise<{
Expand All @@ -104,7 +106,7 @@ export async function transformWithEsbuild(
entryPoint: string,
options: TransformWithEsbuildOptions,
): TransformWithEsbuildResult {
const { isESM = true, define, alias } = options
const { isESM = true, define, alias, cwd = process.cwd() } = options
try {
const result = await build({
entryPoints: [entryPoint],
Expand All @@ -117,6 +119,7 @@ export async function transformWithEsbuild(
format: isESM ? 'esm' : 'cjs',
define,
plugins: [aliasPlugin(alias), externalizeDeps, jsonLoader, json5Loader],
absWorkingDir: cwd,
})
return {
code: result.outputFiles[0].text,
Expand Down Expand Up @@ -147,6 +150,7 @@ export async function loadFromCode<T = any>({
isESM,
cwd,
}: LoadFromCodeOptions): Promise<{ [key: string]: T }> {
filepath = path.resolve(cwd, filepath)
if (isESM) {
const fileBase = `${filepath}.timestamp-${Date.now()}`
const fileNameTmp = `${fileBase}.mjs`
Expand All @@ -163,7 +167,6 @@ export async function loadFromCode<T = any>({
}
}
else {
filepath = path.resolve(cwd, filepath)
const extension = path.extname(filepath)
const realFileName = fs.realpathSync(filepath)
const loaderExt = extension in _require.extensions ? extension : '.js'
Expand Down

0 comments on commit d22004b

Please sign in to comment.