Skip to content

Commit

Permalink
trying to fix resolve in jest
Browse files Browse the repository at this point in the history
  • Loading branch information
nobkd committed Feb 18, 2025
1 parent b395af9 commit 89eb925
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
6 changes: 4 additions & 2 deletions packages/nuekit/src/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import { promises as fs } from 'node:fs'
import { join } from 'node:path'

import { resolve } from './util.js'

// don't reuse saved builder when in test mode
const isTest = process.env.NODE_ENV == 'test'

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

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

try {
cssBuilder = is_lcss ? await import(import.meta.resolve('lightningcss')) : Bun
cssBuilder = is_lcss ? await import(resolve('lightningcss', import.meta.url)) : Bun
if (!is_lcss) {
const v = Bun.version.split('.').map(i => parseInt(i))
if (!(v[0] >= 1 && v[1] >= 2)) throw new Error('Bun version too low')
Expand Down
4 changes: 2 additions & 2 deletions packages/nuekit/src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { fileURLToPath } from 'node:url'
import { compileFile as nueCompile } from 'nuejs-core'

import { buildJS } from './builder.js'
import { version, colors, srcdir } from './util.js'
import { resolve, version, colors, srcdir } from './util.js'


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

function resolvePath(npm_path) {
const [npm_name, ...parts] = npm_path.split('/')
const module_path = dirname(fileURLToPath(import.meta.resolve(npm_name)))
const module_path = dirname(fileURLToPath(resolve(npm_name, import.meta.url)))
return join(module_path, ...parts)
}
12 changes: 9 additions & 3 deletions packages/nuekit/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import { execSync } from 'node:child_process'
import { promises as fs } from 'node:fs'
import { sep, parse, resolve, normalize, join, isAbsolute, dirname } from 'node:path'
import { createRequire } from 'node:module'
import { sep, parse, resolve as resolvePath, normalize, join, isAbsolute, dirname } from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'


Expand All @@ -13,9 +14,14 @@ export function openUrl(url) {
execSync(`${open} ${url}`)
}

export function resolve(mod, parent) {
if (import.meta.resolve) return import.meta.resolve(mod, parent)
return pathToFileURL(createRequire(parent).resolve(mod)).href
}

export function esMain(meta) {
if (!meta || !process.argv[1]) return false
return fileURLToPath(meta.resolve(process.argv[1])) === fileURLToPath(meta.url)
return fileURLToPath(resolve(process.argv[1], meta.url)) === fileURLToPath(meta.url)
}

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

export async function importFromCWD(path) {
const abs_path = resolve(process.cwd(), path)
const abs_path = resolvePath(process.cwd(), path)
return import(pathToFileURL(abs_path).href)
}

Expand Down

0 comments on commit 89eb925

Please sign in to comment.