@animus-ui/next-plugin 0.1.25 → 0.1.27

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/index.cjs CHANGED
@@ -1,10 +1,32 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_turbopack_config = require("./turbopack-config-D-6BRBYi.cjs");
2
+ const require_turbopack_config = require("./turbopack-config-BoNfzhNu.cjs");
3
3
  let _animus_ui_extract_pipeline = require("@animus-ui/extract/pipeline");
4
4
  let _animus_ui_extract_session = require("@animus-ui/extract/session");
5
5
  let fs = require("fs");
6
6
  let path = require("path");
7
7
  let url = require("url");
8
+ //#region src/same-directory.ts
9
+ /** Whether two paths name one directory: by real path, which resolves a
10
+ * trailing slash, a symlink and the filesystem's own spelling, else by
11
+ * device and inode. */
12
+ function sameDirectory(a, b) {
13
+ const real = (path$5) => {
14
+ try {
15
+ return fs.realpathSync.native(path$5);
16
+ } catch {
17
+ return (0, path.resolve)(path$5);
18
+ }
19
+ };
20
+ if (real(a) === real(b)) return true;
21
+ try {
22
+ const left = (0, fs.statSync)(a);
23
+ const right = (0, fs.statSync)(b);
24
+ return left.dev === right.dev && left.ino === right.ino;
25
+ } catch {
26
+ return false;
27
+ }
28
+ }
29
+ //#endregion
8
30
  //#region src/plugin.ts
9
31
  /** The value arrives from a consumer's `next.config`, so the union above is a
10
32
  * model of what webpack accepts, not a proof about the value in hand. */
@@ -16,6 +38,9 @@ var AnimusWebpackPlugin = class {
16
38
  options;
17
39
  session;
18
40
  initialized = false;
41
+ /** Left its build: the next turn joins again and catches up, retried
42
+ * until it succeeds, never reusing the startup it already consumed. */
43
+ rejoining = false;
19
44
  aliasesExtracted = false;
20
45
  animusLoaderPaths;
21
46
  lastBuiltEpoch = null;
@@ -25,13 +50,15 @@ var AnimusWebpackPlugin = class {
25
50
  watchFailure = null;
26
51
  /** `development` is `next dev`: an error-level diagnostic reports and the
27
52
  * compilation keeps publishing, whatever the emission mode. */
28
- constructor(options, { development = false } = {}) {
53
+ constructor(options, { development = false, build } = {}) {
29
54
  this.options = options;
30
- (0, _animus_ui_extract_pipeline.assertNoRetiredEngineSelection)(options.engine);
31
- (0, _animus_ui_extract_session.setSharedEngine)(options.engine ?? "v2");
32
- this.session = new _animus_ui_extract_session.ExtractionSession(options);
55
+ (0, _animus_ui_extract_pipeline.assertSupportedEngine)(options.engine);
56
+ this.session = new _animus_ui_extract_session.ExtractionSession(options, { build });
33
57
  this.session.development = development;
34
- this.session.systemPropsModuleId = _animus_ui_extract_session.TURBOPACK_SYSTEM_PROPS_ID;
58
+ this.session.delivery = {
59
+ ...this.session.delivery,
60
+ systemPropsModuleId: _animus_ui_extract_session.TURBOPACK_SYSTEM_PROPS_ID
61
+ };
35
62
  this.animusLoaderPaths = /* @__PURE__ */ new Set([require_turbopack_config.resolveAnimusLoaderPath()]);
36
63
  if (options.loaderPath) this.animusLoaderPaths.add(options.loaderPath);
37
64
  }
@@ -51,14 +78,22 @@ var AnimusWebpackPlugin = class {
51
78
  else if (Array.isArray(ignored)) {
52
79
  if (!ignored.includes(epochPath)) ignored.push(epochPath);
53
80
  } else if (isWatchIgnoreString(ignored)) watchOptions.ignored = [ignored, epochPath];
54
- else if (ignored instanceof RegExp) watchOptions.ignored = (path) => path === epochPath || ignored.test(path);
55
- else if (isWatchIgnoreMatcher(ignored)) watchOptions.ignored = (path) => path === epochPath || Boolean(ignored(path));
81
+ else if (ignored instanceof RegExp) watchOptions.ignored = (path$1) => path$1 === epochPath || ignored.test(path$1);
82
+ else if (isWatchIgnoreMatcher(ignored)) watchOptions.ignored = (path$2) => path$2 === epochPath || Boolean(ignored(path$2));
56
83
  }
57
84
  /** Webpack's export conditions select the system's kit files as they
58
- * select the bundle's; `...`, webpack's own defaults, names none. */
85
+ * select the bundle's; `...`, webpack's own defaults, names none. Its
86
+ * extension order and aliases are captured alongside. */
59
87
  extractConditions(compiler) {
60
- const names = compiler.options?.resolve?.conditionNames ?? [];
88
+ const resolve = compiler.options?.resolve;
89
+ const names = resolve?.conditionNames ?? [];
61
90
  this.session.conditions = names.filter((name) => name !== "...");
91
+ const resolverFactory = compiler.resolverFactory;
92
+ if (resolverFactory !== void 0) this.session.hostResolve = (specifier, importer) => new Promise((done) => {
93
+ resolverFactory.get("normal", { dependencyType: "esm" }).resolve({}, importer === void 0 ? compiler.context : (0, path.dirname)(importer), specifier, {}, (error, result) => done(error !== null || !result ? null : result));
94
+ });
95
+ this.session.assetClassification = (0, _animus_ui_extract_pipeline.builtInAssetClassification)("hostOptionUnreadable", "next-webpack rules not modelled");
96
+ this.session.extensionPolicy = (0, _animus_ui_extract_pipeline.webpackExtensionPolicy)("next-webpack", resolve ?? {}, compiler.webpack?.version);
62
97
  }
63
98
  extractAliases(compiler) {
64
99
  if (this.aliasesExtracted) return;
@@ -82,6 +117,13 @@ var AnimusWebpackPlugin = class {
82
117
  if (built) this.session.pathAliasesJson = built.json;
83
118
  }
84
119
  apply(compiler) {
120
+ const leave = () => {
121
+ this.session.close();
122
+ this.initialized = false;
123
+ this.rejoining = true;
124
+ };
125
+ compiler.hooks.shutdown?.tap(PLUGIN_NAME, leave);
126
+ compiler.hooks.watchClose?.tap(PLUGIN_NAME, leave);
85
127
  if (compiler.options?.name === "edge-server") return;
86
128
  const NormalModule = compiler.webpack?.NormalModule;
87
129
  if (NormalModule?.getCompilationHooks === void 0) throw new Error(UNSUPPORTED_WEBPACK_MESSAGE);
@@ -102,11 +144,11 @@ var AnimusWebpackPlugin = class {
102
144
  });
103
145
  compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
104
146
  const registerSystemDependencies = () => {
105
- for (const dep of this.session.systemDependencyPaths) if ((0, fs.existsSync)(dep)) compilation.fileDependencies.add(dep);
106
- else compilation.missingDependencies.add(dep);
107
- for (const dep of this.session.assetDependencyPaths) if ((0, fs.existsSync)(dep)) compilation.fileDependencies.add(dep);
108
- else compilation.missingDependencies.add(dep);
109
- for (const root of this.session.externalWatchRoots) compilation.contextDependencies.add(root);
147
+ const plan = this.session.watchPlan();
148
+ for (const path$3 of plan.files) if ((0, fs.existsSync)(path$3)) compilation.fileDependencies.add(path$3);
149
+ else compilation.missingDependencies.add(path$3);
150
+ for (const path$4 of plan.missing) compilation.missingDependencies.add(path$4);
151
+ for (const root of plan.directories) compilation.contextDependencies.add(root);
110
152
  };
111
153
  registerSystemDependencies();
112
154
  const stage = compiler.webpack?.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL ?? -2e3;
@@ -116,7 +158,7 @@ var AnimusWebpackPlugin = class {
116
158
  stage
117
159
  }, () => {
118
160
  registerSystemDependencies();
119
- const css = (0, _animus_ui_extract_session.getSharedCss)();
161
+ const css = this.session.rootDir ? (0, _animus_ui_extract_session.getSharedCss)(this.session.key) : "";
120
162
  if (!css || !RawSource) return;
121
163
  const rootDir = this.session.rootDir || compiler.context;
122
164
  const cssPath = (0, _animus_ui_extract_session.stylesPath)((0, _animus_ui_extract_session.sessionArtifactDir)(rootDir, this.session.sessionId));
@@ -131,13 +173,18 @@ var AnimusWebpackPlugin = class {
131
173
  this.adoptCompilerContext(_compiler);
132
174
  this.extractAliases(_compiler);
133
175
  this.extractConditions(_compiler);
134
- const existing = (0, _animus_ui_extract_session.getAnalysisStartedPromise)();
176
+ if (await this.rejoin()) {
177
+ this.session.agreeWithSystemAuthority();
178
+ return;
179
+ }
180
+ const existing = (0, _animus_ui_extract_session.getAnalysisStartedPromise)(this.session.key);
135
181
  if (existing) {
136
182
  await existing;
183
+ this.session.agreeWithSystemAuthority();
137
184
  return;
138
185
  }
139
186
  const promise = this.session.runFullPipeline();
140
- (0, _animus_ui_extract_session.setAnalysisStartedPromise)(promise);
187
+ (0, _animus_ui_extract_session.setAnalysisStartedPromise)(this.session.key, promise);
141
188
  await promise;
142
189
  });
143
190
  compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async (_compiler) => {
@@ -154,21 +201,38 @@ var AnimusWebpackPlugin = class {
154
201
  }
155
202
  /** Rejects with the analysis error; the published generation stays the
156
203
  * last successful one. */
204
+ /** Joins the build this compiler left and catches up with it: true when
205
+ * the build was live, false when it starts as at first. */
206
+ async rejoin() {
207
+ if (!this.rejoining) return false;
208
+ this.session.reattach();
209
+ const live = await this.session.catchUp();
210
+ this.rejoining = false;
211
+ return live;
212
+ }
157
213
  async analyzeWatchTurn(compiler) {
214
+ if (!this.initialized && await this.rejoin()) {
215
+ this.session.agreeWithSystemAuthority();
216
+ this.initialized = true;
217
+ this.lastBuiltEpoch = (0, _animus_ui_extract_session.getReplacementEpoch)(this.session.key);
218
+ this.epochMovedForNextCompilation = true;
219
+ return;
220
+ }
158
221
  if (!this.initialized) {
159
- let startup = (0, _animus_ui_extract_session.getAnalysisStartedPromise)();
222
+ let startup = (0, _animus_ui_extract_session.getAnalysisStartedPromise)(this.session.key);
160
223
  if (!startup) {
161
224
  startup = this.session.runFullPipeline();
162
- (0, _animus_ui_extract_session.setAnalysisStartedPromise)(startup);
225
+ (0, _animus_ui_extract_session.setAnalysisStartedPromise)(this.session.key, startup);
163
226
  }
164
227
  try {
165
228
  await startup;
166
229
  } catch (err) {
167
- if ((0, _animus_ui_extract_session.getAnalysisStartedPromise)() === startup) (0, _animus_ui_extract_session.resetAnalysisStartedPromise)();
230
+ if ((0, _animus_ui_extract_session.getAnalysisStartedPromise)(this.session.key) === startup) (0, _animus_ui_extract_session.resetAnalysisStartedPromise)(this.session.key);
168
231
  throw err;
169
232
  }
233
+ this.session.agreeWithSystemAuthority();
170
234
  this.initialized = true;
171
- this.lastBuiltEpoch = (0, _animus_ui_extract_session.getReplacementEpoch)();
235
+ this.lastBuiltEpoch = (0, _animus_ui_extract_session.getReplacementEpoch)(this.session.key);
172
236
  this.epochMovedForNextCompilation = this.watchFailure !== null;
173
237
  return;
174
238
  }
@@ -176,7 +240,8 @@ var AnimusWebpackPlugin = class {
176
240
  modifiedFiles: compiler.modifiedFiles,
177
241
  removedFiles: compiler.removedFiles
178
242
  });
179
- const epoch = (0, _animus_ui_extract_session.getReplacementEpoch)();
243
+ this.session.agreeWithSystemAuthority();
244
+ const epoch = (0, _animus_ui_extract_session.getReplacementEpoch)(this.session.key);
180
245
  this.epochMovedForNextCompilation = this.lastBuiltEpoch !== null && epoch !== null && epoch !== this.lastBuiltEpoch;
181
246
  if (epoch !== null) this.lastBuiltEpoch = epoch;
182
247
  }
@@ -189,21 +254,23 @@ var AnimusWebpackPlugin = class {
189
254
  setRootDir(rootDir) {
190
255
  this.session.rootDir = rootDir;
191
256
  }
192
- warnedRootDivergence = false;
257
+ /** Keeps Next's output directory out of discovery, whatever the user's
258
+ * exclude list says. Call after `setRootDir`. */
259
+ excludeOutputDir(outDir) {
260
+ const pattern = (0, _animus_ui_extract_pipeline.outputExclusion)(this.session.rootDir, outDir);
261
+ if (pattern !== null) this.session.structuralExclude = [pattern];
262
+ }
193
263
  /** Adopts `compiler.context` only when no config-time root exists: every
194
264
  * frozen derivation came from that root, so an overwrite re-keys it.
195
265
  * Stub path, css alias, virtual system-props target and watch-ignore are
196
- * all frozen from it, so a disagreement is warned, never reconciled. */
266
+ * all frozen from it, so a disagreement fails, never reconciled. */
197
267
  adoptCompilerContext(compiler) {
198
268
  const context = compiler.context;
199
269
  if (!this.session.rootDir) {
200
270
  this.session.rootDir = context;
201
271
  return;
202
272
  }
203
- if (context && this.session.rootDir !== context && !this.warnedRootDivergence) {
204
- this.warnedRootDivergence = true;
205
- console.warn(`[animus-extract] compiler.context (${context}) differs from the configured project root (${this.session.rootDir}); keeping the configured root — run next against the app directory (or align your custom webpack context) so both agree`);
206
- }
273
+ if (context && !sameDirectory(context, this.session.rootDir)) throw new _animus_ui_extract_pipeline.AnimusConfigError(`[animus-extract] compiler.context (${context}) differs from the configured project root (${this.session.rootDir}). Run next against the app directory, or align your custom webpack context, so both agree.`);
207
274
  }
208
275
  get sessionDir() {
209
276
  return this.session.sessionDir;
@@ -212,15 +279,20 @@ var AnimusWebpackPlugin = class {
212
279
  return this.options;
213
280
  }
214
281
  getExternalPackageDirs() {
215
- return this.session.externalPackageDirs.length > 0 ? this.session.externalPackageDirs : (0, _animus_ui_extract_session.getSharedExternalDirs)();
282
+ return this.session.externalPackageDirs.length > 0 ? this.session.externalPackageDirs : (0, _animus_ui_extract_session.getSharedExternalDirs)(this.session.key);
283
+ }
284
+ /** An import webpack resolved while bundling, with the resolve options of
285
+ * the rules its importer matches (spec §2.2 A4). */
286
+ reportHostResolution(importer, request, resolved) {
287
+ this.session.reportHostResolution(importer, request, resolved);
216
288
  }
217
289
  getExternalSourceEntries() {
218
- return this.session.externalSourceEntries.size > 0 ? this.session.externalSourceEntries : (0, _animus_ui_extract_session.getSharedExternalEntries)();
290
+ return this.session.externalSourceEntries.size > 0 ? this.session.externalSourceEntries : (0, _animus_ui_extract_session.getSharedExternalEntries)(this.session.key);
219
291
  }
220
292
  /** The classification of the entries `getExternalSourceEntries` returns,
221
293
  * from the same source. */
222
294
  getExternalSourceSideEffects() {
223
- return this.session.externalSourceEntries.size > 0 ? this.session.externalSourceSideEffects : (0, _animus_ui_extract_session.getSharedExternalSideEffects)();
295
+ return this.session.externalSourceEntries.size > 0 ? this.session.externalSourceSideEffects : (0, _animus_ui_extract_session.getSharedExternalSideEffects)(this.session.key);
224
296
  }
225
297
  };
226
298
  //#endregion
@@ -234,6 +306,7 @@ const PHASE_PRODUCTION_BUILD = "phase-production-build";
234
306
  let warnedGitignore = false;
235
307
  let warnedUnstableTurbopack = false;
236
308
  function withAnimus(options) {
309
+ (0, _animus_ui_extract_pipeline.assertSupportedEngine)(options.engine);
237
310
  if (!options.system) throw new Error("[animus-extract] Missing required option `system`. Provide the path to your SystemInstance module: withAnimus({ system: \"./src/ds.ts\" })");
238
311
  (0, _animus_ui_extract_pipeline.assertKnownOptionKeys)({ ...options }, [
239
312
  "cssImportTarget",
@@ -241,10 +314,7 @@ function withAnimus(options) {
241
314
  "unstable_turbopack",
242
315
  "loaderPath",
243
316
  "phase"
244
- ], [{
245
- key: "root",
246
- reason: "this driver derives its own root — Next's `dir` under webpack, `process.cwd()` under Turbopack (Next passes no dir to a config module)"
247
- }], {
317
+ ], [], {
248
318
  onUnknownKey: "warn",
249
319
  warn: (message) => console.warn(`[animus-extract] ${message}`)
250
320
  });
@@ -252,21 +322,31 @@ function withAnimus(options) {
252
322
  warnedUnstableTurbopack = true;
253
323
  console.warn("[animus-extract] `unstable_turbopack` is deprecated — rename it to `turbopack` (same shape)");
254
324
  }
325
+ const build = (0, _animus_ui_extract_session.createBuildId)();
326
+ const appRoot = options.root === void 0 ? process.cwd() : (0, path.resolve)(options.root);
255
327
  return (authoredConfig) => {
256
- const nextConfig = withSourceKits(authoredConfig, process.cwd());
257
- if (require_turbopack_config.resolveTurbopackMode(options)) return turbopackConfig(nextConfig, options);
328
+ const nextConfig = withSourceKits(authoredConfig, appRoot);
329
+ if (require_turbopack_config.resolveTurbopackMode(options)) {
330
+ warnTurbopackCwdRoot(options, appRoot);
331
+ return turbopackConfig(nextConfig, options, build, appRoot);
332
+ }
258
333
  const existingWebpack = nextConfig.webpack;
259
334
  return {
260
335
  ...nextConfig,
261
336
  webpack(config, context) {
262
337
  if (existingWebpack) config = existingWebpack(config, context);
263
- const rootDir = context.dir?.length ? context.dir : process.cwd();
338
+ if (context.dir?.length && !sameDirectory(context.dir, appRoot)) throw new _animus_ui_extract_pipeline.AnimusConfigError(`[animus-extract] Next builds ${context.dir}, but Animus read the app's source kits from ${appRoot}. Run next from the app directory, or pass withAnimus({ root }) naming it.`);
339
+ const rootDir = appRoot;
264
340
  const { mode } = (0, _animus_ui_extract_pipeline.resolveMode)(options.mode, () => context.dev === true ? "development" : "production");
265
341
  const plugin = new AnimusWebpackPlugin({
266
342
  ...options,
267
343
  mode
268
- }, { development: context.dev === true });
344
+ }, {
345
+ development: context.dev === true,
346
+ build
347
+ });
269
348
  plugin.setRootDir(rootDir);
349
+ if (nextConfig.distDir) plugin.excludeOutputDir((0, path.resolve)(rootDir, nextConfig.distDir));
270
350
  const sessionDir = plugin.sessionDir;
271
351
  if (!(0, fs.existsSync)(sessionDir)) (0, fs.mkdirSync)(sessionDir, { recursive: true });
272
352
  const stubCssPath = (0, _animus_ui_extract_session.stylesPath)(sessionDir);
@@ -311,6 +391,9 @@ function withAnimus(options) {
311
391
  }
312
392
  });
313
393
  nmf.hooks.afterResolve.tap("AnimusVirtualResolve", (resolveData) => {
394
+ const issuer = resolveData.contextInfo?.issuer;
395
+ const resolved = resolveData.createData?.resource;
396
+ if (issuer && resolved !== void 0) plugin.reportHostResolution(issuer, resolveData.request, resolved);
314
397
  const sideEffects = redirectSideEffects.get(resolveData);
315
398
  const settings = resolveData.createData?.settings;
316
399
  if (!settings) return;
@@ -336,9 +419,10 @@ function withAnimus(options) {
336
419
  use: [{
337
420
  loader: actualLoaderPath,
338
421
  options: {
339
- strict: options.strict,
340
422
  development: context.dev === true,
341
- cssImportTarget: options.cssImportTarget
423
+ cssImportTarget: options.cssImportTarget,
424
+ build,
425
+ root: rootDir
342
426
  }
343
427
  }]
344
428
  });
@@ -359,17 +443,35 @@ function bindTurbopackWatchDeathReport(outcome, rootDir) {
359
443
  * source, but Next's server keeps an installed package external, and so
360
444
  * loads its runtime entry, unless the package is transpiled; transpiling
361
445
  * also compiles its TypeScript source. */
446
+ let warnedTurbopackCwd = false;
447
+ /** Turbopack's loaders get no app dir either, so a cwd that is not the app
448
+ * cannot be detected later: warn at startup when the cwd holds no Next
449
+ * config and no `root` names the app. */
450
+ function warnTurbopackCwdRoot(options, appRoot) {
451
+ if (options.root !== void 0 || warnedTurbopackCwd) return;
452
+ if ([
453
+ "js",
454
+ "mjs",
455
+ "cjs",
456
+ "ts",
457
+ "mts"
458
+ ].some((ext) => (0, fs.existsSync)((0, path.join)(appRoot, `next.config.${ext}`)))) return;
459
+ warnedTurbopackCwd = true;
460
+ console.warn(`[animus-extract] the current directory (${appRoot}) holds no next.config: under Turbopack, Animus reads the app from the current directory. Run next from the app directory, or pass withAnimus({ root }) naming it.`);
461
+ }
362
462
  function withSourceKits(nextConfig, rootDir) {
363
463
  const declared = nextConfig.transpilePackages ?? [];
364
- const kits = (0, _animus_ui_extract_pipeline.sourceKitDependencies)(rootDir).map((kit) => kit.name).filter((name) => !declared.includes(name));
464
+ const kits = (0, _animus_ui_extract_pipeline.sourceKitDependencies)(rootDir, (0, _animus_ui_extract_session.requireEngine)()).map((kit) => kit.name).filter((name) => !declared.includes(name));
365
465
  if (kits.length === 0) return nextConfig;
366
466
  return {
367
467
  ...nextConfig,
368
468
  transpilePackages: [...declared, ...kits]
369
469
  };
370
470
  }
371
- function turbopackConfig(nextConfig, options) {
372
- const load = (phase) => loadTurbopackFragment(options, phase).then((fragment) => mergeTurbopackFragment(nextConfig, fragment));
471
+ function turbopackConfig(nextConfig, options, build, appRoot) {
472
+ const resolveExtensions = nextConfig.turbopack?.resolveExtensions ?? nextConfig.experimental?.turbo?.resolveExtensions;
473
+ const outputExclude = nextConfig.distDir ? (0, _animus_ui_extract_pipeline.outputExclusion)(appRoot, (0, path.resolve)(appRoot, nextConfig.distDir)) : null;
474
+ const load = (phase) => loadTurbopackFragment(options, phase, resolveExtensions, outputExclude, nextConfig.images?.disableStaticImages === true, build, appRoot).then((fragment) => mergeTurbopackFragment(nextConfig, fragment));
373
475
  function then(onResolved, onRejected) {
374
476
  const phase = options.phase ?? (require_turbopack_config.isTurbopackDevelopment() ? PHASE_DEVELOPMENT_SERVER : PHASE_PRODUCTION_BUILD);
375
477
  return load(phase).then(onResolved, onRejected);
@@ -378,49 +480,67 @@ function turbopackConfig(nextConfig, options) {
378
480
  Object.defineProperty(config, "then", { enumerable: false });
379
481
  return config;
380
482
  }
381
- let liveTurbopackSession = null;
382
- let liveTurbopackWatcher = null;
383
- /** The load the live session came from. Next loads the config again after
384
- * Ready (validateTurboNextConfig), and a user config may await this one
385
- * besides: the same phase and root share one analysis and keep the watcher.
386
- * A rejected load is dropped, so a repaired source loads again. */
387
- let liveTurbopackLoad = null;
388
- function loadTurbopackFragment(options, phase) {
389
- const rootDir = process.cwd();
390
- const key = JSON.stringify([phase, rootDir]);
391
- if (liveTurbopackLoad?.key === key) return liveTurbopackLoad.fragment;
392
- const fragment = analyzeForTurbopack(options, phase, rootDir);
483
+ /** Each wrapper's owner, by the build it created: another wrapper of the
484
+ * same root is another build and never shares or closes this one's. */
485
+ const turbopackOwners = /* @__PURE__ */ new Map();
486
+ function turbopackOwner(build) {
487
+ let owner = turbopackOwners.get(build);
488
+ if (owner === void 0) {
489
+ owner = {
490
+ session: null,
491
+ watcher: null,
492
+ load: null
493
+ };
494
+ turbopackOwners.set(build, owner);
495
+ }
496
+ return owner;
497
+ }
498
+ function loadTurbopackFragment(options, phase, resolveExtensions, outputExclude, disableStaticImages, build, rootDir) {
499
+ const key = JSON.stringify([
500
+ phase,
501
+ rootDir,
502
+ resolveExtensions ?? null,
503
+ disableStaticImages
504
+ ]);
505
+ const owner = turbopackOwner(build);
506
+ if (owner.load?.key === key) return owner.load.fragment;
507
+ const fragment = analyzeForTurbopack(options, phase, rootDir, resolveExtensions, outputExclude, disableStaticImages, build);
393
508
  const load = {
394
509
  key,
395
510
  fragment
396
511
  };
397
- liveTurbopackLoad = load;
512
+ owner.load = load;
398
513
  fragment.catch(() => {
399
- if (liveTurbopackLoad === load) liveTurbopackLoad = null;
514
+ if (owner.load === load) owner.load = null;
400
515
  });
401
516
  return fragment;
402
517
  }
403
- async function analyzeForTurbopack(options, phase, rootDir) {
518
+ async function analyzeForTurbopack(options, phase, rootDir, resolveExtensions, outputExclude, disableStaticImages, build) {
404
519
  const development = phase === PHASE_DEVELOPMENT_SERVER;
405
520
  const { mode } = (0, _animus_ui_extract_pipeline.resolveMode)(options.mode, () => development ? "development" : "production");
406
521
  const session = new _animus_ui_extract_session.ExtractionSession({
407
522
  ...options,
408
523
  mode
409
- }, { unbundledComputedImports: true });
410
- liveTurbopackSession?.close();
411
- liveTurbopackWatcher?.close();
412
- liveTurbopackWatcher = null;
413
- liveTurbopackSession = session;
524
+ }, {
525
+ unbundledComputedImports: true,
526
+ build: `${build}-${phase}`
527
+ });
528
+ const owner = turbopackOwner(build);
529
+ owner.session?.close();
530
+ owner.watcher?.close();
531
+ owner.watcher = null;
532
+ owner.session = session;
414
533
  session.development = development;
415
534
  session.rootDir = rootDir;
416
- const aliasPairs = (0, _animus_ui_extract_pipeline.readTsconfigAliasPairs)(rootDir);
417
- const builtAliases = (0, _animus_ui_extract_pipeline.buildPathAliasesJson)(aliasPairs, rootDir);
418
- if (builtAliases) session.pathAliasesJson = builtAliases.json;
535
+ if (outputExclude !== null) session.structuralExclude = [outputExclude];
536
+ session.extensionPolicy = (0, _animus_ui_extract_pipeline.turbopackExtensionPolicy)(resolveExtensions);
537
+ session.assetClassification = (0, _animus_ui_extract_pipeline.nextImageAssetClassification)(rootDir, disableStaticImages);
538
+ session.aliasesFromTsconfig = true;
419
539
  await (0, _animus_ui_extract_session.runSessionPipeline)(session);
420
- if (development && liveTurbopackSession === session) {
540
+ if (development && owner.session === session) {
421
541
  const outcome = (0, _animus_ui_extract_session.startTurbopackWatcher)(session, rootDir);
422
542
  bindTurbopackWatchDeathReport(outcome, rootDir);
423
- if (outcome.kind === "started") liveTurbopackWatcher = outcome.handle;
543
+ if (outcome.kind === "started") owner.watcher = outcome.handle;
424
544
  }
425
545
  for (const line of require_turbopack_config.turbopackSideEffectsLimits(rootDir, session.externalSourceEntries, session.externalSourceSideEffects)) console.warn(line);
426
546
  return require_turbopack_config.buildTurbopackConfig({