@sanity/pkg-utils 5.1.5 → 5.1.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/pkg-utils",
3
- "version": "5.1.5",
3
+ "version": "5.1.7",
4
4
  "description": "Simple utilities for modern npm packages.",
5
5
  "keywords": [
6
6
  "sanity-io",
@@ -132,7 +132,7 @@
132
132
  "typescript": "5.4.2",
133
133
  "vitest": "^1.4.0",
134
134
  "vitest-github-actions-reporter": "^0.11.1",
135
- "@sanity/pkg-utils": "5.1.5"
135
+ "@sanity/pkg-utils": "5.1.7"
136
136
  },
137
137
  "peerDependencies": {
138
138
  "typescript": "5.4.2"
package/src/global.ts CHANGED
@@ -3,4 +3,4 @@ declare global {
3
3
  var __DEV__: boolean
4
4
  }
5
5
 
6
- export {}
6
+ export type {}
package/src/node/build.ts CHANGED
@@ -33,9 +33,10 @@ export async function build(options: {
33
33
  const {cwd, emitDeclarationOnly, strict = false, tsconfig: tsconfigOption} = options
34
34
  const logger = createLogger()
35
35
 
36
- const pkg = await loadPkgWithReporting({cwd, logger, strict})
37
-
38
36
  const config = await loadConfig({cwd})
37
+ const legacyExports = config?.legacyExports ?? false
38
+ const pkg = await loadPkgWithReporting({cwd, logger, strict, legacyExports})
39
+
39
40
  const tsconfig = tsconfigOption || config?.tsconfig || 'tsconfig.json'
40
41
 
41
42
  const ctx = await resolveBuildContext({
package/src/node/check.ts CHANGED
@@ -17,8 +17,9 @@ export async function check(options: {
17
17
  const {cwd, strict = false, tsconfig: tsconfigOption} = options
18
18
  const logger = createLogger()
19
19
 
20
- const pkg = await loadPkgWithReporting({cwd, logger, strict})
21
20
  const config = await loadConfig({cwd})
21
+ const legacyExports = config?.legacyExports ?? false
22
+ const pkg = await loadPkgWithReporting({cwd, logger, strict, legacyExports})
22
23
  const tsconfig = tsconfigOption || config?.tsconfig || 'tsconfig.json'
23
24
  const ctx = await resolveBuildContext({config, cwd, logger, pkg, strict, tsconfig})
24
25
 
@@ -11,18 +11,28 @@ export async function loadPkgWithReporting(options: {
11
11
  cwd: string
12
12
  logger: Logger
13
13
  strict: boolean
14
+ legacyExports: boolean
14
15
  }): Promise<PackageJSON> {
15
- const {cwd, logger, strict} = options
16
+ const {cwd, logger, strict, legacyExports} = options
16
17
 
17
18
  try {
18
19
  const pkg = await loadPkg({cwd})
19
20
  let shouldError = false
20
21
 
21
- if (strict && !pkg.type) {
22
- shouldError = true
23
- logger.error(
24
- `the \`type\` field in \`./package.json\` must be either "module" or "commonjs")`,
25
- )
22
+ if (strict) {
23
+ if (legacyExports && pkg.type === 'commonjs') {
24
+ shouldError = true
25
+ logger.error(
26
+ `the \`type\` field in \`./package.json\` shouldn't be "commonjs" when \`legacyExports\` is set to true)`,
27
+ )
28
+ }
29
+
30
+ if (!legacyExports && !pkg.type) {
31
+ shouldError = true
32
+ logger.error(
33
+ `the \`type\` field in \`./package.json\` must be either "module" or "commonjs")`,
34
+ )
35
+ }
26
36
  }
27
37
 
28
38
  // validate exports
@@ -7,7 +7,8 @@ export function validateExports(
7
7
  options: {pkg: PackageJSON},
8
8
  ): string[] {
9
9
  const {pkg} = options
10
- const ext = extMap[pkg.type || 'commonjs']
10
+ const type = pkg.type || 'commonjs'
11
+ const ext = extMap[type]
11
12
 
12
13
  const errors: string[] = []
13
14
 
@@ -15,13 +16,13 @@ export function validateExports(
15
16
  for (const exp of _exports) {
16
17
  if (exp.require && !exp.require.endsWith(ext.commonjs)) {
17
18
  errors.push(
18
- `package.json with \`type: "${pkg.type}"\` - \`exports["${exp._path}"].require\` must end with "${ext.commonjs}"`,
19
+ `package.json with \`type: "${type}"\` - \`exports["${exp._path}"].require\` must end with "${ext.commonjs}"`,
19
20
  )
20
21
  }
21
22
 
22
23
  if (exp.import && !exp.import.endsWith(ext.esm)) {
23
24
  errors.push(
24
- `package.json with \`type: "${pkg.type}"\` - \`exports["${exp._path}"].import\` must end with "${ext.esm}"`,
25
+ `package.json with \`type: "${type}"\` - \`exports["${exp._path}"].import\` must end with "${ext.esm}"`,
25
26
  )
26
27
  }
27
28
  }
package/src/node/watch.ts CHANGED
@@ -30,8 +30,9 @@ export async function watch(options: {
30
30
  throw new Error('missing package.json')
31
31
  }
32
32
 
33
- const pkg = await loadPkgWithReporting({cwd, logger, strict})
34
33
  const config = await loadConfig({cwd})
34
+ const legacyExports = config?.legacyExports ?? false
35
+ const pkg = await loadPkgWithReporting({cwd, logger, strict, legacyExports})
35
36
  const tsconfig = tsconfigOption || config?.tsconfig || 'tsconfig.json'
36
37
 
37
38
  return resolveBuildContext({config, cwd, logger, pkg, strict, tsconfig})