Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: no such file or directory with cwd option #88

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading