Skip to content

Commit

Permalink
fix: incorrect filepath resolution during dynamic import (#91)
Browse files Browse the repository at this point in the history
* Only URLs with a scheme in: file, data, and node are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'd:'

* chore: tweak

---------

Co-authored-by: pengzhanbo <volodymyr@foxmail.com>
  • Loading branch information
hlwen and pengzhanbo authored Aug 14, 2024
1 parent c37d2a6 commit be65433
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions plugin/src/core/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,16 @@ export async function loadFromCode<T = any>({
}: LoadFromCodeOptions): Promise<T | { [key: string]: T }> {
filepath = path.resolve(cwd, filepath)
const ext = isESM ? '.mjs' : '.cjs'
const file = `${filepath}.timestamp-${Date.now()}${ext}`
await fsp.writeFile(file, code, 'utf8')
const filepathTmp = `${filepath}.timestamp-${Date.now()}${ext}`
const file = pathToFileURL(filepathTmp).toString()
await fsp.writeFile(filepathTmp, code, 'utf8')
try {
const mod = await import(file)
return mod.default || mod
}
finally {
try {
fs.unlinkSync(file)
fs.unlinkSync(filepathTmp)
}
catch {}
}
Expand Down

0 comments on commit be65433

Please sign in to comment.