@sanity/pkg-utils 4.4.1 → 5.0.1

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": "4.4.1",
3
+ "version": "5.0.1",
4
4
  "description": "Simple utilities for modern npm packages.",
5
5
  "keywords": [
6
6
  "sanity-io",
@@ -92,7 +92,7 @@
92
92
  "pretty-bytes": "^5.6.0",
93
93
  "prompts": "^2.4.2",
94
94
  "rimraf": "^4.4.1",
95
- "rollup": "^4.12.1",
95
+ "rollup": "^4.13.0",
96
96
  "rollup-plugin-esbuild": "^6.1.1",
97
97
  "rxjs": "^7.8.1",
98
98
  "treeify": "^1.1.0",
@@ -100,8 +100,8 @@
100
100
  "zod": "^3.22.4"
101
101
  },
102
102
  "devDependencies": {
103
- "@commitlint/cli": "^19.0.3",
104
- "@commitlint/config-conventional": "^19.0.3",
103
+ "@commitlint/cli": "^19.1.0",
104
+ "@commitlint/config-conventional": "^19.1.0",
105
105
  "@types/babel__core": "^7.20.5",
106
106
  "@types/cpx": "^1.5.5",
107
107
  "@types/find-config": "^1.0.4",
@@ -112,8 +112,8 @@
112
112
  "@types/rimraf": "^3.0.2",
113
113
  "@types/treeify": "^1.0.3",
114
114
  "@types/uuid": "^9.0.8",
115
- "@typescript-eslint/eslint-plugin": "^7.1.1",
116
- "@typescript-eslint/parser": "^7.1.1",
115
+ "@typescript-eslint/eslint-plugin": "^7.2.0",
116
+ "@typescript-eslint/parser": "^7.2.0",
117
117
  "browserslist-to-esbuild": "2.1.1",
118
118
  "commitizen": "^4.3.0",
119
119
  "cpx": "^1.5.0",
@@ -127,13 +127,13 @@
127
127
  "lint-staged": "^15.0.2",
128
128
  "npm-run-all2": "^6.1.2",
129
129
  "outdent": "^0.8.0",
130
- "recast": "^0.23.5",
130
+ "recast": "^0.23.6",
131
131
  "rollup-plugin-visualizer": "^5.12.0",
132
132
  "ts-node": "^10.9.2",
133
- "typescript": "^5.3.3",
133
+ "typescript": "^5.4.2",
134
134
  "vitest": "^1.3.1",
135
135
  "vitest-github-actions-reporter": "^0.11.1",
136
- "@sanity/pkg-utils": "4.4.1"
136
+ "@sanity/pkg-utils": "5.0.1"
137
137
  },
138
138
  "peerDependencies": {
139
139
  "typescript": "^4.7 || ^5"
@@ -33,28 +33,14 @@ export async function loadPkgWithReporting(options: {
33
33
  logger.error(`exports["${expPath}"]: the \`types\` property should be the first property`)
34
34
  }
35
35
 
36
- if (exp.node) {
37
- const nodeKeys = Object.keys(exp.node)
38
-
39
- if (!assertOrder('module', 'import', nodeKeys)) {
40
- shouldError = true
41
- logger.error(
42
- `exports["${expPath}"]: the \`node.module\` property should come before the \`node.import\` property`,
43
- )
44
- }
45
-
46
- if (!assertOrder('import', 'require', nodeKeys)) {
47
- logger.warn(
48
- `exports["${expPath}"]: the \`node.import\` property should come before the \`node.require\` property`,
49
- )
50
- }
51
-
52
- if (!assertOrder('module', 'require', nodeKeys)) {
53
- logger.warn(
54
- `exports["${expPath}"]: the \`node.module\` property should come before \`node.require\` property`,
55
- )
56
- }
36
+ if (exp.module) {
37
+ shouldError = true
38
+ logger.error(
39
+ `exports["${expPath}"]: the \`module\` condition shouldn't be used as it's not well supported in all bundlers.`,
40
+ )
41
+ }
57
42
 
43
+ if (exp.node) {
58
44
  if (exp.import && exp.node.import && !assertOrder('node', 'import', keys)) {
59
45
  shouldError = true
60
46
  logger.error(
@@ -62,23 +48,22 @@ export async function loadPkgWithReporting(options: {
62
48
  )
63
49
  }
64
50
 
65
- if (exp.module && exp.node.module && !assertOrder('node', 'module', keys)) {
51
+ if (exp.node.module) {
66
52
  shouldError = true
67
53
  logger.error(
68
- `exports["${expPath}"]: the \`node\` property should come before the \`module\` property`,
54
+ `exports["${expPath}"]: the \`node.module\` condition shouldn't be used as it's not well supported in all bundlers. A better strategy is to refactor the codebase to no longer be vulnerable to the "dual package hazard"`,
69
55
  )
70
56
  }
71
57
 
72
58
  if (
59
+ !exp.node.source &&
73
60
  exp.node.import &&
74
- !exp.node.require &&
75
- exp.node.module &&
76
- exp.import &&
77
- exp.node.module !== exp.import
61
+ (exp.node.require || exp.require) &&
62
+ (exp.node.import.endsWith('.cjs.js') || exp.node.import.endsWith('.cjs.mjs'))
78
63
  ) {
79
64
  shouldError = true
80
65
  logger.error(
81
- `exports["${expPath}"]: the \`node.module\` property should match \`import\``,
66
+ `exports["${expPath}"]: the \`node.import\` re-export pattern shouldn't be used as it's not well supported in all bundlers. A better strategy is to refactor the codebase to no longer be vulnerable to the "dual package hazard"`,
82
67
  )
83
68
  }
84
69
 
@@ -99,12 +84,6 @@ export async function loadPkgWithReporting(options: {
99
84
  `exports["${expPath}"]: the \`import\` property should come before the \`require\` property`,
100
85
  )
101
86
  }
102
-
103
- if (!assertOrder('module', 'import', keys)) {
104
- logger.warn(
105
- `exports["${expPath}"]: the \`module\` property should come before \`import\` property`,
106
- )
107
- }
108
87
  }
109
88
 
110
89
  if (!assertLast('default', keys)) {
@@ -33,12 +33,10 @@ const pkgSchema = z.object({
33
33
  node: z.optional(
34
34
  z.object({
35
35
  source: z.optional(z.string()),
36
- module: z.optional(z.string()),
37
36
  import: z.optional(z.string()),
38
37
  require: z.optional(z.string()),
39
38
  }),
40
39
  ),
41
- module: z.optional(z.string()),
42
40
  import: z.optional(z.string()),
43
41
  require: z.optional(z.string()),
44
42
  default: z.string(),
@@ -2,7 +2,7 @@ import fs from 'fs'
2
2
  import path from 'path'
3
3
 
4
4
  import {BuildContext, PkgExport, PkgFormat, PkgRuntime} from './core'
5
- import {BuildTask, DtsTask, NodeReExportFromCJSTask, RollupTask, RollupTaskEntry} from './tasks'
5
+ import {BuildTask, DtsTask, RollupTask, RollupTaskEntry} from './tasks'
6
6
 
7
7
  /** @internal */
8
8
  export function resolveBuildTasks(ctx: BuildContext): BuildTask[] {
@@ -156,33 +156,5 @@ export function resolveBuildTasks(ctx: BuildContext): BuildTask[] {
156
156
  }
157
157
  }
158
158
 
159
- const nodeReexportCjsEntries: NodeReExportFromCJSTask['entries'] = []
160
-
161
- // Parse tasks to re-export from CommonJS for Node.js
162
- for (const exp of exports) {
163
- if (
164
- exp.node &&
165
- !exp.node.source &&
166
- exp.node.import &&
167
- (exp.node.require || exp.require) &&
168
- (exp.node.import.endsWith('.cjs.js') || exp.node.import.endsWith('.cjs.mjs'))
169
- ) {
170
- const importId = path.join(pkg.name, exp._path)
171
-
172
- nodeReexportCjsEntries.push({
173
- importId,
174
- import: exp.node.import,
175
- require: (exp.node.require || exp.require)!,
176
- })
177
- }
178
- }
179
-
180
- if (nodeReexportCjsEntries.length) {
181
- tasks.push({
182
- type: 'node:reexport-cjs',
183
- entries: nodeReexportCjsEntries,
184
- })
185
- }
186
-
187
159
  return tasks
188
160
  }
@@ -1,5 +1,4 @@
1
1
  import {dtsTask, dtsWatchTask} from './dts'
2
- import {nodeReexportCjsTask} from './node/reexportCjsTask'
3
2
  import {rollupTask, rollupWatchTask} from './rollup'
4
3
  import {BuildTaskHandlers, WatchTaskHandlers} from './types'
5
4
 
@@ -11,7 +10,6 @@ export * from './types'
11
10
  export const buildTaskHandlers: BuildTaskHandlers = {
12
11
  'build:dts': dtsTask,
13
12
  'build:js': rollupTask,
14
- 'node:reexport-cjs': nodeReexportCjsTask,
15
13
  }
16
14
 
17
15
  /** @internal */
@@ -32,13 +32,7 @@ export interface RollupWatchTask {
32
32
  }
33
33
 
34
34
  /** @internal */
35
- export interface NodeReExportFromCJSTask {
36
- type: 'node:reexport-cjs'
37
- entries: {importId: string; import: string; require: string}[]
38
- }
39
-
40
- /** @internal */
41
- export type BuildTask = DtsTask | RollupTask | NodeReExportFromCJSTask
35
+ export type BuildTask = DtsTask | RollupTask
42
36
 
43
37
  /** @internal */
44
38
  export type WatchTask = DtsWatchTask | RollupWatchTask
@@ -55,7 +49,6 @@ export type TaskHandler<Task, Result = void> = {
55
49
  export interface BuildTaskHandlers {
56
50
  'build:dts': TaskHandler<DtsTask, DtsResult>
57
51
  'build:js': TaskHandler<RollupTask>
58
- 'node:reexport-cjs': TaskHandler<NodeReExportFromCJSTask>
59
52
  }
60
53
 
61
54
  /** @internal */
@@ -1,87 +0,0 @@
1
- import chalk from 'chalk'
2
- import {writeFile} from 'fs/promises'
3
- import {mkdirp} from 'mkdirp'
4
- import path from 'path'
5
- import {from} from 'rxjs'
6
-
7
- import {BuildContext} from '../../core'
8
- import {NodeReExportFromCJSTask, TaskHandler} from '../types'
9
-
10
- /** @internal */
11
- export const nodeReexportCjsTask: TaskHandler<NodeReExportFromCJSTask> = {
12
- name: (_ctx, task) =>
13
- [
14
- `Re-export CommonJS module in ESM wrapper…`,
15
- ` entries:`,
16
- ...task.entries.map((entry) =>
17
- [
18
- ` - ${chalk.cyan(entry.importId)}: `,
19
- `${chalk.yellow(entry.require)} ${chalk.gray('→')} ${chalk.yellow(entry.import)}`,
20
- ].join(''),
21
- ),
22
- ].join('\n'),
23
- exec: (ctx, task) => {
24
- return from(exec(ctx, task))
25
- },
26
- complete: () => {
27
- //
28
- },
29
- error: (_ctx, _task, err) => {
30
- // eslint-disable-next-line no-console
31
- console.error(err)
32
- },
33
- }
34
-
35
- async function exec(ctx: BuildContext, task: NodeReExportFromCJSTask) {
36
- const {cwd} = ctx
37
-
38
- for (const entry of task.entries) {
39
- const sourcePath = path.resolve(cwd, entry.require)
40
- const targetPath = path.resolve(cwd, entry.import)
41
- const targetDir = path.dirname(targetPath)
42
-
43
- await mkdirp(targetDir)
44
-
45
- // eslint-disable-next-line @typescript-eslint/no-var-requires
46
- const mod = require(sourcePath)
47
-
48
- let relativeImport = `./${path.relative(targetDir, sourcePath)}`
49
-
50
- // Strip leading "./" from relative import
51
- if (relativeImport.startsWith('./.')) {
52
- relativeImport = relativeImport.slice(2)
53
- }
54
-
55
- const code = compileESMWrapper(mod, relativeImport)
56
-
57
- await writeFile(targetPath, code, 'utf8')
58
- }
59
- }
60
-
61
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
62
- function compileESMWrapper(mod: any, relativeImport: string) {
63
- const keys = Object.keys(mod).filter((k) => k !== '__esModule')
64
-
65
- let code = `import cjs from '${relativeImport}';\n`
66
-
67
- if (keys.length) {
68
- code += `\n`
69
- code += keys
70
- .filter((k) => (mod.__esModule ? k !== 'default' : true))
71
- .map((k) => `export const ${k} = cjs.${k};\n`)
72
- .join('')
73
- }
74
-
75
- code += `\n`
76
-
77
- if (mod.__esModule) {
78
- // If there is no default export, we shouldn't add it as it'll be undefined
79
- if ('default' in mod) {
80
- code += `export default cjs.default;\n`
81
- }
82
- } else {
83
- code += `export default cjs;\n`
84
- }
85
-
86
- return code
87
- }