@reticlehq/next 2.3.0 → 2.5.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/index.cjs +118 -0
  2. package/package.json +2 -2
package/index.cjs CHANGED
@@ -32,6 +32,116 @@ function readPairingToken() {
32
32
  }
33
33
  }
34
34
 
35
+ /**
36
+ * The stamping loader, addressed by package export so both bundlers can resolve it. Turbopack takes
37
+ * loaders by module id, not by absolute path.
38
+ */
39
+ const LOADER_MODULE = '@reticlehq/next/loader';
40
+
41
+ /**
42
+ * The top-level `turbopack` config key is stable from Next 15.3; before that it was
43
+ * `experimental.turbo`, and an unknown top-level key makes Next print an "Invalid next.config.js
44
+ * options detected" warning on every boot. Older Next also defaults to webpack, so it does not need
45
+ * the key at all — emitting it would be pure noise in somebody's terminal.
46
+ * @returns {boolean}
47
+ */
48
+ function supportsTurbopackKey() {
49
+ try {
50
+ const { version } = require('next/package.json');
51
+ const [major, minor] = String(version)
52
+ .split('.')
53
+ .map((n) => parseInt(n, 10));
54
+ if (!Number.isFinite(major)) return true; // unreadable version: assume modern, matching npm's default
55
+ if (major > 15) return true;
56
+ return major === 15 && Number.isFinite(minor) && minor >= 3;
57
+ } catch {
58
+ return true;
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Turbopack rules mirroring the webpack pre-loader.
64
+ *
65
+ * Next 16 made Turbopack the DEFAULT, and a config carrying a `webpack` key with no `turbopack` key
66
+ * is a hard startup error there ("This build is using Turbopack, with a webpack config and no
67
+ * turbopack config") — so `withReticle` killed `next dev` outright for every new Next app. Emitting
68
+ * both keys means whichever bundler is running finds its own wiring and neither errors on the other.
69
+ * @param {Record<string, unknown> | undefined} existing
70
+ */
71
+ function turbopackConfig(existing) {
72
+ const rule = { loaders: [LOADER_MODULE] };
73
+ const prev = existing !== undefined && existing !== null ? existing : {};
74
+ const prevRules = prev.rules !== undefined && prev.rules !== null ? prev.rules : {};
75
+ return {
76
+ ...prev,
77
+ rules: {
78
+ ...prevRules,
79
+ // No `as:` — the loader only stamps attributes, so the module type is unchanged. Naming the
80
+ // type here makes Turbopack rewrite the module id (./x.tsx → ./x.tsx.tsx) and every import breaks.
81
+ '*.tsx': rule,
82
+ '*.jsx': rule,
83
+ },
84
+ };
85
+ }
86
+
87
+ /**
88
+ * The React kit the host app imports the SDK from. Deliberately NOT a dependency of this package —
89
+ * it is the user's own install, probed for its version and nothing else. Declaring it here would
90
+ * force the React adapter onto every Next user, including the ones who never import it.
91
+ */
92
+ const RETICLE_SDK_PACKAGE = '@reticlehq/react';
93
+
94
+ /** How far up from the resolved entry to look for the manifest beside it. */
95
+ const MANIFEST_SEARCH_DEPTH = 5;
96
+
97
+ /**
98
+ * Resolve from the APP, not from this file. `next.config.js` is loaded with cwd at the project root,
99
+ * where the user's `@reticlehq/react` always is. A bare `require` resolves relative to THIS package
100
+ * instead, and since the SDK is deliberately not a dependency here, that lookup fails outright under
101
+ * pnpm's strict node_modules layout — silently, into a `catch` that returns ''. Every pnpm Next user
102
+ * therefore reported no `sdkVersion`, which is the one value that turns a skewed pair into a named
103
+ * mismatch rather than a bare -32000.
104
+ * @param {string} specifier
105
+ * @returns {string}
106
+ */
107
+ function resolveFromApp(specifier) {
108
+ return require.resolve(specifier, { paths: [process.cwd()] });
109
+ }
110
+
111
+ /**
112
+ * The installed SDK's package version, for the HELLO's `sdkVersion`. Mirrors
113
+ * `@reticlehq/vite-plugin`'s `sdkPackageVersion` — this package is plain CJS tooling with no
114
+ * dependency on the TS packages, so the logic is duplicated rather than imported.
115
+ * @returns {string}
116
+ */
117
+ function sdkPackageVersion() {
118
+ // Preferred: the package exports its own manifest. Newer SDKs do.
119
+ try {
120
+ const { version } = require(resolveFromApp(`${RETICLE_SDK_PACKAGE}/package.json`));
121
+ if (typeof version === 'string') return version;
122
+ } catch {
123
+ // Falls through — see below.
124
+ }
125
+ // Fallback, and it is load-bearing rather than defensive: an OLDER SDK has no `./package.json` in
126
+ // its exports map, and an older SDK is precisely the skew this value exists to name.
127
+ try {
128
+ let dir = path.dirname(resolveFromApp(RETICLE_SDK_PACKAGE));
129
+ for (let up = 0; up < MANIFEST_SEARCH_DEPTH; up++) {
130
+ const candidate = path.join(dir, 'package.json');
131
+ if (fs.existsSync(candidate)) {
132
+ const { version } = JSON.parse(fs.readFileSync(candidate, 'utf8'));
133
+ if (typeof version === 'string') return version;
134
+ }
135
+ const parent = path.dirname(dir);
136
+ if (parent === dir) break;
137
+ dir = parent;
138
+ }
139
+ } catch {
140
+ // Unresolvable (not installed, exotic layout) — report nothing rather than guessing.
141
+ }
142
+ return '';
143
+ }
144
+
35
145
  /**
36
146
  * @param {import('next').NextConfig} [nextConfig]
37
147
  * @returns {import('next').NextConfig}
@@ -44,12 +154,20 @@ function withReticle(nextConfig = {}) {
44
154
  const token = readPairingToken();
45
155
  return {
46
156
  ...nextConfig,
157
+ ...(supportsTurbopackKey() ? { turbopack: turbopackConfig(nextConfig.turbopack) } : {}),
47
158
  // Expose the token to the client bundle as process.env.NEXT_PUBLIC_RETICLE_TOKEN (Next's convention
48
159
  // for client-readable env), so a dev-only client connect can present it. Omitted when the daemon
49
160
  // hasn't provisioned one yet — the client then connects without it and the page reloads once it has.
50
161
  env: {
51
162
  ...nextConfig.env,
52
163
  ...(token !== undefined ? { NEXT_PUBLIC_RETICLE_TOKEN: token } : {}),
164
+ // The project root, so the SDK can report React's absolute `_debugSource.fileName` as a
165
+ // repo-relative path. Passed via `env` rather than a webpack DefinePlugin because Turbopack — the
166
+ // Next 16 default — never runs the webpack branch, and a source pointer that only works on one
167
+ // of the two bundlers is worse than one that works on neither.
168
+ NEXT_PUBLIC_RETICLE_ROOT: process.cwd(),
169
+ // So a version-skewed pair can name itself instead of surfacing as a bare -32000.
170
+ NEXT_PUBLIC_RETICLE_SDK_VERSION: sdkPackageVersion(),
53
171
  },
54
172
  webpack(config, ctx) {
55
173
  config.module = config.module || { rules: [] };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reticlehq/next",
3
- "version": "2.3.0",
3
+ "version": "2.5.0",
4
4
  "description": "Next.js helper for Reticle: keeps SWC, adds source-file mapping via a dev-only webpack pre-loader (data-reticle-source).",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Reticle contributors",
@@ -38,7 +38,7 @@
38
38
  ],
39
39
  "dependencies": {
40
40
  "@babel/core": "^7.26.0",
41
- "@reticlehq/babel-plugin": "2.3.0"
41
+ "@reticlehq/babel-plugin": "2.5.0"
42
42
  },
43
43
  "peerDependencies": {
44
44
  "next": ">=13"