@reticlehq/next 3.2.0 → 3.3.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 (3) hide show
  1. package/index.cjs +49 -3
  2. package/loader.cjs +9 -1
  3. package/package.json +2 -2
package/index.cjs CHANGED
@@ -12,6 +12,13 @@ const crypto = require('node:crypto');
12
12
  // Kept in sync with @reticlehq/core (ReticleDir / ReticleEnv). This package is plain CJS tooling and
13
13
  // deliberately has no ESM/TS dependency on core, so the two constants are mirrored here.
14
14
  const PAIRING_TOKEN_DIR_ENV = 'RETICLE_PAIRING_TOKEN_DIR';
15
+ /**
16
+ * Instrument anyway, whatever NODE_ENV says — for `NODE_ENV=production next dev`.
17
+ *
18
+ * Named here rather than imported: this package is plain CJS tooling that deliberately depends on
19
+ * nothing, which is why `RETICLE_PAIRING_TOKEN_DIR` above is a local constant too.
20
+ */
21
+ const DEV_OVERRIDE_ENV = 'RETICLE_DEV';
15
22
  const PAIRING_TOKEN_FILE = 'pairing-token';
16
23
  const RETICLE_CONFIG_FILE = '.reticle.json';
17
24
  const RETICLE_HOME_DIR = '.reticle';
@@ -200,10 +207,26 @@ function turbopackConfig(existing) {
200
207
  // type here makes Turbopack rewrite the module id (./x.tsx → ./x.tsx.tsx) and every import breaks.
201
208
  '*.tsx': rule,
202
209
  '*.jsx': rule,
210
+ // A JavaScript Next project's pages are .js (#1081). `.js` also names every file in
211
+ // node_modules, so the rule is limited to the project's own code; `condition` is Next 16+,
212
+ // and an older Turbopack that does not know the key is left without the rule rather than
213
+ // handed one it would reject.
214
+ ...(supportsRuleConditions() ? { '*.js': { ...rule, condition: { not: 'foreign' } } } : {}),
203
215
  },
204
216
  };
205
217
  }
206
218
 
219
+ /** Whether this Next's Turbopack accepts a rule `condition` (added with Next 16). */
220
+ function supportsRuleConditions() {
221
+ try {
222
+ const { version } = require(resolveFromApp('next/package.json'));
223
+ const major = parseInt(String(version).split('.')[0] ?? '', 10);
224
+ return !Number.isFinite(major) || major >= 16;
225
+ } catch {
226
+ return true;
227
+ }
228
+ }
229
+
207
230
  /**
208
231
  * The React kit the host app imports the SDK from. Deliberately NOT a dependency of this package —
209
232
  * it is the user's own install, probed for its version and nothing else. Declaring it here would
@@ -275,8 +298,30 @@ function sdkPackageVersion() {
275
298
  * @returns {import('next').NextConfig}
276
299
  */
277
300
  function withReticle(nextConfig = {}, options = {}) {
278
- // Production builds are untouched — this is a dev-time aid only.
279
- if (process.env.NODE_ENV === 'production') return nextConfig;
301
+ /*
302
+ * Production builds are untouched — this is a dev-time aid only.
303
+ *
304
+ * NODE_ENV is the gate because it is the only signal available where this runs. `withReticle`
305
+ * receives no `phase`, and Next evaluates the config inside `start-server.js`, a child process
306
+ * whose argv carries no `dev` — MEASURED, after a first attempt read argv and was wrong on a real
307
+ * `next dev`. The webpack hook below does get Next's own `dev` flag, but the pairing token is
308
+ * injected at CONFIG level, and baking one into a production bundle is the thing this gate exists
309
+ * to prevent.
310
+ *
311
+ * What was actually broken is that it happened in SILENCE. `NODE_ENV=production next dev` is a
312
+ * real configuration — people use it to reproduce production behaviour locally — and the result
313
+ * was an app that looked instrumented, started cleanly and never connected, with no reason to
314
+ * suspect an env var set for something else. So it says why, once, and names both the variable
315
+ * responsible and the way out (#1069).
316
+ */
317
+ if (process.env.NODE_ENV === 'production' && process.env[DEV_OVERRIDE_ENV] !== '1') {
318
+ console.log(
319
+ `[reticle] instrumentation is OFF because NODE_ENV=production. That is correct for a build. ` +
320
+ `If this is a dev server you want instrumented, set ${DEV_OVERRIDE_ENV}=1 (or do not export ` +
321
+ `NODE_ENV=production for it) — nothing else about your config needs to change.`,
322
+ );
323
+ return nextConfig;
324
+ }
280
325
 
281
326
  const userWebpack = nextConfig.webpack;
282
327
  const token = readPairingToken();
@@ -308,7 +353,8 @@ function withReticle(nextConfig = {}, options = {}) {
308
353
  config.module = config.module || { rules: [] };
309
354
  config.module.rules = config.module.rules || [];
310
355
  config.module.rules.push({
311
- test: /\.(t|j)sx$/,
356
+ // .js as well: see the Turbopack rule above (#1081).
357
+ test: /\.(tsx|jsx|js)$/,
312
358
  exclude: /node_modules/,
313
359
  enforce: 'pre',
314
360
  use: [{ loader: require.resolve('./loader.cjs') }],
package/loader.cjs CHANGED
@@ -10,7 +10,15 @@ module.exports = function reticleNextLoader(source, inputMap) {
10
10
  const callback = this.async();
11
11
  const filename = this.resourcePath;
12
12
 
13
- if (!/\.(t|j)sx$/.test(filename) || filename.includes('node_modules')) {
13
+ // `.js` too: create-next-app's JavaScript template writes every page as .js, and matching only
14
+ // .tsx/.jsx left those projects with no stamp anywhere (#1081). A .js file with no JSX in it is
15
+ // handed back untouched without being parsed, which is every config and route handler.
16
+ const isJs = /\.js$/.test(filename);
17
+ if (
18
+ !(/\.(t|j)sx$/.test(filename) || isJs) ||
19
+ filename.includes('node_modules') ||
20
+ (isJs && !/<[A-Za-z]/.test(source))
21
+ ) {
14
22
  callback(null, source, inputMap);
15
23
  return;
16
24
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reticlehq/next",
3
- "version": "3.2.0",
3
+ "version": "3.3.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": "3.2.0"
41
+ "@reticlehq/babel-plugin": "3.3.0"
42
42
  },
43
43
  "peerDependencies": {
44
44
  "next": ">=13"