@sanity/pkg-utils 6.4.0 → 6.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.
package/dist/index.d.ts CHANGED
@@ -8,6 +8,7 @@ import type {Plugin as RollupPlugin} from 'rollup'
8
8
  import {RollupWatcherEvent} from 'rollup'
9
9
  import type {TreeshakingOptions} from 'rollup'
10
10
  import ts from 'typescript'
11
+ import {z} from 'zod'
11
12
 
12
13
  /**
13
14
  * Build the distribution files of a npm package.
@@ -145,6 +146,11 @@ export declare function getExtractMessagesConfig(options: {
145
146
  rules: NonNullable<PkgConfigOptions['extract']>['rules']
146
147
  }): IExtractorMessagesConfig
147
148
 
149
+ /**
150
+ * @public
151
+ */
152
+ export declare type InferredStrictOptions = z.infer<typeof strictOptions>
153
+
148
154
  /** @public */
149
155
  export declare function init(options: {cwd: string; path: string}): Promise<void>
150
156
 
@@ -260,7 +266,9 @@ export declare interface PackageJSON {
260
266
  export declare function parseExports(options: {
261
267
  pkg: PackageJSON
262
268
  strict: boolean
269
+ strictOptions: InferredStrictOptions
263
270
  legacyExports: boolean
271
+ logger: Logger
264
272
  }): (PkgExport & {
265
273
  _path: string
266
274
  })[]
@@ -365,6 +373,10 @@ export declare interface PkgConfigOptions {
365
373
  */
366
374
  src?: string
367
375
  tsconfig?: string
376
+ /**
377
+ * Configure what checks are made when running `--strict` builds and checks
378
+ */
379
+ strictOptions?: StrictOptions
368
380
  }
369
381
 
370
382
  /** @public */
@@ -535,6 +547,60 @@ export declare interface RollupWatchTask {
535
547
  /** @internal */
536
548
  export declare const rollupWatchTask: TaskHandler<RollupWatchTask, RollupWatcherEvent>
537
549
 
550
+ /**
551
+ * @public
552
+ */
553
+ export declare interface StrictOptions {
554
+ /**
555
+ * Disallows a top level `typings` field in `package.json` if it is equal to `exports['.'].source`.
556
+ * @defaultValue 'error'
557
+ */
558
+ noPackageJsonTypings?: ToggleType
559
+ /**
560
+ * Requires specifying `sideEffects` in `package.json`.
561
+ * @defaultValue 'warn'
562
+ */
563
+ noImplicitSideEffects?: ToggleType
564
+ /**
565
+ * Requires specifying `browserslist` in `package.json`, instead of relying on it implicitly being:
566
+ * @example
567
+ * ```
568
+ * "browserslist": "extends @sanity/browserslist-config"
569
+ * ```
570
+ * @defaultValue 'warn'
571
+ */
572
+ noImplicitBrowsersList?: ToggleType
573
+ }
574
+
575
+ /**
576
+ * @public
577
+ */
578
+ export declare const strictOptions: z.ZodObject<
579
+ {
580
+ noPackageJsonTypings: z.ZodDefault<
581
+ z.ZodUnion<[z.ZodLiteral<'error'>, z.ZodLiteral<'warn'>, z.ZodLiteral<'off'>]>
582
+ >
583
+ noImplicitSideEffects: z.ZodDefault<
584
+ z.ZodUnion<[z.ZodLiteral<'error'>, z.ZodLiteral<'warn'>, z.ZodLiteral<'off'>]>
585
+ >
586
+ noImplicitBrowsersList: z.ZodDefault<
587
+ z.ZodUnion<[z.ZodLiteral<'error'>, z.ZodLiteral<'warn'>, z.ZodLiteral<'off'>]>
588
+ >
589
+ },
590
+ 'strict',
591
+ z.ZodTypeAny,
592
+ {
593
+ noPackageJsonTypings: 'error' | 'warn' | 'off'
594
+ noImplicitSideEffects: 'error' | 'warn' | 'off'
595
+ noImplicitBrowsersList: 'error' | 'warn' | 'off'
596
+ },
597
+ {
598
+ noPackageJsonTypings?: 'error' | 'warn' | 'off' | undefined
599
+ noImplicitSideEffects?: 'error' | 'warn' | 'off' | undefined
600
+ noImplicitBrowsersList?: 'error' | 'warn' | 'off' | undefined
601
+ }
602
+ >
603
+
538
604
  /** @internal */
539
605
  export declare type TaskHandler<Task, Result = void> = {
540
606
  name: (ctx: BuildContext, task: Task) => string
@@ -543,6 +609,18 @@ export declare type TaskHandler<Task, Result = void> = {
543
609
  error: (ctx: BuildContext, task: Task, error: unknown) => void
544
610
  }
545
611
 
612
+ /**
613
+ * @public
614
+ */
615
+ export declare const toggle: z.ZodUnion<
616
+ [z.ZodLiteral<'error'>, z.ZodLiteral<'warn'>, z.ZodLiteral<'off'>]
617
+ >
618
+
619
+ /**
620
+ * @public
621
+ */
622
+ export declare type ToggleType = z.infer<typeof toggle>
623
+
546
624
  /** @public */
547
625
  export declare interface TSDocCustomTag {
548
626
  name: string
package/dist/index.js CHANGED
@@ -10,6 +10,7 @@ import fs, { writeFile, readFile, readdir, lstat } from "node:fs/promises";
10
10
  import browserslist from "browserslist";
11
11
  import config from "@sanity/browserslist-config";
12
12
  import ts from "typescript";
13
+ import { errorMap } from "zod-validation-error";
13
14
  import { Observable, switchMap } from "rxjs";
14
15
  import { ExtractorConfig, Extractor } from "@microsoft/api-extractor";
15
16
  import { mkdirp } from "mkdirp";
@@ -269,7 +270,9 @@ function validateExports(_exports, options2) {
269
270
  return errors;
270
271
  }
271
272
  function parseExports(options2) {
272
- const { pkg, strict, legacyExports } = options2, type = pkg.type || "commonjs", errors = [];
273
+ const { pkg, strict, strictOptions: strictOptions2, legacyExports, logger } = options2, type = pkg.type || "commonjs", errors = [], report = (kind, message) => {
274
+ kind === "warn" ? logger.warn(message) : errors.push(message);
275
+ };
273
276
  if (pkg.source) {
274
277
  if (strict && pkg.exports?.["."] && typeof pkg.exports["."] == "object" && "source" in pkg.exports["."] && pkg.exports["."].source === pkg.source)
275
278
  errors.push(
@@ -345,7 +348,7 @@ function parseExports(options2) {
345
348
  - `)
346
349
  );
347
350
  const _exports = [];
348
- strict && "typings" in pkg && errors.push("package.json: `typings` should be `types`"), strict && !pkg.types && pkg.source?.endsWith(".ts") && errors.push(
351
+ strict && strictOptions2.noPackageJsonTypings !== "off" && "typings" in pkg && report(strictOptions2.noPackageJsonTypings, "package.json: `typings` should be `types`"), strict && !pkg.types && pkg.source?.endsWith(".ts") && errors.push(
349
352
  "package.json: `types` must be declared for the npm listing to show as a TypeScript module."
350
353
  ), strict && !pkg.exports["./package.json"] && errors.push('package.json: `exports["./package.json"] must be declared.');
351
354
  for (const [exportPath, exportEntry] of Object.entries(pkg.exports))
@@ -520,6 +523,16 @@ function resolveNodeTarget(versions) {
520
523
  if (target.length !== 0)
521
524
  return target;
522
525
  }
526
+ const toggle = z.union([z.literal("error"), z.literal("warn"), z.literal("off")]), strictOptions = z.object({
527
+ noPackageJsonTypings: toggle.default("error"),
528
+ noImplicitSideEffects: toggle.default("warn"),
529
+ noImplicitBrowsersList: toggle.default("warn")
530
+ }).strict(), validationSchema = z.object({
531
+ strictOptions: strictOptions.default({})
532
+ });
533
+ function parseStrictOptions(input) {
534
+ return validationSchema.parse({ strictOptions: input }, { errorMap }).strictOptions;
535
+ }
523
536
  async function resolveBuildContext(options2) {
524
537
  const {
525
538
  config: config2,
@@ -529,15 +542,27 @@ async function resolveBuildContext(options2) {
529
542
  pkg,
530
543
  strict,
531
544
  tsconfig: tsconfigPath
532
- } = options2, tsconfig = await loadTSConfig({ cwd, tsconfigPath });
545
+ } = options2, tsconfig = await loadTSConfig({ cwd, tsconfigPath }), strictOptions2 = parseStrictOptions(config2?.strictOptions ?? {});
533
546
  let browserslist2 = pkg.browserslist;
534
- browserslist2 || (strict && logger.warn(
535
- 'Could not detect a `browserslist` property in `package.json`, using default configuration. Add `"browserslist": "extends @sanity/browserslist-config"` to silence this warning.'
536
- ), browserslist2 = DEFAULT_BROWSERSLIST_QUERY);
547
+ if (!browserslist2) {
548
+ if (strict && strictOptions2.noImplicitBrowsersList !== "off") {
549
+ if (strictOptions2.noImplicitBrowsersList === "error")
550
+ throw new Error(
551
+ '\n- package.json: "browserslist" is missing, set it to `"browserslist": "extends @sanity/browserslist-config"`'
552
+ );
553
+ logger.warn(
554
+ 'Could not detect a `browserslist` property in `package.json`, using default configuration. Add `"browserslist": "extends @sanity/browserslist-config"` to silence this warning.'
555
+ );
556
+ }
557
+ browserslist2 = DEFAULT_BROWSERSLIST_QUERY;
558
+ }
537
559
  const targetVersions = browserslistToEsbuild(browserslist2);
538
- strict && typeof pkg.sideEffects > "u" && logger.error(
539
- 'No `sideEffects` field in `package.json`, assuming all files are side-effectful. Add `"sideEffects": true` to silence this warning. See https://webpack.js.org/guides/tree-shaking/#clarifying-tree-shaking-and-sideeffects for how to define `sideEffects`.'
540
- );
560
+ if (strict && strictOptions2.noImplicitSideEffects !== "off" && typeof pkg.sideEffects > "u") {
561
+ const msg = "package.json: `sideEffects` is missing, see https://webpack.js.org/guides/tree-shaking/#clarifying-tree-shaking-and-sideeffects for how to define `sideEffects`";
562
+ if (strictOptions2.noImplicitSideEffects === "error")
563
+ throw new Error(msg);
564
+ logger.warn(msg);
565
+ }
541
566
  const nodeTarget = resolveNodeTarget(targetVersions), webTarget = resolveBrowserTarget(targetVersions);
542
567
  if (!nodeTarget)
543
568
  throw new Error("no matching `node` target");
@@ -550,7 +575,9 @@ async function resolveBuildContext(options2) {
550
575
  }, parsedExports = parseExports({
551
576
  pkg,
552
577
  strict,
553
- legacyExports: config2?.legacyExports ?? !1
578
+ legacyExports: config2?.legacyExports ?? !1,
579
+ strictOptions: strictOptions2,
580
+ logger
554
581
  }).reduce((acc, x) => {
555
582
  const { _path: exportPath, ...exportEntry } = x;
556
583
  return { ...acc, [exportPath]: exportEntry };
@@ -20794,7 +20821,7 @@ async function build(options2) {
20794
20821
  tsconfig
20795
20822
  });
20796
20823
  clean && (logger.log(
20797
- `Cleaning the contents of the \`dist\` folder: './${path$1.relative(cwd, ctx.distPath)}'`
20824
+ `Deleting the \`dist\` folder: './${path$1.relative(cwd, ctx.distPath)}' before building...`
20798
20825
  ), await rimraf(ctx.distPath));
20799
20826
  const buildTasks = resolveBuildTasks(ctx);
20800
20827
  for (const task of buildTasks) {
@@ -21585,6 +21612,8 @@ export {
21585
21612
  rollupLegacyTask,
21586
21613
  rollupTask,
21587
21614
  rollupWatchTask,
21615
+ strictOptions,
21616
+ toggle,
21588
21617
  watch,
21589
21618
  watchTaskHandlers
21590
21619
  };