tempo-sdk 0.0.4 → 0.0.5

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/dist/nextjs.d.cts DELETED
@@ -1,108 +0,0 @@
1
- import { ElementInfo, PropValue } from '@modules/annotation';
2
- export { ElementInfo, PropValue } from '@modules/annotation';
3
-
4
- /**
5
- * Next.js plugin that registers the tempo-annotate loader for Webpack and
6
- * Turbopack. Mirrors the user-facing pattern of `@next/mdx`: a higher-order
7
- * function that takes plugin options, returns a function that takes the user's
8
- * `next.config`, and returns a new config with the plugin's loader rules
9
- * merged in.
10
- *
11
- * See SPEC_NEXTJS.md (in this directory) for the full design rationale.
12
- *
13
- * @example
14
- * ```js
15
- * // next.config.js
16
- * const tempoNextjsPlugin = require("tempo-sdk/nextjs");
17
- * module.exports = tempoNextjsPlugin()({ reactStrictMode: true });
18
- * ```
19
- */
20
-
21
- type ComputeAnnotatedProps = (info: ElementInfo, filepath: string, modifiedtsMs: number) => Record<string, PropValue>;
22
- interface TempoNextjsOptions {
23
- /** Path to a JS/TS module that default-exports a `ComputeAnnotatedProps`
24
- * function. Resolved relative to the project root (CWD where the user
25
- * invokes `next dev` / `next build`). Turbopack does not allow function-
26
- * valued loader options, so this must be a path. */
27
- computeAnnotatedPropsModule?: string;
28
- /** File patterns to include. Default: `["**\/*.tsx", "**\/*.jsx"]`. */
29
- include?: string | RegExp | Array<string | RegExp>;
30
- /** File patterns to exclude. Default: `["**\/node_modules\/**"]`. */
31
- exclude?: string | RegExp | Array<string | RegExp>;
32
- /** Auto-inject `<meta name="tempo-project-root">` into JSX `<head>`.
33
- * Default: `true`. */
34
- autoInjectRootMeta?: boolean;
35
- /** Run the loader during production builds too. Default: `false`. */
36
- productionBuild?: boolean;
37
- }
38
- interface TurbopackRule {
39
- condition?: unknown;
40
- loaders?: Array<{
41
- loader: string;
42
- options?: unknown;
43
- } | string>;
44
- }
45
- interface TurbopackConfig {
46
- rules?: Record<string, TurbopackRule | TurbopackRule[]>;
47
- [key: string]: unknown;
48
- }
49
- interface NextConfigLike {
50
- webpack?: ((config: unknown, context: {
51
- dev: boolean;
52
- [k: string]: unknown;
53
- }) => unknown) | null;
54
- turbopack?: TurbopackConfig;
55
- experimental?: {
56
- turbo?: TurbopackConfig;
57
- [k: string]: unknown;
58
- };
59
- [key: string]: unknown;
60
- }
61
- declare function resolveLoaderPath(): string;
62
- interface NextVersionInfo {
63
- major: number;
64
- minor: number;
65
- }
66
- declare function detectNextVersion(): NextVersionInfo | null;
67
- /** Should the wrapper write to `experimental.turbo` instead of `turbopack`? */
68
- declare function useExperimentalTurbo(version: NextVersionInfo | null): boolean;
69
- /** Should the wrapper omit Turbopack registration entirely? Some pre-13 versions
70
- * error on unknown config keys. */
71
- declare function shouldOmitTurbopack(version: NextVersionInfo | null): boolean;
72
- declare function buildTurbopackRule(loaderPath: string, options: TempoNextjsOptions): TurbopackRule;
73
- declare function mergeTurbopackRules(existing: TurbopackConfig | undefined, newRule: TurbopackRule): TurbopackConfig;
74
- declare function buildWebpackHook(loaderPath: string, options: TempoNextjsOptions, userWebpack: NextConfigLike["webpack"]): NonNullable<NextConfigLike["webpack"]>;
75
- /**
76
- * Returns the canonical project root that the Tempo loader uses to compute
77
- * `data-tempo-filepath` values. Use this in `pages/_document.tsx` to set the
78
- * content of `<meta name="tempo-project-root">`, ensuring the manual meta tag
79
- * value matches what the loader produces.
80
- */
81
- declare function tempoProjectRoot(): string;
82
- /** Reset the cached project root. For tests only. */
83
- declare function _resetTempoProjectRoot(): void;
84
- /**
85
- * Higher-order function that wraps a Next.js config to register the
86
- * tempo-annotate loader with both Webpack and Turbopack. No-op when
87
- * `process.env.TEMPO` is not set.
88
- *
89
- * @example
90
- * ```js
91
- * // next.config.js
92
- * const tempoNextjsPlugin = require("tempo-sdk/nextjs");
93
- * module.exports = tempoNextjsPlugin()({ reactStrictMode: true });
94
- * ```
95
- */
96
- declare function tempoNextjsPlugin(pluginOptions?: TempoNextjsOptions): (inputConfig?: NextConfigLike) => NextConfigLike;
97
-
98
- declare const _internal: {
99
- buildTurbopackRule: typeof buildTurbopackRule;
100
- mergeTurbopackRules: typeof mergeTurbopackRules;
101
- buildWebpackHook: typeof buildWebpackHook;
102
- detectNextVersion: typeof detectNextVersion;
103
- useExperimentalTurbo: typeof useExperimentalTurbo;
104
- shouldOmitTurbopack: typeof shouldOmitTurbopack;
105
- resolveLoaderPath: typeof resolveLoaderPath;
106
- };
107
-
108
- export { type ComputeAnnotatedProps, type TempoNextjsOptions, _internal, _resetTempoProjectRoot, tempoNextjsPlugin as default, tempoNextjsPlugin, tempoProjectRoot };
package/dist/nextjs.d.ts DELETED
@@ -1,108 +0,0 @@
1
- import { ElementInfo, PropValue } from '@modules/annotation';
2
- export { ElementInfo, PropValue } from '@modules/annotation';
3
-
4
- /**
5
- * Next.js plugin that registers the tempo-annotate loader for Webpack and
6
- * Turbopack. Mirrors the user-facing pattern of `@next/mdx`: a higher-order
7
- * function that takes plugin options, returns a function that takes the user's
8
- * `next.config`, and returns a new config with the plugin's loader rules
9
- * merged in.
10
- *
11
- * See SPEC_NEXTJS.md (in this directory) for the full design rationale.
12
- *
13
- * @example
14
- * ```js
15
- * // next.config.js
16
- * const tempoNextjsPlugin = require("tempo-sdk/nextjs");
17
- * module.exports = tempoNextjsPlugin()({ reactStrictMode: true });
18
- * ```
19
- */
20
-
21
- type ComputeAnnotatedProps = (info: ElementInfo, filepath: string, modifiedtsMs: number) => Record<string, PropValue>;
22
- interface TempoNextjsOptions {
23
- /** Path to a JS/TS module that default-exports a `ComputeAnnotatedProps`
24
- * function. Resolved relative to the project root (CWD where the user
25
- * invokes `next dev` / `next build`). Turbopack does not allow function-
26
- * valued loader options, so this must be a path. */
27
- computeAnnotatedPropsModule?: string;
28
- /** File patterns to include. Default: `["**\/*.tsx", "**\/*.jsx"]`. */
29
- include?: string | RegExp | Array<string | RegExp>;
30
- /** File patterns to exclude. Default: `["**\/node_modules\/**"]`. */
31
- exclude?: string | RegExp | Array<string | RegExp>;
32
- /** Auto-inject `<meta name="tempo-project-root">` into JSX `<head>`.
33
- * Default: `true`. */
34
- autoInjectRootMeta?: boolean;
35
- /** Run the loader during production builds too. Default: `false`. */
36
- productionBuild?: boolean;
37
- }
38
- interface TurbopackRule {
39
- condition?: unknown;
40
- loaders?: Array<{
41
- loader: string;
42
- options?: unknown;
43
- } | string>;
44
- }
45
- interface TurbopackConfig {
46
- rules?: Record<string, TurbopackRule | TurbopackRule[]>;
47
- [key: string]: unknown;
48
- }
49
- interface NextConfigLike {
50
- webpack?: ((config: unknown, context: {
51
- dev: boolean;
52
- [k: string]: unknown;
53
- }) => unknown) | null;
54
- turbopack?: TurbopackConfig;
55
- experimental?: {
56
- turbo?: TurbopackConfig;
57
- [k: string]: unknown;
58
- };
59
- [key: string]: unknown;
60
- }
61
- declare function resolveLoaderPath(): string;
62
- interface NextVersionInfo {
63
- major: number;
64
- minor: number;
65
- }
66
- declare function detectNextVersion(): NextVersionInfo | null;
67
- /** Should the wrapper write to `experimental.turbo` instead of `turbopack`? */
68
- declare function useExperimentalTurbo(version: NextVersionInfo | null): boolean;
69
- /** Should the wrapper omit Turbopack registration entirely? Some pre-13 versions
70
- * error on unknown config keys. */
71
- declare function shouldOmitTurbopack(version: NextVersionInfo | null): boolean;
72
- declare function buildTurbopackRule(loaderPath: string, options: TempoNextjsOptions): TurbopackRule;
73
- declare function mergeTurbopackRules(existing: TurbopackConfig | undefined, newRule: TurbopackRule): TurbopackConfig;
74
- declare function buildWebpackHook(loaderPath: string, options: TempoNextjsOptions, userWebpack: NextConfigLike["webpack"]): NonNullable<NextConfigLike["webpack"]>;
75
- /**
76
- * Returns the canonical project root that the Tempo loader uses to compute
77
- * `data-tempo-filepath` values. Use this in `pages/_document.tsx` to set the
78
- * content of `<meta name="tempo-project-root">`, ensuring the manual meta tag
79
- * value matches what the loader produces.
80
- */
81
- declare function tempoProjectRoot(): string;
82
- /** Reset the cached project root. For tests only. */
83
- declare function _resetTempoProjectRoot(): void;
84
- /**
85
- * Higher-order function that wraps a Next.js config to register the
86
- * tempo-annotate loader with both Webpack and Turbopack. No-op when
87
- * `process.env.TEMPO` is not set.
88
- *
89
- * @example
90
- * ```js
91
- * // next.config.js
92
- * const tempoNextjsPlugin = require("tempo-sdk/nextjs");
93
- * module.exports = tempoNextjsPlugin()({ reactStrictMode: true });
94
- * ```
95
- */
96
- declare function tempoNextjsPlugin(pluginOptions?: TempoNextjsOptions): (inputConfig?: NextConfigLike) => NextConfigLike;
97
-
98
- declare const _internal: {
99
- buildTurbopackRule: typeof buildTurbopackRule;
100
- mergeTurbopackRules: typeof mergeTurbopackRules;
101
- buildWebpackHook: typeof buildWebpackHook;
102
- detectNextVersion: typeof detectNextVersion;
103
- useExperimentalTurbo: typeof useExperimentalTurbo;
104
- shouldOmitTurbopack: typeof shouldOmitTurbopack;
105
- resolveLoaderPath: typeof resolveLoaderPath;
106
- };
107
-
108
- export { type ComputeAnnotatedProps, type TempoNextjsOptions, _internal, _resetTempoProjectRoot, tempoNextjsPlugin as default, tempoNextjsPlugin, tempoProjectRoot };
package/dist/nextjs.js DELETED
@@ -1,174 +0,0 @@
1
- // nextjs.ts
2
- import { existsSync, realpathSync } from "fs";
3
- import { createRequire } from "module";
4
- import { dirname, resolve } from "path";
5
- var TURBOPACK_RULE_KEY = "*.{tsx,jsx}";
6
- var requireFromHere = createRequire(
7
- // In ESM the source compiles using import.meta.url; tsup rewrites this for
8
- // CJS output to use __filename automatically.
9
- typeof __filename !== "undefined" ? __filename : import.meta.url
10
- );
11
- function resolveLoaderPath() {
12
- for (const candidate of ["./nextjs-loader.cjs", "./dist/nextjs-loader.cjs"]) {
13
- try {
14
- return requireFromHere.resolve(candidate);
15
- } catch {
16
- }
17
- }
18
- throw new Error(
19
- "tempo-sdk/nextjs: could not resolve nextjs-loader.cjs. Run `pnpm build` in the tempo-sdk package to produce the dist/ output."
20
- );
21
- }
22
- function detectNextVersion() {
23
- try {
24
- const pkg = requireFromHere("next/package.json");
25
- const m = /^(\d+)\.(\d+)/.exec(pkg.version);
26
- if (!m) return null;
27
- return { major: Number(m[1]), minor: Number(m[2]) };
28
- } catch {
29
- return null;
30
- }
31
- }
32
- function useExperimentalTurbo(version) {
33
- if (!version) return false;
34
- if (version.major < 15) return true;
35
- if (version.major === 15 && version.minor < 3) return true;
36
- return false;
37
- }
38
- function shouldOmitTurbopack(version) {
39
- if (!version) return false;
40
- return version.major < 13;
41
- }
42
- function buildTurbopackRule(loaderPath, options) {
43
- const loaderOptions = {};
44
- if (options.computeAnnotatedPropsModule !== void 0) {
45
- loaderOptions.computeAnnotatedPropsModule = options.computeAnnotatedPropsModule;
46
- }
47
- if (options.autoInjectRootMeta !== void 0) {
48
- loaderOptions.autoInjectRootMeta = options.autoInjectRootMeta;
49
- }
50
- const condition = options.productionBuild ? { not: "foreign" } : { all: [{ not: "foreign" }, "development"] };
51
- return {
52
- condition,
53
- loaders: [{ loader: loaderPath, options: loaderOptions }]
54
- };
55
- }
56
- function mergeTurbopackRules(existing, newRule) {
57
- const existingRule = existing?.rules?.[TURBOPACK_RULE_KEY];
58
- let mergedRule;
59
- if (!existingRule) {
60
- mergedRule = newRule;
61
- } else if (Array.isArray(existingRule)) {
62
- mergedRule = [...existingRule, newRule];
63
- } else {
64
- mergedRule = [existingRule, newRule];
65
- }
66
- return {
67
- ...existing,
68
- rules: {
69
- ...existing?.rules,
70
- [TURBOPACK_RULE_KEY]: mergedRule
71
- }
72
- };
73
- }
74
- function buildWebpackHook(loaderPath, options, userWebpack) {
75
- const loaderOptions = {};
76
- if (options.computeAnnotatedPropsModule !== void 0) {
77
- loaderOptions.computeAnnotatedPropsModule = options.computeAnnotatedPropsModule;
78
- }
79
- if (options.autoInjectRootMeta !== void 0) {
80
- loaderOptions.autoInjectRootMeta = options.autoInjectRootMeta;
81
- }
82
- const allowProduction = options.productionBuild === true;
83
- return (configIn, context) => {
84
- var _a;
85
- const config = configIn;
86
- const ctx = context;
87
- if (ctx.dev || allowProduction) {
88
- config.module ?? (config.module = {});
89
- (_a = config.module).rules ?? (_a.rules = []);
90
- config.module.rules.push({
91
- test: /\.(tsx|jsx)$/,
92
- exclude: /node_modules/,
93
- enforce: "pre",
94
- use: [{ loader: loaderPath, options: loaderOptions }]
95
- });
96
- }
97
- if (typeof userWebpack === "function") {
98
- return userWebpack(config, context);
99
- }
100
- return config;
101
- };
102
- }
103
- var cachedTempoProjectRoot = null;
104
- function tempoProjectRoot() {
105
- if (cachedTempoProjectRoot !== null) return cachedTempoProjectRoot;
106
- let dir = process.cwd();
107
- while (true) {
108
- if (existsSync(resolve(dir, "package.json"))) break;
109
- const parent = dirname(dir);
110
- if (parent === dir) break;
111
- dir = parent;
112
- }
113
- let canonical = dir;
114
- try {
115
- canonical = realpathSync.native(dir);
116
- } catch {
117
- }
118
- cachedTempoProjectRoot = canonical;
119
- return canonical;
120
- }
121
- function _resetTempoProjectRoot() {
122
- cachedTempoProjectRoot = null;
123
- }
124
- function tempoNextjsPlugin(pluginOptions = {}) {
125
- return (inputConfigArg) => {
126
- const inputConfig = inputConfigArg ?? {};
127
- if (!process.env.TEMPO) return inputConfig;
128
- const loaderPath = _internal.resolveLoaderPath();
129
- const version = _internal.detectNextVersion();
130
- const omitTurbopack = _internal.shouldOmitTurbopack(version);
131
- const useExperimental = _internal.useExperimentalTurbo(version);
132
- const newRule = buildTurbopackRule(loaderPath, pluginOptions);
133
- const existingTurbo = useExperimental ? inputConfig.experimental?.turbo : inputConfig.turbopack;
134
- const mergedTurbo = mergeTurbopackRules(existingTurbo, newRule);
135
- const webpackHook = buildWebpackHook(
136
- loaderPath,
137
- pluginOptions,
138
- inputConfig.webpack ?? null
139
- );
140
- const next = {
141
- ...inputConfig,
142
- webpack: webpackHook
143
- };
144
- if (!omitTurbopack) {
145
- if (useExperimental) {
146
- next.experimental = {
147
- ...inputConfig.experimental,
148
- turbo: mergedTurbo
149
- };
150
- } else {
151
- next.turbopack = mergedTurbo;
152
- }
153
- }
154
- return next;
155
- };
156
- }
157
- var nextjs_default = tempoNextjsPlugin;
158
- var _internal = {
159
- buildTurbopackRule,
160
- mergeTurbopackRules,
161
- buildWebpackHook,
162
- detectNextVersion,
163
- useExperimentalTurbo,
164
- shouldOmitTurbopack,
165
- resolveLoaderPath
166
- };
167
- export {
168
- _internal,
169
- _resetTempoProjectRoot,
170
- nextjs_default as default,
171
- tempoNextjsPlugin,
172
- tempoProjectRoot
173
- };
174
- //# sourceMappingURL=nextjs.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../nextjs.ts"],"sourcesContent":["/**\n * Next.js plugin that registers the tempo-annotate loader for Webpack and\n * Turbopack. Mirrors the user-facing pattern of `@next/mdx`: a higher-order\n * function that takes plugin options, returns a function that takes the user's\n * `next.config`, and returns a new config with the plugin's loader rules\n * merged in.\n *\n * See SPEC_NEXTJS.md (in this directory) for the full design rationale.\n *\n * @example\n * ```js\n * // next.config.js\n * const tempoNextjsPlugin = require(\"tempo-sdk/nextjs\");\n * module.exports = tempoNextjsPlugin()({ reactStrictMode: true });\n * ```\n */\n\nimport { existsSync, realpathSync } from \"node:fs\";\nimport { createRequire } from \"node:module\";\nimport { dirname, resolve } from \"node:path\";\n\nimport type { ElementInfo, PropValue } from \"@modules/annotation\";\n\n// ---------------------------------------------------------------------------\n// Types. We keep these minimal and self-contained rather than depending on\n// Next.js types — the plugin file is loaded by `next.config.js` evaluation,\n// before `next` itself is fully resolved in some environments.\n// ---------------------------------------------------------------------------\n\nexport type ComputeAnnotatedProps = (\n info: ElementInfo,\n filepath: string,\n modifiedtsMs: number,\n) => Record<string, PropValue>;\n\nexport interface TempoNextjsOptions {\n /** Path to a JS/TS module that default-exports a `ComputeAnnotatedProps`\n * function. Resolved relative to the project root (CWD where the user\n * invokes `next dev` / `next build`). Turbopack does not allow function-\n * valued loader options, so this must be a path. */\n computeAnnotatedPropsModule?: string;\n\n /** File patterns to include. Default: `[\"**\\/*.tsx\", \"**\\/*.jsx\"]`. */\n include?: string | RegExp | Array<string | RegExp>;\n\n /** File patterns to exclude. Default: `[\"**\\/node_modules\\/**\"]`. */\n exclude?: string | RegExp | Array<string | RegExp>;\n\n /** Auto-inject `<meta name=\"tempo-project-root\">` into JSX `<head>`.\n * Default: `true`. */\n autoInjectRootMeta?: boolean;\n\n /** Run the loader during production builds too. Default: `false`. */\n productionBuild?: boolean;\n}\n\ninterface TurbopackRule {\n condition?: unknown;\n loaders?: Array<{ loader: string; options?: unknown } | string>;\n}\n\ninterface TurbopackConfig {\n rules?: Record<string, TurbopackRule | TurbopackRule[]>;\n [key: string]: unknown;\n}\n\ninterface NextConfigLike {\n webpack?: ((config: unknown, context: { dev: boolean; [k: string]: unknown }) => unknown) | null;\n turbopack?: TurbopackConfig;\n experimental?: { turbo?: TurbopackConfig; [k: string]: unknown };\n [key: string]: unknown;\n}\n\ntype NextConfigInput = NextConfigLike | undefined;\n\ninterface WebpackHookContext {\n dev: boolean;\n [key: string]: unknown;\n}\n\ninterface WebpackConfigLike {\n module?: { rules?: unknown[] };\n [key: string]: unknown;\n}\n\n// ---------------------------------------------------------------------------\n// Helpers\n// ---------------------------------------------------------------------------\n\nconst TURBOPACK_RULE_KEY = \"*.{tsx,jsx}\";\n\nconst requireFromHere = createRequire(\n // In ESM the source compiles using import.meta.url; tsup rewrites this for\n // CJS output to use __filename automatically.\n typeof __filename !== \"undefined\" ? __filename : import.meta.url,\n);\n\nfunction resolveLoaderPath(): string {\n // Production: dist/nextjs.{js,cjs} sits next to dist/nextjs-loader.cjs.\n // Development: source nextjs.ts sits at <pkg>/nextjs.ts; the built\n // loader is at <pkg>/dist/nextjs-loader.cjs. Try both so unit tests\n // running directly against the .ts source file still find the loader.\n for (const candidate of [\"./nextjs-loader.cjs\", \"./dist/nextjs-loader.cjs\"]) {\n try {\n return requireFromHere.resolve(candidate);\n } catch {\n // try next candidate\n }\n }\n throw new Error(\n \"tempo-sdk/nextjs: could not resolve nextjs-loader.cjs. \" +\n \"Run `pnpm build` in the tempo-sdk package to produce the dist/ output.\",\n );\n}\n\ninterface NextVersionInfo {\n major: number;\n minor: number;\n}\n\nfunction detectNextVersion(): NextVersionInfo | null {\n try {\n const pkg = requireFromHere(\"next/package.json\") as { version: string };\n const m = /^(\\d+)\\.(\\d+)/.exec(pkg.version);\n if (!m) return null;\n return { major: Number(m[1]), minor: Number(m[2]) };\n } catch {\n return null;\n }\n}\n\n/** Should the wrapper write to `experimental.turbo` instead of `turbopack`? */\nfunction useExperimentalTurbo(version: NextVersionInfo | null): boolean {\n if (!version) return false;\n if (version.major < 15) return true;\n if (version.major === 15 && version.minor < 3) return true;\n return false;\n}\n\n/** Should the wrapper omit Turbopack registration entirely? Some pre-13 versions\n * error on unknown config keys. */\nfunction shouldOmitTurbopack(version: NextVersionInfo | null): boolean {\n if (!version) return false;\n return version.major < 13;\n}\n\nfunction buildTurbopackRule(\n loaderPath: string,\n options: TempoNextjsOptions,\n): TurbopackRule {\n const loaderOptions: Record<string, unknown> = {};\n if (options.computeAnnotatedPropsModule !== undefined) {\n loaderOptions.computeAnnotatedPropsModule = options.computeAnnotatedPropsModule;\n }\n if (options.autoInjectRootMeta !== undefined) {\n loaderOptions.autoInjectRootMeta = options.autoInjectRootMeta;\n }\n\n // {all: [{not: \"foreign\"}, \"development\"]} — exclude node_modules / Next\n // internals AND only run during `next dev`. The \"development\" condition is\n // a Turbopack built-in; it matches both `next dev` and `next build` in dev\n // mode but is inert during a production `next build`.\n const condition = options.productionBuild\n ? { not: \"foreign\" }\n : { all: [{ not: \"foreign\" }, \"development\"] };\n\n return {\n condition,\n loaders: [{ loader: loaderPath, options: loaderOptions }],\n };\n}\n\nfunction mergeTurbopackRules(\n existing: TurbopackConfig | undefined,\n newRule: TurbopackRule,\n): TurbopackConfig {\n const existingRule = existing?.rules?.[TURBOPACK_RULE_KEY];\n let mergedRule: TurbopackRule | TurbopackRule[];\n if (!existingRule) {\n mergedRule = newRule;\n } else if (Array.isArray(existingRule)) {\n mergedRule = [...existingRule, newRule];\n } else {\n mergedRule = [existingRule, newRule];\n }\n return {\n ...existing,\n rules: {\n ...existing?.rules,\n [TURBOPACK_RULE_KEY]: mergedRule,\n },\n };\n}\n\nfunction buildWebpackHook(\n loaderPath: string,\n options: TempoNextjsOptions,\n userWebpack: NextConfigLike[\"webpack\"],\n): NonNullable<NextConfigLike[\"webpack\"]> {\n const loaderOptions: Record<string, unknown> = {};\n if (options.computeAnnotatedPropsModule !== undefined) {\n loaderOptions.computeAnnotatedPropsModule = options.computeAnnotatedPropsModule;\n }\n if (options.autoInjectRootMeta !== undefined) {\n loaderOptions.autoInjectRootMeta = options.autoInjectRootMeta;\n }\n\n const allowProduction = options.productionBuild === true;\n\n return (configIn, context) => {\n const config = configIn as WebpackConfigLike;\n const ctx = context as WebpackHookContext;\n if (ctx.dev || allowProduction) {\n config.module ??= {};\n config.module.rules ??= [];\n config.module.rules.push({\n test: /\\.(tsx|jsx)$/,\n exclude: /node_modules/,\n enforce: \"pre\",\n use: [{ loader: loaderPath, options: loaderOptions }],\n });\n }\n if (typeof userWebpack === \"function\") {\n return userWebpack(config, context);\n }\n return config;\n };\n}\n\n// ---------------------------------------------------------------------------\n// tempoProjectRoot — sync helper for Pages Router users (Section 6.4)\n// ---------------------------------------------------------------------------\n\nlet cachedTempoProjectRoot: string | null = null;\n\n/**\n * Returns the canonical project root that the Tempo loader uses to compute\n * `data-tempo-filepath` values. Use this in `pages/_document.tsx` to set the\n * content of `<meta name=\"tempo-project-root\">`, ensuring the manual meta tag\n * value matches what the loader produces.\n */\nexport function tempoProjectRoot(): string {\n if (cachedTempoProjectRoot !== null) return cachedTempoProjectRoot;\n\n // Walk upward from CWD looking for the nearest package.json. CWD is what\n // Next.js itself uses as the project root when the user invokes\n // `next dev` / `next build` — the loader's `rootContext` matches.\n let dir = process.cwd();\n // eslint-disable-next-line no-constant-condition\n while (true) {\n if (existsSync(resolve(dir, \"package.json\"))) break;\n const parent = dirname(dir);\n if (parent === dir) break;\n dir = parent;\n }\n let canonical = dir;\n try {\n canonical = realpathSync.native(dir);\n } catch {\n // Keep `dir` as fallback.\n }\n cachedTempoProjectRoot = canonical;\n return canonical;\n}\n\n/** Reset the cached project root. For tests only. */\nexport function _resetTempoProjectRoot(): void {\n cachedTempoProjectRoot = null;\n}\n\n// ---------------------------------------------------------------------------\n// tempoNextjsPlugin — the wrapper\n// ---------------------------------------------------------------------------\n\n/**\n * Higher-order function that wraps a Next.js config to register the\n * tempo-annotate loader with both Webpack and Turbopack. No-op when\n * `process.env.TEMPO` is not set.\n *\n * @example\n * ```js\n * // next.config.js\n * const tempoNextjsPlugin = require(\"tempo-sdk/nextjs\");\n * module.exports = tempoNextjsPlugin()({ reactStrictMode: true });\n * ```\n */\nexport function tempoNextjsPlugin(\n pluginOptions: TempoNextjsOptions = {},\n): (inputConfig?: NextConfigLike) => NextConfigLike {\n return (inputConfigArg?: NextConfigLike): NextConfigLike => {\n const inputConfig: NextConfigLike = inputConfigArg ?? {};\n\n if (!process.env.TEMPO) return inputConfig;\n\n // Routed through `_internal.*` so unit tests can spy on these helpers\n // without rewiring the imports.\n const loaderPath = _internal.resolveLoaderPath();\n const version = _internal.detectNextVersion();\n const omitTurbopack = _internal.shouldOmitTurbopack(version);\n const useExperimental = _internal.useExperimentalTurbo(version);\n\n const newRule = buildTurbopackRule(loaderPath, pluginOptions);\n\n const existingTurbo = useExperimental\n ? inputConfig.experimental?.turbo\n : inputConfig.turbopack;\n const mergedTurbo = mergeTurbopackRules(existingTurbo, newRule);\n\n const webpackHook = buildWebpackHook(\n loaderPath,\n pluginOptions,\n inputConfig.webpack ?? null,\n );\n\n const next: NextConfigLike = {\n ...inputConfig,\n webpack: webpackHook,\n };\n\n if (!omitTurbopack) {\n if (useExperimental) {\n next.experimental = {\n ...inputConfig.experimental,\n turbo: mergedTurbo,\n };\n } else {\n next.turbopack = mergedTurbo;\n }\n }\n\n return next;\n };\n}\n\nexport default tempoNextjsPlugin;\n\n// ---------------------------------------------------------------------------\n// Type re-exports for users writing custom strategies.\n// ---------------------------------------------------------------------------\n\nexport type { ElementInfo, PropValue } from \"@modules/annotation\";\n\n// Internal exports used by tests.\nexport const _internal = {\n buildTurbopackRule,\n mergeTurbopackRules,\n buildWebpackHook,\n detectNextVersion,\n useExperimentalTurbo,\n shouldOmitTurbopack,\n resolveLoaderPath,\n};\n"],"mappings":";AAiBA,SAAS,YAAY,oBAAoB;AACzC,SAAS,qBAAqB;AAC9B,SAAS,SAAS,eAAe;AAsEjC,IAAM,qBAAqB;AAE3B,IAAM,kBAAkB;AAAA;AAAA;AAAA,EAGtB,OAAO,eAAe,cAAc,aAAa,YAAY;AAC/D;AAEA,SAAS,oBAA4B;AAKnC,aAAW,aAAa,CAAC,uBAAuB,0BAA0B,GAAG;AAC3E,QAAI;AACF,aAAO,gBAAgB,QAAQ,SAAS;AAAA,IAC1C,QAAQ;AAAA,IAER;AAAA,EACF;AACA,QAAM,IAAI;AAAA,IACR;AAAA,EAEF;AACF;AAOA,SAAS,oBAA4C;AACnD,MAAI;AACF,UAAM,MAAM,gBAAgB,mBAAmB;AAC/C,UAAM,IAAI,gBAAgB,KAAK,IAAI,OAAO;AAC1C,QAAI,CAAC,EAAG,QAAO;AACf,WAAO,EAAE,OAAO,OAAO,EAAE,CAAC,CAAC,GAAG,OAAO,OAAO,EAAE,CAAC,CAAC,EAAE;AAAA,EACpD,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAGA,SAAS,qBAAqB,SAA0C;AACtE,MAAI,CAAC,QAAS,QAAO;AACrB,MAAI,QAAQ,QAAQ,GAAI,QAAO;AAC/B,MAAI,QAAQ,UAAU,MAAM,QAAQ,QAAQ,EAAG,QAAO;AACtD,SAAO;AACT;AAIA,SAAS,oBAAoB,SAA0C;AACrE,MAAI,CAAC,QAAS,QAAO;AACrB,SAAO,QAAQ,QAAQ;AACzB;AAEA,SAAS,mBACP,YACA,SACe;AACf,QAAM,gBAAyC,CAAC;AAChD,MAAI,QAAQ,gCAAgC,QAAW;AACrD,kBA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