@jcoreio/toolchain-esnext 5.8.7 → 5.8.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcoreio/toolchain-esnext",
3
- "version": "5.8.7",
3
+ "version": "5.8.8",
4
4
  "description": "ESNext JS build toolchain",
5
5
  "repository": {
6
6
  "type": "git",
@@ -28,12 +28,13 @@
28
28
  "babel-register-esm": "^1.2.5",
29
29
  "dedent-js": "^1.0.1",
30
30
  "eslint": "^9.17.0",
31
+ "find-up": "^5.0.0",
31
32
  "fs-extra": "^10.0.0",
32
33
  "globals": "^16.0.0",
33
34
  "resolve": "^1.22.2",
34
35
  "resolve-bin": "^1.0.0",
35
36
  "semver": "^7.5.3",
36
- "@jcoreio/toolchain": "5.8.7"
37
+ "@jcoreio/toolchain": "5.8.8"
37
38
  },
38
39
  "toolchainManaged": {
39
40
  "dependencies": {
@@ -3,36 +3,73 @@ import { fileURLToPath, pathToFileURL } from 'url'
3
3
  import { resolve as baseResolve, load as wrappedLoad } from 'babel-register-esm'
4
4
  import { projectDir } from '@jcoreio/toolchain/util/findUps.cjs'
5
5
  import resolveAltType from './resolveAltType.cjs'
6
+ import findUp from 'find-up'
7
+ import fs from 'fs-extra'
6
8
 
7
9
  const projectDirPrefix = pathToFileURL(projectDir) + '/'
8
10
 
11
+ const packageJsonCache = new Map()
12
+ let packageJsonCacheTime = Date.now()
13
+
14
+ async function getPackageJson(cwd) {
15
+ const now = Date.now()
16
+ if (now - packageJsonCacheTime > 10000) {
17
+ packageJsonCache.clear()
18
+ packageJsonCacheTime = now
19
+ }
20
+ if (packageJsonCache.has(cwd)) return packageJsonCache.get(cwd)
21
+ const packageJsonFile = await findUp('package.json', { cwd })
22
+ const packageJson =
23
+ packageJsonFile ?
24
+ await fs.readJson(packageJsonFile).catch(() => undefined)
25
+ : undefined
26
+ packageJsonCache.set(cwd, packageJson)
27
+ return packageJson
28
+ }
29
+
9
30
  export async function resolve(specifier, context, nextResolve) {
10
- if (
11
- specifier.startsWith(projectDirPrefix) ||
12
- (specifier.startsWith('.') &&
13
- context.parentURL.startsWith('file:') &&
14
- !context.parentURL.includes('/node_modules/'))
15
- ) {
16
- const basedir =
17
- context.parentURL ?
18
- path.dirname(fileURLToPath(context.parentURL))
19
- : process.cwd()
20
- if (!specifier.startsWith('.')) {
21
- specifier = path.relative(basedir, fileURLToPath(specifier))
22
- if (!specifier.startsWith('.')) specifier = `./${specifier}`
23
- }
24
- const altTypeResolved = resolveAltType(specifier, basedir)
25
- if (altTypeResolved) {
26
- return {
27
- url: pathToFileURL(path.resolve(basedir, altTypeResolved)).toString(),
28
- shortCircuit: true,
29
- format:
30
- process.env.JCOREIO_TOOLCHAIN_CJS ? 'commonjs'
31
- : process.env.JCOREIO_TOOLCHAIN_ESM ? 'module'
32
- : context.format,
31
+ if (specifier.startsWith('file:') || specifier.startsWith('.')) {
32
+ const resolved = new URL(specifier, context.parentURL).toString()
33
+ if (
34
+ resolved.startsWith(projectDirPrefix) &&
35
+ !resolved.includes('/node_modules/')
36
+ ) {
37
+ const basedir =
38
+ context.parentURL ?
39
+ path.dirname(fileURLToPath(context.parentURL))
40
+ : process.cwd()
41
+ const file = fileURLToPath(resolved)
42
+ const altTypeResolved = resolveAltType(
43
+ path.relative(basedir, file),
44
+ basedir
45
+ )
46
+ if (altTypeResolved) {
47
+ return {
48
+ url: resolved,
49
+ shortCircuit: true,
50
+ format:
51
+ process.env.JCOREIO_TOOLCHAIN_CJS ? 'commonjs'
52
+ : process.env.JCOREIO_TOOLCHAIN_ESM ? 'module'
53
+ : context.format,
54
+ }
55
+ }
56
+ if (
57
+ /\.[jt]sx?$/.test(resolved) &&
58
+ !process.env.JCOREIO_TOOLCHAIN_CJS &&
59
+ !process.env.JCOREIO_TOOLCHAIN_ESM
60
+ ) {
61
+ const packageJson = await getPackageJson(path.dirname(file))
62
+ if (packageJson) {
63
+ return {
64
+ url: resolved,
65
+ shortCircuit: true,
66
+ format: packageJson.type || 'commonjs',
67
+ }
68
+ }
33
69
  }
34
70
  }
35
71
  }
72
+
36
73
  return await baseResolve(specifier, context, nextResolve)
37
74
  }
38
75