@sanity/pkg-utils 6.9.2 → 6.9.4-canary.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.
package/dist/index.d.cts CHANGED
@@ -64,12 +64,13 @@ export declare interface BuildFile {
64
64
  }
65
65
 
66
66
  /** @internal */
67
- export declare type BuildTask = DtsTask | RollupTask | RollupLegacyTask
67
+ export declare type BuildTask = DtsTask | RollupTask | RollupLegacyTask | RollupReactCompilerTask
68
68
 
69
69
  /** @internal */
70
70
  export declare interface BuildTaskHandlers {
71
71
  'build:dts': TaskHandler<DtsTask, DtsResult>
72
72
  'build:js': TaskHandler<RollupTask>
73
+ 'build:react-compiler': TaskHandler<RollupReactCompilerTask>
73
74
  'build:legacy': TaskHandler<RollupLegacyTask>
74
75
  }
75
76
 
@@ -399,22 +400,26 @@ export declare type PkgConfigPropertyResolver<T> = (prev: T) => T
399
400
  /** @public */
400
401
  export declare interface PkgExport {
401
402
  /** @internal */
402
- _exported?: boolean
403
- browser?: {
403
+ '_exported'?: boolean
404
+ 'browser'?: {
404
405
  source: string
405
406
  import?: string
406
407
  require?: string
407
408
  }
408
- node?: {
409
+ 'react-compiler'?: {
410
+ source?: string
411
+ default: string
412
+ }
413
+ 'node'?: {
409
414
  source?: string
410
415
  import?: string
411
416
  require?: string
412
417
  }
413
- types?: string
414
- source: string
415
- import?: string
416
- require?: string
417
- default: string
418
+ 'types'?: string
419
+ 'source': string
420
+ 'import'?: string
421
+ 'require'?: string
422
+ 'default': string
418
423
  }
419
424
 
420
425
  /** @public */
@@ -525,6 +530,18 @@ export declare const rollupLegacyTask: TaskHandler<RollupLegacyTask>
525
530
 
526
531
  export {RollupPlugin}
527
532
 
533
+ /** @internal */
534
+ export declare interface RollupReactCompilerTask {
535
+ type: 'build:react-compiler'
536
+ entries: RollupTaskEntry[]
537
+ runtime: 'browser'
538
+ format: 'esm'
539
+ target: string[]
540
+ }
541
+
542
+ /** @internal */
543
+ export declare const rollupReactCompilerTask: TaskHandler<RollupReactCompilerTask>
544
+
528
545
  /** @internal */
529
546
  export declare interface RollupTask {
530
547
  type: 'build:js'
package/dist/index.d.ts CHANGED
@@ -64,12 +64,13 @@ export declare interface BuildFile {
64
64
  }
65
65
 
66
66
  /** @internal */
67
- export declare type BuildTask = DtsTask | RollupTask | RollupLegacyTask
67
+ export declare type BuildTask = DtsTask | RollupTask | RollupLegacyTask | RollupReactCompilerTask
68
68
 
69
69
  /** @internal */
70
70
  export declare interface BuildTaskHandlers {
71
71
  'build:dts': TaskHandler<DtsTask, DtsResult>
72
72
  'build:js': TaskHandler<RollupTask>
73
+ 'build:react-compiler': TaskHandler<RollupReactCompilerTask>
73
74
  'build:legacy': TaskHandler<RollupLegacyTask>
74
75
  }
75
76
 
@@ -399,22 +400,26 @@ export declare type PkgConfigPropertyResolver<T> = (prev: T) => T
399
400
  /** @public */
400
401
  export declare interface PkgExport {
401
402
  /** @internal */
402
- _exported?: boolean
403
- browser?: {
403
+ '_exported'?: boolean
404
+ 'browser'?: {
404
405
  source: string
405
406
  import?: string
406
407
  require?: string
407
408
  }
408
- node?: {
409
+ 'react-compiler'?: {
410
+ source?: string
411
+ default: string
412
+ }
413
+ 'node'?: {
409
414
  source?: string
410
415
  import?: string
411
416
  require?: string
412
417
  }
413
- types?: string
414
- source: string
415
- import?: string
416
- require?: string
417
- default: string
418
+ 'types'?: string
419
+ 'source': string
420
+ 'import'?: string
421
+ 'require'?: string
422
+ 'default': string
418
423
  }
419
424
 
420
425
  /** @public */
@@ -525,6 +530,18 @@ export declare const rollupLegacyTask: TaskHandler<RollupLegacyTask>
525
530
 
526
531
  export {RollupPlugin}
527
532
 
533
+ /** @internal */
534
+ export declare interface RollupReactCompilerTask {
535
+ type: 'build:react-compiler'
536
+ entries: RollupTaskEntry[]
537
+ runtime: 'browser'
538
+ format: 'esm'
539
+ target: string[]
540
+ }
541
+
542
+ /** @internal */
543
+ export declare const rollupReactCompilerTask: TaskHandler<RollupReactCompilerTask>
544
+
528
545
  /** @internal */
529
546
  export declare interface RollupTask {
530
547
  type: 'build:js'
package/dist/index.js CHANGED
@@ -111,6 +111,16 @@ const pkgSchema = z.object({
111
111
  require: z.optional(z.string())
112
112
  })
113
113
  ),
114
+ "react-compiler": z.optional(
115
+ // z.union([
116
+ // @TODO add support for a string shortcut
117
+ // z.string(),
118
+ z.object({
119
+ source: z.optional(z.string()),
120
+ default: z.string()
121
+ })
122
+ // ]),
123
+ ),
114
124
  node: z.optional(
115
125
  z.object({
116
126
  source: z.optional(z.string()),
@@ -237,7 +247,12 @@ function getTargetPaths(_type, expOrBundle) {
237
247
  expOrBundle.node.import.replace(fileEnding, type === "module" ? dtsEnding : mtsEnding)
238
248
  ), expOrBundle.node?.require && set.add(
239
249
  expOrBundle.node.require.replace(fileEnding, type === "commonjs" ? dtsEnding : ctsEnding)
240
- ))), Array.from(set);
250
+ )), expOrBundle["react-compiler"]?.source || expOrBundle["react-compiler"]?.default && set.add(
251
+ expOrBundle["react-compiler"].default.replace(
252
+ fileEnding,
253
+ type === "module" ? dtsEnding : mtsEnding
254
+ )
255
+ )), Array.from(set);
241
256
  }
242
257
  function isPkgExport$1(exp) {
243
258
  return exp?.browser || exp?.node || exp?.default;
@@ -596,6 +611,8 @@ async function resolveBuildContext(options) {
596
611
  exportEntry.browser?.require,
597
612
  exportEntry.node?.source && exportEntry.node.import,
598
613
  exportEntry.node?.source && exportEntry.node.require
614
+ // @TODO implement this
615
+ // exportEntry['react-compiler']?.source && exportEntry['react-compiler']?.default,
599
616
  ].filter(Boolean)).map((p) => path.resolve(cwd, p)), commonDistPath = findCommonDirPath(outputPaths);
600
617
  if (commonDistPath === cwd)
601
618
  throw new Error(
@@ -634,7 +651,13 @@ function resolveBuildTasks(ctx) {
634
651
  ), dtsTask2 = {
635
652
  type: "build:dts",
636
653
  entries: []
637
- }, rollupTasks = {}, rollupLegacyTasks = {};
654
+ }, rollupTasks = {}, rollupLegacyTasks = {}, rollupReactCompilerTask2 = {
655
+ type: "build:react-compiler",
656
+ runtime: "browser",
657
+ format: "esm",
658
+ entries: [],
659
+ target: target.browser
660
+ };
638
661
  function addRollupTaskEntry(format2, runtime, entry) {
639
662
  const buildId = `${format2}:${runtime}`;
640
663
  rollupTasks[buildId] ? rollupTasks[buildId].entries.push(entry) : rollupTasks[buildId] = {
@@ -658,6 +681,9 @@ function resolveBuildTasks(ctx) {
658
681
  target: target[runtime]
659
682
  };
660
683
  }
684
+ function addRollupReactCompilerTaskEntry(entry) {
685
+ rollupReactCompilerTask2.entries.push(entry);
686
+ }
661
687
  for (const exp of exports) {
662
688
  const importId = path.join(pkg.name, exp._path);
663
689
  exp.source?.endsWith(".ts") && dtsTask2.entries.push({
@@ -720,7 +746,15 @@ function resolveBuildTasks(ctx) {
720
746
  output
721
747
  });
722
748
  }
723
- if (config2?.legacyExports)
749
+ for (const exp of exports) {
750
+ const output = exp["react-compiler"]?.default;
751
+ output && addRollupReactCompilerTaskEntry({
752
+ path: exp._path,
753
+ source: exp["react-compiler"]?.source || exp.source,
754
+ output
755
+ });
756
+ }
757
+ if (rollupReactCompilerTask2.entries.length && tasks.push(rollupReactCompilerTask2), config2?.legacyExports)
724
758
  for (const exp of exports) {
725
759
  const runtime = exp.browser?.import ? "browser" : ctx.runtime, output = exp.browser?.import || exp.import;
726
760
  if (!output) continue;
@@ -1148,7 +1182,7 @@ function createConsoleSpy(options) {
1148
1182
  };
1149
1183
  }
1150
1184
  function resolveRollupConfig(ctx, buildTask) {
1151
- const { format: format2, runtime, target } = buildTask, { config: config2, cwd, exports: _exports, external, distPath, logger, pkg, ts: ts2 } = ctx, isLegacyExports = buildTask.type === "build:legacy", outputExt = isLegacyExports ? pkgExtMap.legacy : pkgExtMap[pkg.type || "commonjs"][format2], minify = config2?.minify ?? !1, outDir = path.relative(cwd, distPath), pathAliases = Object.fromEntries(
1185
+ const { format: format2, runtime, target } = buildTask, { config: config2, cwd, exports: _exports, external, distPath, logger, pkg, ts: ts2 } = ctx, isLegacyExports = buildTask.type === "build:legacy", isReactCompiler = buildTask.type === "build:react-compiler", outputExt = isLegacyExports ? pkgExtMap.legacy : pkgExtMap[pkg.type || "commonjs"][format2], minify = config2?.minify ?? !1, outDir = path.relative(cwd, distPath), pathAliases = Object.fromEntries(
1152
1186
  Object.entries(ts2.config?.options.paths || {}).map(([key, val]) => [key, path.resolve(cwd, ts2.config?.options.baseUrl || ".", val[0])])
1153
1187
  ), entries = buildTask.entries.map((entry) => ({
1154
1188
  ...entry,
@@ -1183,7 +1217,7 @@ function resolveRollupConfig(ctx, buildTask) {
1183
1217
  }),
1184
1218
  commonjs(),
1185
1219
  json(),
1186
- config2?.babel?.reactCompiler && babel({
1220
+ (isReactCompiler || config2?.babel?.reactCompiler) && babel({
1187
1221
  babelrc: !1,
1188
1222
  presets: ["@babel/preset-typescript"],
1189
1223
  babelHelpers: "bundled",
@@ -1227,7 +1261,7 @@ function resolveRollupConfig(ctx, buildTask) {
1227
1261
  }
1228
1262
  }
1229
1263
  })
1230
- ].filter(Boolean), userPlugins = config2?.rollup?.plugins, plugins = Array.isArray(userPlugins) ? defaultPlugins.concat(userPlugins) : resolveConfigProperty(config2?.rollup?.plugins, defaultPlugins), hashChunkFileNames = config2?.rollup?.hashChunkFileNames ?? !1, chunkFileNames = `${isLegacyExports ? "_legacy" : hashChunkFileNames ? "_chunks" : "_chunks-[format]"}/${hashChunkFileNames ? "[name]-[hash]" : "[name]"}${outputExt}`, entryFileNames = isLegacyExports ? "[name].js" : `[name]${outputExt}`;
1264
+ ].filter(Boolean), userPlugins = config2?.rollup?.plugins, plugins = Array.isArray(userPlugins) ? defaultPlugins.concat(userPlugins) : resolveConfigProperty(config2?.rollup?.plugins, defaultPlugins), hashChunkFileNames = config2?.rollup?.hashChunkFileNames ?? !1, chunkFileNames = `${isReactCompiler ? "_compiled" : isLegacyExports ? "_legacy" : hashChunkFileNames ? "_chunks" : "_chunks-[format]"}/${hashChunkFileNames ? "[name]-[hash]" : "[name]"}${outputExt}`, entryFileNames = isLegacyExports ? "[name].js" : `[name]${outputExt}`;
1231
1265
  return {
1232
1266
  inputOptions: {
1233
1267
  context: cwd,
@@ -1298,6 +1332,65 @@ const rollupLegacyTask = {
1298
1332
  ...targetLines,
1299
1333
  ...entriesLines
1300
1334
  ].join(`
1335
+ `);
1336
+ },
1337
+ exec: (ctx, task) => new Observable((observer) => {
1338
+ execPromise$2(ctx, task).then((result) => {
1339
+ observer.next(result), observer.complete();
1340
+ }).catch((err) => observer.error(err));
1341
+ }),
1342
+ complete: () => {
1343
+ },
1344
+ error: (_ctx, _task, err) => {
1345
+ console.error(err);
1346
+ }
1347
+ };
1348
+ async function execPromise$2(ctx, task) {
1349
+ const { distPath, files, logger } = ctx, outDir = path.relative(ctx.cwd, distPath), consoleSpy = createConsoleSpy({
1350
+ onRestored: (messages) => {
1351
+ for (const msg of messages) {
1352
+ const text = String(msg.args[0]);
1353
+ msg.code !== "CIRCULAR_DEPENDENCY" && (text.startsWith("Dynamic import can only") || text.startsWith("Sourcemap is likely to be incorrect") || (msg.type === "log" && logger.info(...msg.args), msg.type === "warn" && logger.warn(...msg.args), msg.type === "error" && logger.error(...msg.args)));
1354
+ }
1355
+ }
1356
+ });
1357
+ try {
1358
+ const { inputOptions, outputOptions } = resolveRollupConfig(ctx, task), bundle = await rollup({
1359
+ ...inputOptions,
1360
+ onwarn(warning) {
1361
+ consoleSpy.messages.push({
1362
+ type: "warn",
1363
+ code: warning.code,
1364
+ args: [warning.message]
1365
+ });
1366
+ }
1367
+ }), { output } = await bundle.generate(outputOptions);
1368
+ for (const chunkOrAsset of output)
1369
+ chunkOrAsset.type === "asset" ? files.push({
1370
+ type: "asset",
1371
+ path: path.resolve(outDir, chunkOrAsset.fileName)
1372
+ }) : files.push({
1373
+ type: "chunk",
1374
+ path: path.resolve(outDir, chunkOrAsset.fileName)
1375
+ });
1376
+ await bundle.write(outputOptions), await bundle.close(), consoleSpy.restore();
1377
+ } catch (err) {
1378
+ throw consoleSpy.restore(), err;
1379
+ }
1380
+ }
1381
+ const rollupReactCompilerTask = {
1382
+ name: (ctx, task) => {
1383
+ const entries = task.entries.filter((e) => !e.path.includes("__$$bundle_")), targetLines = task.target.length ? [" target:", ...task.target.map((t) => ` - ${chalk.yellow(t)}`)] : [], entriesLines = entries.length ? [
1384
+ " entries:",
1385
+ ...entries.map(
1386
+ (e) => [
1387
+ " - ",
1388
+ `${chalk.cyan(path.join(ctx.pkg.name, e.path))}: `,
1389
+ `${chalk.yellow(e.source)} ${chalk.gray("\u2192")} ${chalk.yellow(e.output)}`
1390
+ ].join("")
1391
+ )
1392
+ ] : [];
1393
+ return ["Build react compiler exports...", ...targetLines, ...entriesLines].join(`
1301
1394
  `);
1302
1395
  },
1303
1396
  exec: (ctx, task) => new Observable((observer) => {
@@ -1457,7 +1550,8 @@ const rollupWatchTask = {
1457
1550
  }, buildTaskHandlers = {
1458
1551
  "build:dts": dtsTask,
1459
1552
  "build:js": rollupTask,
1460
- "build:legacy": rollupLegacyTask
1553
+ "build:legacy": rollupLegacyTask,
1554
+ "build:react-compiler": rollupReactCompilerTask
1461
1555
  }, watchTaskHandlers = {
1462
1556
  "watch:dts": dtsWatchTask,
1463
1557
  "watch:js": rollupWatchTask
@@ -2226,6 +2320,7 @@ export {
2226
2320
  resolveBuildTasks,
2227
2321
  resolveConfigProperty,
2228
2322
  rollupLegacyTask,
2323
+ rollupReactCompilerTask,
2229
2324
  rollupTask,
2230
2325
  rollupWatchTask,
2231
2326
  strictOptions,