@sanity/pkg-utils 5.1.2 → 5.1.3

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.
Files changed (38) hide show
  1. package/README.md +1 -1
  2. package/dist/_chunks-cjs/build.cjs +36 -16
  3. package/dist/_chunks-cjs/build.cjs.map +1 -1
  4. package/dist/_chunks-cjs/check.cjs +4 -4
  5. package/dist/_chunks-cjs/check.cjs.map +1 -1
  6. package/dist/_chunks-cjs/consoleSpy.cjs +65 -40
  7. package/dist/_chunks-cjs/consoleSpy.cjs.map +1 -1
  8. package/dist/_chunks-cjs/handleError.cjs +3 -3
  9. package/dist/_chunks-cjs/index.cjs +77 -40
  10. package/dist/_chunks-cjs/index.cjs.map +1 -1
  11. package/dist/_chunks-cjs/init.cjs +3 -3
  12. package/dist/_chunks-cjs/logger.cjs +3 -3
  13. package/dist/_chunks-cjs/watch.cjs +8 -8
  14. package/dist/_chunks-cjs/watch.cjs.map +1 -1
  15. package/dist/cli.cjs +3 -3
  16. package/dist/index.cjs +2 -2
  17. package/dist/index.d.cts +21 -10
  18. package/dist/index.d.ts +21 -10
  19. package/dist/index.js +164 -84
  20. package/dist/index.js.map +1 -1
  21. package/package.json +2 -2
  22. package/src/node/build.ts +1 -3
  23. package/src/node/check.ts +2 -3
  24. package/src/node/core/contexts/buildContext.ts +4 -5
  25. package/src/node/core/pkg/parseExports.ts +35 -13
  26. package/src/node/core/pkg/pkgExt.ts +6 -19
  27. package/src/node/core/pkg/validateExports.ts +6 -5
  28. package/src/node/resolveBuildContext.ts +11 -11
  29. package/src/node/resolveBuildTasks.ts +49 -7
  30. package/src/node/tasks/dts/buildTypes.ts +5 -1
  31. package/src/node/tasks/dts/getTargetPaths.ts +6 -2
  32. package/src/node/tasks/index.ts +3 -2
  33. package/src/node/tasks/rollup/index.ts +1 -0
  34. package/src/node/tasks/rollup/resolveRollupConfig.ts +22 -31
  35. package/src/node/tasks/rollup/rollupLegacyTask.ts +144 -0
  36. package/src/node/tasks/rollup/rollupTask.ts +2 -2
  37. package/src/node/tasks/types.ts +16 -5
  38. package/src/node/watch.ts +3 -4
@@ -0,0 +1,144 @@
1
+ import chalk from 'chalk'
2
+ import path from 'path'
3
+ import {rollup} from 'rollup'
4
+ import {Observable} from 'rxjs'
5
+
6
+ import {createConsoleSpy} from '../../consoleSpy'
7
+ import type {BuildContext} from '../../core'
8
+ import type {RollupLegacyTask, TaskHandler} from '../types'
9
+ import {resolveRollupConfig} from './resolveRollupConfig'
10
+
11
+ /** @internal */
12
+ export const rollupLegacyTask: TaskHandler<RollupLegacyTask> = {
13
+ name: (ctx, task) => {
14
+ const entries = task.entries.filter((e) => !e.path.includes('__$$bundle_'))
15
+
16
+ const targetLines = task.target.length
17
+ ? [' target:', ...task.target.map((t) => ` - ${chalk.yellow(t)}`)]
18
+ : []
19
+
20
+ const entriesLines = entries.length
21
+ ? [
22
+ ' entries:',
23
+ ...entries.map((e) =>
24
+ [
25
+ ' - ',
26
+ `${chalk.cyan(path.join(ctx.pkg.name, e.path))}: `,
27
+ `${chalk.yellow(e.source)} ${chalk.gray('→')} ${chalk.yellow(e.output)}`,
28
+ ].join(''),
29
+ ),
30
+ ]
31
+ : []
32
+
33
+ // @TODO list out the root level files that are generated
34
+
35
+ return [
36
+ 'Build legacy exports...',
37
+ ` format: ${chalk.yellow(task.format)}`,
38
+ ...targetLines,
39
+ ...entriesLines,
40
+ ].join('\n')
41
+ },
42
+ exec: (ctx, task) => {
43
+ return new Observable((observer) => {
44
+ execPromise(ctx, task)
45
+ .then((result) => {
46
+ observer.next(result)
47
+ observer.complete()
48
+ })
49
+ .catch((err) => observer.error(err))
50
+ })
51
+ },
52
+ complete: () => {
53
+ //
54
+ },
55
+ error: (_ctx, _task, err) => {
56
+ // eslint-disable-next-line no-console
57
+ console.error(err)
58
+ },
59
+ }
60
+
61
+ async function execPromise(ctx: BuildContext, task: RollupLegacyTask) {
62
+ const {distPath, files, logger} = ctx
63
+ const outDir = path.relative(ctx.cwd, distPath)
64
+
65
+ // Prevent rollup from printing directly to the console
66
+ const consoleSpy = createConsoleSpy({
67
+ onRestored: (messages) => {
68
+ for (const msg of messages) {
69
+ const text = String(msg.args[0])
70
+
71
+ if (msg.code === 'CIRCULAR_DEPENDENCY') {
72
+ continue // ignore
73
+ }
74
+
75
+ if (text.startsWith('Dynamic import can only')) {
76
+ continue // ignore
77
+ }
78
+
79
+ if (text.startsWith('Sourcemap is likely to be incorrect')) {
80
+ continue // ignore
81
+ }
82
+
83
+ if (msg.type === 'log') {
84
+ logger.info(...msg.args)
85
+ }
86
+
87
+ if (msg.type === 'warn') {
88
+ logger.warn(...msg.args)
89
+ }
90
+
91
+ if (msg.type === 'error') {
92
+ logger.error(...msg.args)
93
+ }
94
+ }
95
+ },
96
+ })
97
+
98
+ try {
99
+ const {inputOptions, outputOptions} = resolveRollupConfig(ctx, task)
100
+
101
+ // Create bundle
102
+ const bundle = await rollup({
103
+ ...inputOptions,
104
+ onwarn(warning) {
105
+ consoleSpy.messages.push({
106
+ type: 'warn',
107
+ code: warning.code,
108
+ args: [warning.message],
109
+ })
110
+ },
111
+ })
112
+
113
+ // generate output specific code in-memory
114
+ // you can call this function multiple times on the same bundle object
115
+ const {output} = await bundle.generate(outputOptions)
116
+
117
+ for (const chunkOrAsset of output) {
118
+ if (chunkOrAsset.type === 'asset') {
119
+ files.push({
120
+ type: 'asset',
121
+ path: path.resolve(outDir, chunkOrAsset.fileName),
122
+ })
123
+ } else {
124
+ files.push({
125
+ type: 'chunk',
126
+ path: path.resolve(outDir, chunkOrAsset.fileName),
127
+ })
128
+ }
129
+ }
130
+
131
+ // or write the bundle to disk
132
+ await bundle.write(outputOptions)
133
+
134
+ // closes the bundle
135
+ await bundle.close()
136
+
137
+ // Restore console
138
+ consoleSpy.restore()
139
+ } catch (err) {
140
+ // Restore console
141
+ consoleSpy.restore()
142
+ throw err
143
+ }
144
+ }
@@ -4,8 +4,8 @@ import {rollup} from 'rollup'
4
4
  import {Observable} from 'rxjs'
5
5
 
6
6
  import {createConsoleSpy} from '../../consoleSpy'
7
- import {BuildContext} from '../../core'
8
- import {RollupTask, TaskHandler} from '../types'
7
+ import type {BuildContext} from '../../core'
8
+ import type {RollupTask, TaskHandler} from '../types'
9
9
  import {resolveRollupConfig} from './resolveRollupConfig'
10
10
 
11
11
  /** @internal */
@@ -1,8 +1,8 @@
1
- import {RollupWatcherEvent} from 'rollup'
2
- import {Observable} from 'rxjs'
1
+ import type {RollupWatcherEvent} from 'rollup'
2
+ import type {Observable} from 'rxjs'
3
3
 
4
- import {BuildContext, PkgRuntime} from '../core'
5
- import {DtsResult, DtsTask, DtsWatchTask} from './dts'
4
+ import type {BuildContext, PkgRuntime} from '../core'
5
+ import type {DtsResult, DtsTask, DtsWatchTask} from './dts'
6
6
 
7
7
  /** @internal */
8
8
  export interface RollupTaskEntry {
@@ -21,6 +21,16 @@ export interface RollupTask {
21
21
  target: string[]
22
22
  }
23
23
 
24
+ /** @internal */
25
+ export interface RollupLegacyTask {
26
+ type: 'build:legacy'
27
+ buildId: string
28
+ entries: RollupTaskEntry[]
29
+ runtime: PkgRuntime
30
+ format: 'esm'
31
+ target: string[]
32
+ }
33
+
24
34
  /** @internal */
25
35
  export interface RollupWatchTask {
26
36
  type: 'watch:js'
@@ -32,7 +42,7 @@ export interface RollupWatchTask {
32
42
  }
33
43
 
34
44
  /** @internal */
35
- export type BuildTask = DtsTask | RollupTask
45
+ export type BuildTask = DtsTask | RollupTask | RollupLegacyTask
36
46
 
37
47
  /** @internal */
38
48
  export type WatchTask = DtsWatchTask | RollupWatchTask
@@ -49,6 +59,7 @@ export type TaskHandler<Task, Result = void> = {
49
59
  export interface BuildTaskHandlers {
50
60
  'build:dts': TaskHandler<DtsTask, DtsResult>
51
61
  'build:js': TaskHandler<RollupTask>
62
+ 'build:legacy': TaskHandler<RollupLegacyTask>
52
63
  }
53
64
 
54
65
  /** @internal */
package/src/node/watch.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import path from 'path'
2
2
  import {switchMap} from 'rxjs'
3
3
 
4
- import {getPkgExtMap, loadConfig, loadPkgWithReporting} from './core'
4
+ import {loadConfig, loadPkgWithReporting} from './core'
5
5
  import {createLogger} from './logger'
6
6
  import {resolveBuildContext} from './resolveBuildContext'
7
7
  import {resolveWatchTasks} from './resolveWatchTasks'
8
- import {TaskHandler, WatchTask, watchTaskHandlers} from './tasks'
8
+ import {type TaskHandler, type WatchTask, watchTaskHandlers} from './tasks'
9
9
  import {watchConfigFiles} from './watchConfigFiles'
10
10
 
11
11
  /** @public */
@@ -32,10 +32,9 @@ export async function watch(options: {
32
32
 
33
33
  const pkg = await loadPkgWithReporting({cwd, logger, strict})
34
34
  const config = await loadConfig({cwd})
35
- const extMap = getPkgExtMap({legacyExports: config?.legacyExports ?? false})
36
35
  const tsconfig = tsconfigOption || config?.tsconfig || 'tsconfig.json'
37
36
 
38
- return resolveBuildContext({config, cwd, extMap, logger, pkg, strict, tsconfig})
37
+ return resolveBuildContext({config, cwd, logger, pkg, strict, tsconfig})
39
38
  }),
40
39
  )
41
40