@jcoreio/toolchain-esnext 5.8.7 → 5.8.9

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.9",
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.9"
37
38
  },
38
39
  "toolchainManaged": {
39
40
  "dependencies": {
@@ -3,36 +3,72 @@ 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
+ let relativeSpec = path.relative(basedir, file)
43
+ if (!relativeSpec.startsWith('.')) relativeSpec = `./${relativeSpec}`
44
+ const altTypeResolved = resolveAltType(relativeSpec, basedir)
45
+ if (altTypeResolved) {
46
+ return {
47
+ url: pathToFileURL(path.resolve(basedir, altTypeResolved)).toString(),
48
+ shortCircuit: true,
49
+ format:
50
+ process.env.JCOREIO_TOOLCHAIN_CJS ? 'commonjs'
51
+ : process.env.JCOREIO_TOOLCHAIN_ESM ? 'module'
52
+ : context.format,
53
+ }
54
+ }
55
+ if (
56
+ /\.[jt]sx?$/.test(resolved) &&
57
+ !process.env.JCOREIO_TOOLCHAIN_CJS &&
58
+ !process.env.JCOREIO_TOOLCHAIN_ESM
59
+ ) {
60
+ const packageJson = await getPackageJson(path.dirname(file))
61
+ if (packageJson) {
62
+ return {
63
+ url: resolved,
64
+ shortCircuit: true,
65
+ format: packageJson.type || 'commonjs',
66
+ }
67
+ }
33
68
  }
34
69
  }
35
70
  }
71
+
36
72
  return await baseResolve(specifier, context, nextResolve)
37
73
  }
38
74