@opendatacapture/instrument-bundler 2.1.3 → 2.2.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.
Files changed (2) hide show
  1. package/package.json +8 -6
  2. package/src/build.ts +29 -1
package/package.json CHANGED
@@ -1,7 +1,10 @@
1
1
  {
2
2
  "name": "@opendatacapture/instrument-bundler",
3
3
  "type": "module",
4
- "version": "2.1.3",
4
+ "version": "2.2.0",
5
+ "sideEffects": [
6
+ "**/cli.ts"
7
+ ],
5
8
  "license": "Apache-2.0",
6
9
  "publishConfig": {
7
10
  "access": "public"
@@ -32,15 +35,14 @@
32
35
  "glob": "^11.1.0",
33
36
  "slashes": "^3.0.12",
34
37
  "@opendatacapture/runtime-internal": "0.0.0",
35
- "ts-pattern": "npm:ts-pattern__5.x@5.3.1",
36
38
  "type-fest": "npm:type-fest__4.x@4.23.0",
37
- "zod": "npm:zod__3.x@3.25.36"
39
+ "zod": "npm:zod__3.x@3.25.36",
40
+ "ts-pattern": "npm:ts-pattern__5.x@5.3.1"
38
41
  },
39
42
  "devDependencies": {
40
- "@opendatacapture/runtime-v1": "2.1.3",
41
- "react": "npm:react__19.x@19.1.0",
43
+ "@opendatacapture/runtime-v1": "2.2.0",
42
44
  "react-dom": "npm:react-dom__19.x@19.1.0",
43
- "@opendatacapture/instrument-stubs": "0.0.0"
45
+ "react": "npm:react__19.x@19.1.0"
44
46
  },
45
47
  "scripts": {
46
48
  "format": "prettier --write src",
package/src/build.ts CHANGED
@@ -11,6 +11,32 @@ import type { BundlerInput } from './schemas.js';
11
11
  import type { BuildOutput } from './types.js';
12
12
  import type { BuildResult } from './vendor/esbuild.js';
13
13
 
14
+ const DEFAULT_REACT_PACKAGE = 'react@19.x';
15
+
16
+ const REACT_PACKAGE_PATTERN = /['"]\/runtime\/v1\/(react@[^'"/]+)/g;
17
+
18
+ /**
19
+ * JSX compiles to the JSX runtime of one react, and elements of one major are not renderable by
20
+ * another, so the runtime has to be the react the source itself imports rather than a fixed version.
21
+ */
22
+ function resolveJsxImportSource(inputs: BundlerInput[]): string {
23
+ const packages = new Set<string>();
24
+ for (const { content } of inputs) {
25
+ if (typeof content !== 'string') {
26
+ continue;
27
+ }
28
+ for (const [, name] of content.matchAll(REACT_PACKAGE_PATTERN)) {
29
+ packages.add(name!);
30
+ }
31
+ }
32
+ if (packages.size > 1) {
33
+ throw new InstrumentBundlerError(
34
+ `Cannot resolve the JSX runtime: expected at most one version of react, found ${[...packages].join(', ')}`
35
+ );
36
+ }
37
+ return `/runtime/v1/${packages.values().next().value ?? DEFAULT_REACT_PACKAGE}`;
38
+ }
39
+
14
40
  function parseBuildResult(result: BuildResult): BuildOutput {
15
41
  const cssOutput = result.outputFiles?.find((output) => output.path.endsWith('bundle.css'));
16
42
  const jsOutput = result.outputFiles?.find((output) => output.path.endsWith('bundle.js'));
@@ -41,6 +67,8 @@ export async function build({
41
67
  logLevel?: LogLevel;
42
68
  }): Promise<BuildOutput> {
43
69
  const index = resolveIndexInput(inputs);
70
+ // outside the try, whose catch rewrites anything it sees into an esbuild failure
71
+ const jsxImportSource = resolveJsxImportSource(inputs);
44
72
  let result: BuildResult;
45
73
  try {
46
74
  result = await esbuild.build({
@@ -48,7 +76,7 @@ export async function build({
48
76
  charset: 'ascii',
49
77
  format: 'esm',
50
78
  jsx: 'automatic',
51
- jsxImportSource: '/runtime/v1/react@19.x',
79
+ jsxImportSource,
52
80
  keepNames: true,
53
81
  logLevel,
54
82
  metafile: true,