Skip to content

Commit 89eb925

Browse files
committed
trying to fix resolve in jest
1 parent b395af9 commit 89eb925

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

packages/nuekit/src/builder.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import { promises as fs } from 'node:fs'
44
import { join } from 'node:path'
55

6+
import { resolve } from './util.js'
7+
68
// don't reuse saved builder when in test mode
79
const isTest = process.env.NODE_ENV == 'test'
810

@@ -12,7 +14,7 @@ export async function getJsBuilder(is_esbuild) {
1214
if (!isTest && jsBuilder) return jsBuilder
1315

1416
try {
15-
return jsBuilder = is_esbuild ? await import(import.meta.resolve('esbuild')) : Bun
17+
return jsBuilder = is_esbuild ? await import(resolve('esbuild', import.meta.url)) : Bun
1618
} catch {
1719
throw 'JS bundler not found. Please use Bun or install esbuild'
1820
}
@@ -23,7 +25,7 @@ export async function getCssBuilder(is_lcss) {
2325
if (!isTest && cssBuilder) return cssBuilder
2426

2527
try {
26-
cssBuilder = is_lcss ? await import(import.meta.resolve('lightningcss')) : Bun
28+
cssBuilder = is_lcss ? await import(resolve('lightningcss', import.meta.url)) : Bun
2729
if (!is_lcss) {
2830
const v = Bun.version.split('.').map(i => parseInt(i))
2931
if (!(v[0] >= 1 && v[1] >= 2)) throw new Error('Bun version too low')

packages/nuekit/src/init.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { fileURLToPath } from 'node:url'
55
import { compileFile as nueCompile } from 'nuejs-core'
66

77
import { buildJS } from './builder.js'
8-
import { version, colors, srcdir } from './util.js'
8+
import { resolve, version, colors, srcdir } from './util.js'
99

1010

1111
export async function initNueDir({ dist, is_dev, esbuild, force }) {
@@ -90,6 +90,6 @@ async function initDir({ dist, is_dev, esbuild, cwd, srcdir, outdir }) {
9090

9191
function resolvePath(npm_path) {
9292
const [npm_name, ...parts] = npm_path.split('/')
93-
const module_path = dirname(fileURLToPath(import.meta.resolve(npm_name)))
93+
const module_path = dirname(fileURLToPath(resolve(npm_name, import.meta.url)))
9494
return join(module_path, ...parts)
9595
}

packages/nuekit/src/util.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import { execSync } from 'node:child_process'
44
import { promises as fs } from 'node:fs'
5-
import { sep, parse, resolve, normalize, join, isAbsolute, dirname } from 'node:path'
5+
import { createRequire } from 'node:module'
6+
import { sep, parse, resolve as resolvePath, normalize, join, isAbsolute, dirname } from 'node:path'
67
import { fileURLToPath, pathToFileURL } from 'node:url'
78

89

@@ -13,9 +14,14 @@ export function openUrl(url) {
1314
execSync(`${open} ${url}`)
1415
}
1516

17+
export function resolve(mod, parent) {
18+
if (import.meta.resolve) return import.meta.resolve(mod, parent)
19+
return pathToFileURL(createRequire(parent).resolve(mod)).href
20+
}
21+
1622
export function esMain(meta) {
1723
if (!meta || !process.argv[1]) return false
18-
return fileURLToPath(meta.resolve(process.argv[1])) === fileURLToPath(meta.url)
24+
return fileURLToPath(resolve(process.argv[1], meta.url)) === fileURLToPath(meta.url)
1925
}
2026

2127
// read from package.json
@@ -26,7 +32,7 @@ export const version = await async function() {
2632
}()
2733

2834
export async function importFromCWD(path) {
29-
const abs_path = resolve(process.cwd(), path)
35+
const abs_path = resolvePath(process.cwd(), path)
3036
return import(pathToFileURL(abs_path).href)
3137
}
3238

0 commit comments

Comments
 (0)