@wix/zero-config-implementation 1.58.0 → 1.59.0

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
@@ -4,7 +4,7 @@
4
4
  "registry": "https://registry.npmjs.org/",
5
5
  "access": "public"
6
6
  },
7
- "version": "1.58.0",
7
+ "version": "1.59.0",
8
8
  "description": "Core library for extracting component manifests from JS and CSS files",
9
9
  "type": "module",
10
10
  "main": "dist/index.js",
@@ -85,5 +85,5 @@
85
85
  ]
86
86
  }
87
87
  },
88
- "falconPackageHash": "f7fcdc7c7a9bd8348098a4e3301700ca40fb26281cd6352c13146b45"
88
+ "falconPackageHash": "3762cf48efbed240f0744fc678539dd9e70ea2021dc6171c70fb714b"
89
89
  }
@@ -4,6 +4,7 @@ import { ResultAsync, errAsync } from 'neverthrow'
4
4
  import { parseNative } from 'tsconfck'
5
5
  import ts from 'typescript'
6
6
  import { NotFoundError, ParseError } from './errors'
7
+ import { createCompilerHostWithBundledLibs } from './ts-lib-host'
7
8
 
8
9
  /**
9
10
  * Compile a TypeScript file into a ts.Program.
@@ -30,5 +31,8 @@ export function compileTsFile(
30
31
  cause: error as Error,
31
32
  props: { phase: 'compile' },
32
33
  }),
33
- ).map(({ result }) => ts.createProgram([filePath], result.options))
34
+ ).map(({ result }) => {
35
+ const compilerHost = createCompilerHostWithBundledLibs(result.options)
36
+ return ts.createProgram([filePath], result.options, compilerHost)
37
+ })
34
38
  }
@@ -0,0 +1,44 @@
1
+ import { createRequire } from 'node:module'
2
+ import path from 'node:path'
3
+ import ts from 'typescript'
4
+
5
+ /**
6
+ * Creates a `ts.CompilerHost` that resolves TypeScript's `lib.*.d.ts` files
7
+ * from the host project's installed `typescript` package, rather than from
8
+ * the (potentially bundled) `typescript` we imported.
9
+ *
10
+ * Why: consumers that bundle this package (esbuild/tsup) inline `typescript`'s
11
+ * JS but cannot reach its on-disk `lib.*.d.ts` files at runtime. Without this
12
+ * override, `ts.getDefaultLibFilePath` returns a non-existent path and global
13
+ * symbols like `Array` and `Promise` go missing, silently collapsing prop types
14
+ * to `any`. `typescript` is declared as a peer dependency, so we can rely on
15
+ * the host project having an intact on-disk install reachable from its cwd.
16
+ */
17
+ export function createCompilerHostWithBundledLibs(options: ts.CompilerOptions): ts.CompilerHost {
18
+ const compilerHost = ts.createCompilerHost(options, true)
19
+ const hostLibDirectory = resolveHostTypeScriptLibDirectory()
20
+ if (!hostLibDirectory) return compilerHost
21
+
22
+ compilerHost.getDefaultLibLocation = () => hostLibDirectory
23
+ compilerHost.getDefaultLibFileName = (compilerOptions) =>
24
+ path.join(hostLibDirectory, ts.getDefaultLibFileName(compilerOptions))
25
+
26
+ return compilerHost
27
+ }
28
+
29
+ /**
30
+ * Locates the `lib/` directory of the host project's installed `typescript`
31
+ * package, walking up from the current working directory. Returns `undefined`
32
+ * if `typescript` cannot be resolved — in which case the default host's own
33
+ * lib location is used (correct for non-bundled callers, broken for bundled
34
+ * callers without a peer install).
35
+ */
36
+ function resolveHostTypeScriptLibDirectory(): string | undefined {
37
+ try {
38
+ const requireFromCwd = createRequire(path.join(process.cwd(), 'package.json'))
39
+ const typescriptEntryPath = requireFromCwd.resolve('typescript/lib/typescript.js')
40
+ return path.dirname(typescriptEntryPath)
41
+ } catch {
42
+ return undefined
43
+ }
44
+ }