@vitus-labs/tools-rolldown 1.15.5 → 2.0.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.
@@ -1,218 +0,0 @@
1
- import { CONFIG, PKG } from '../config/index.js'
2
-
3
- const isESModuleOnly = PKG.type === 'module'
4
-
5
- const hasDifferentNativeBuild = () => {
6
- return PKG['react-native'] !== PKG.module
7
- }
8
-
9
- const hasDifferentBrowserBuild = (type: string) => {
10
- if (!PKG.browser) return false
11
-
12
- return Object.entries(PKG.browser as Record<string, string>).some(
13
- ([key, value]) => {
14
- const source = key.substring(2)
15
- const output = value.substring(2)
16
-
17
- return source !== PKG[type] && source !== output
18
- },
19
- )
20
- }
21
-
22
- const BUILD_VARIANTS: Record<
23
- string,
24
- { format: string; env: string; platform?: string }
25
- > = {
26
- main: {
27
- format: isESModuleOnly ? 'es' : 'cjs',
28
- env: 'development',
29
- platform: 'universal',
30
- },
31
- module: {
32
- format: 'es',
33
- env: 'development',
34
- platform: 'universal',
35
- },
36
- 'react-native': {
37
- format: 'es',
38
- env: 'development',
39
- platform: 'native',
40
- },
41
- 'umd:main': { format: 'umd', env: 'development' },
42
- unpkg: { format: 'umd', env: 'production' },
43
- }
44
-
45
- /** Check if an exports object uses subpath keys (e.g. ".", "./devtools") */
46
- const isSubpathExports = (obj: Record<string, any>): boolean =>
47
- Object.keys(obj).some((k) => k === '.' || k.startsWith('./'))
48
-
49
- /** Resolve the source input file for a subpath export using convention:
50
- * "." → "src/index.ts", "./devtools" → "src/devtools" */
51
- const resolveSubpathInput = (exportPath: string): string => {
52
- if (exportPath === '.') return `${CONFIG.sourceDir}/index.ts`
53
- const subpath = exportPath.slice(2) // strip "./"
54
- return `${CONFIG.sourceDir}/${subpath}`
55
- }
56
-
57
- /** Extract build variants from a single export's condition object */
58
- const parseConditions = (
59
- conditions: Record<string, any>,
60
- input?: string,
61
- ): Record<string, any>[] => {
62
- const result: Record<string, any>[] = []
63
- const base = input ? { input } : {}
64
-
65
- if (conditions.import) {
66
- result.push({ file: conditions.import, ...BUILD_VARIANTS.module, ...base })
67
- }
68
- if (conditions.require) {
69
- result.push({
70
- file: conditions.require,
71
- format: 'cjs',
72
- env: 'development',
73
- platform: 'universal',
74
- ...base,
75
- })
76
- }
77
- if (conditions.node) {
78
- result.push({
79
- file: conditions.node,
80
- ...BUILD_VARIANTS.module,
81
- platform: 'node',
82
- ...base,
83
- })
84
- }
85
- if (conditions.default && !conditions.import) {
86
- result.push({ file: conditions.default, ...BUILD_VARIANTS.module, ...base })
87
- }
88
-
89
- return result
90
- }
91
-
92
- /** Parse subpath exports object into build variants */
93
- const parseSubpathExports = (
94
- exportsOptions: Record<string, any>,
95
- ): Record<string, any>[] => {
96
- const result: Record<string, any>[] = []
97
-
98
- for (const [exportPath, exportConfig] of Object.entries(exportsOptions)) {
99
- if (typeof exportConfig === 'string') {
100
- // Skip passthrough exports (e.g. "./package.json": "./package.json")
101
- if (!exportConfig.endsWith('.js') && !exportConfig.endsWith('.mjs')) {
102
- continue
103
- }
104
- result.push({
105
- file: exportConfig,
106
- input: resolveSubpathInput(exportPath),
107
- ...BUILD_VARIANTS.module,
108
- })
109
- } else if (typeof exportConfig === 'object' && exportConfig !== null) {
110
- // Skip exports without build conditions (import/require/node/default)
111
- if (
112
- !exportConfig.import &&
113
- !exportConfig.require &&
114
- !exportConfig.node &&
115
- !exportConfig.default
116
- ) {
117
- continue
118
- }
119
- const input = resolveSubpathInput(exportPath)
120
- result.push(...parseConditions(exportConfig, input))
121
- }
122
- }
123
-
124
- return result
125
- }
126
-
127
- const getExportsOptions = () => {
128
- const exportsOptions = PKG.exports
129
-
130
- if (!exportsOptions) return []
131
-
132
- if (typeof exportsOptions === 'string') {
133
- return [{ file: PKG.exports, ...BUILD_VARIANTS.module }]
134
- }
135
-
136
- if (typeof exportsOptions === 'object') {
137
- return isSubpathExports(exportsOptions)
138
- ? parseSubpathExports(exportsOptions)
139
- : parseConditions(exportsOptions)
140
- }
141
-
142
- return []
143
- }
144
-
145
- const createBasicBuildVariants = () => {
146
- let result: Record<string, any>[] = []
147
-
148
- if (isESModuleOnly) result = [...getExportsOptions()]
149
-
150
- Object.keys(BUILD_VARIANTS).forEach((key) => {
151
- const PKGOutDir = PKG[key]
152
-
153
- if (PKGOutDir) {
154
- const hasBrowserBuild = hasDifferentBrowserBuild(key)
155
- const hasNativeBuild = hasDifferentNativeBuild()
156
-
157
- // create a helper function for adding a build variant to an array
158
- const add = (props = {}) => {
159
- result.push({ ...BUILD_VARIANTS[key], file: PKGOutDir, ...props })
160
- }
161
-
162
- if (key === 'react-native') {
163
- // add a separate RN build only if output path differs from module path
164
- if (hasNativeBuild) {
165
- add()
166
- }
167
- } else if (hasBrowserBuild) {
168
- // if has a different browser build, set default platform to node
169
- // as there is going to be created a separate browser build as well
170
- add({ platform: 'node' })
171
- } else {
172
- add()
173
- }
174
- }
175
- })
176
-
177
- return result
178
- }
179
-
180
- const createBrowserBuildVariants = () => {
181
- const result: Record<string, any>[] = []
182
- if (!PKG.browser) return result
183
-
184
- Object.entries(PKG.browser as Record<string, string>).forEach(
185
- ([key, value]) => {
186
- const source = key.substring(2) // strip './' from the beginning of path
187
- const output = value.substring(2) // strip './' from the beginning of path
188
-
189
- Object.keys(BUILD_VARIANTS).forEach((item) => {
190
- if (PKG[item] === source && source !== output) {
191
- result.push({
192
- ...BUILD_VARIANTS[item],
193
- file: output,
194
- platform: 'browser',
195
- })
196
- }
197
- })
198
- },
199
- )
200
-
201
- return result
202
- }
203
-
204
- const createBuildPipeline = () => {
205
- if (Array.isArray(CONFIG.entries) && CONFIG.entries.length > 0) {
206
- return CONFIG.entries.map((entry: Record<string, string | undefined>) => ({
207
- format: entry.format || 'es',
208
- env: entry.env || 'development',
209
- platform: entry.platform || 'universal',
210
- file: entry.file,
211
- input: entry.input,
212
- }))
213
- }
214
-
215
- return [...createBasicBuildVariants(), ...createBrowserBuildVariants()]
216
- }
217
-
218
- export default createBuildPipeline
@@ -1,4 +0,0 @@
1
- import config, { buildAllDts, buildDts } from './config.js'
2
- import createBuildPipeline from './createBuildPipeline.js'
3
-
4
- export { buildAllDts, buildDts, config, createBuildPipeline }
@@ -1,336 +0,0 @@
1
- import { beforeEach, describe, expect, it, vi } from 'vitest'
2
-
3
- const {
4
- mockRolldown,
5
- mockBundleWrite,
6
- mockBundleClose,
7
- mockReaddirSync,
8
- mockCreateBuildPipeline,
9
- mockRolldownConfig,
10
- mockBuildAllDts,
11
- } = vi.hoisted(() => ({
12
- mockRolldown: vi.fn(),
13
- mockBundleWrite: vi.fn(),
14
- mockBundleClose: vi.fn(),
15
- mockReaddirSync: vi.fn(),
16
- mockCreateBuildPipeline: vi.fn(),
17
- mockRolldownConfig: vi.fn(),
18
- mockBuildAllDts: vi.fn(),
19
- }))
20
-
21
- vi.mock('rolldown', () => ({ rolldown: mockRolldown }))
22
- vi.mock('rimraf', () => ({ rimraf: { sync: vi.fn() } }))
23
- vi.mock('node:fs', () => ({
24
- readdirSync: mockReaddirSync,
25
- renameSync: vi.fn(),
26
- statSync: vi.fn(),
27
- unlinkSync: vi.fn(),
28
- cpSync: vi.fn(),
29
- mkdirSync: vi.fn(),
30
- }))
31
-
32
- vi.mock('../config/index.js', () => ({
33
- CONFIG: { outputDir: 'lib' },
34
- PKG: { name: '@test/pkg', version: '1.0.0' },
35
- }))
36
-
37
- vi.mock('../rolldown/index.js', () => ({
38
- createBuildPipeline: mockCreateBuildPipeline,
39
- config: mockRolldownConfig,
40
- buildAllDts: mockBuildAllDts,
41
- }))
42
-
43
- describe('build', () => {
44
- beforeEach(() => {
45
- vi.clearAllMocks()
46
-
47
- mockBundleWrite.mockResolvedValue(undefined)
48
- mockBundleClose.mockResolvedValue(undefined)
49
- mockRolldown.mockResolvedValue({
50
- write: mockBundleWrite,
51
- close: mockBundleClose,
52
- })
53
- mockReaddirSync.mockReturnValue([])
54
-
55
- mockCreateBuildPipeline.mockReturnValue([
56
- {
57
- file: 'lib/index.js',
58
- format: 'es',
59
- env: 'development',
60
- platform: 'universal',
61
- },
62
- ])
63
- mockRolldownConfig.mockImplementation((item: any) => ({
64
- input: 'src',
65
- output: {
66
- dir: 'lib',
67
- entryFileNames: 'index.js',
68
- format: item.format,
69
- },
70
- }))
71
- mockBuildAllDts.mockReturnValue([])
72
- })
73
-
74
- it('should execute build pipeline successfully', async () => {
75
- vi.resetModules()
76
- const { runBuild } = await import('./build.js')
77
-
78
- await runBuild()
79
-
80
- expect(mockRolldown).toHaveBeenCalled()
81
- expect(mockBundleWrite).toHaveBeenCalled()
82
- expect(mockBundleClose).toHaveBeenCalled()
83
- })
84
-
85
- it('should generate DTS when buildAllDts returns configs', async () => {
86
- mockBuildAllDts.mockReturnValue([
87
- {
88
- file: './lib/index.d.ts',
89
- input: 'src/index.ts',
90
- output: {
91
- dir: 'lib',
92
- entryFileNames: 'index.d.ts',
93
- format: 'es',
94
- },
95
- },
96
- ])
97
-
98
- vi.resetModules()
99
- const { runBuild } = await import('./build.js')
100
- vi.clearAllMocks()
101
-
102
- // Re-setup after clear
103
- mockRolldown.mockResolvedValue({
104
- write: mockBundleWrite,
105
- close: mockBundleClose,
106
- })
107
- mockBundleWrite.mockResolvedValue(undefined)
108
- mockBundleClose.mockResolvedValue(undefined)
109
- mockReaddirSync.mockReturnValue([])
110
- mockRolldownConfig.mockImplementation((item: any) => ({
111
- input: 'src',
112
- output: {
113
- dir: 'lib',
114
- entryFileNames: 'index.js',
115
- format: item.format,
116
- },
117
- }))
118
- mockBuildAllDts.mockReturnValue([
119
- {
120
- file: './lib/index.d.ts',
121
- input: 'src/index.ts',
122
- output: {
123
- dir: 'lib',
124
- entryFileNames: 'index.d.ts',
125
- format: 'es',
126
- },
127
- },
128
- ])
129
- // buildDtsIsolated reads temp dir to find largest .d.ts
130
- mockReaddirSync.mockReturnValue(['index.d.ts'])
131
- const { statSync } = await import('node:fs')
132
- vi.mocked(statSync).mockReturnValue({ size: 100 } as ReturnType<
133
- typeof statSync
134
- >)
135
-
136
- await runBuild()
137
-
138
- // main build + DTS build = 2 rolldown calls
139
- expect(mockRolldown).toHaveBeenCalledTimes(2)
140
- })
141
-
142
- it('should pick largest DTS file when code-split occurs', async () => {
143
- mockBuildAllDts.mockReturnValue([
144
- {
145
- file: './lib/index.d.ts',
146
- input: 'src/index.ts',
147
- output: {
148
- dir: 'lib',
149
- entryFileNames: 'index.d.ts',
150
- format: 'es',
151
- },
152
- },
153
- ])
154
-
155
- vi.resetModules()
156
- const { runBuild } = await import('./build.js')
157
- const { statSync, renameSync, mkdirSync } = await import('node:fs')
158
- vi.clearAllMocks()
159
-
160
- // Re-setup
161
- mockRolldown.mockResolvedValue({
162
- write: mockBundleWrite,
163
- close: mockBundleClose,
164
- })
165
- mockBundleWrite.mockResolvedValue(undefined)
166
- mockBundleClose.mockResolvedValue(undefined)
167
- mockRolldownConfig.mockImplementation((item: any) => ({
168
- input: 'src',
169
- output: {
170
- dir: 'lib',
171
- entryFileNames: 'index.js',
172
- format: item.format,
173
- },
174
- }))
175
- mockBuildAllDts.mockReturnValue([
176
- {
177
- file: './lib/index.d.ts',
178
- input: 'src/index.ts',
179
- output: {
180
- dir: 'lib',
181
- entryFileNames: 'index.d.ts',
182
- format: 'es',
183
- },
184
- },
185
- ])
186
- // Temp dir has entry stub + larger chunk — largest should be picked
187
- mockReaddirSync.mockReturnValue(['index.d.ts', 'index2.d.ts'])
188
- vi.mocked(statSync).mockImplementation((p: any) => {
189
- if (String(p).includes('index2'))
190
- return { size: 1000 } as ReturnType<typeof statSync>
191
- return { size: 10 } as ReturnType<typeof statSync>
192
- })
193
-
194
- await runBuild()
195
-
196
- // buildDtsIsolated creates temp dir, picks largest, moves to final
197
- expect(mkdirSync).toHaveBeenCalled()
198
- expect(renameSync).toHaveBeenCalled()
199
- })
200
-
201
- it('should handle build failure gracefully', async () => {
202
- vi.resetModules()
203
- const { runBuild } = await import('./build.js')
204
- vi.clearAllMocks()
205
-
206
- const buildError = new Error('rolldown failed')
207
- mockRolldown.mockRejectedValue(buildError)
208
- mockReaddirSync.mockReturnValue([])
209
- mockRolldownConfig.mockImplementation((item: any) => ({
210
- input: 'src',
211
- output: {
212
- dir: 'lib',
213
- entryFileNames: 'index.js',
214
- format: item.format,
215
- },
216
- }))
217
- mockBuildAllDts.mockReturnValue([])
218
-
219
- await expect(runBuild()).rejects.toThrow('rolldown failed')
220
- })
221
-
222
- it('should handle multiple builds in sequence', async () => {
223
- mockCreateBuildPipeline.mockReturnValue([
224
- {
225
- file: 'lib/index.js',
226
- format: 'es',
227
- env: 'development',
228
- platform: 'universal',
229
- },
230
- {
231
- file: 'lib/index.cjs',
232
- format: 'cjs',
233
- env: 'development',
234
- platform: 'universal',
235
- },
236
- ])
237
-
238
- vi.resetModules()
239
- const { runBuild } = await import('./build.js')
240
- vi.clearAllMocks()
241
-
242
- mockRolldown.mockResolvedValue({
243
- write: mockBundleWrite,
244
- close: mockBundleClose,
245
- })
246
- mockBundleWrite.mockResolvedValue(undefined)
247
- mockBundleClose.mockResolvedValue(undefined)
248
- mockReaddirSync.mockReturnValue([])
249
- mockRolldownConfig.mockImplementation((item: any) => ({
250
- input: 'src',
251
- output: {
252
- dir: 'lib',
253
- entryFileNames: 'index.js',
254
- format: item.format,
255
- },
256
- }))
257
- mockBuildAllDts.mockReturnValue([])
258
-
259
- await runBuild()
260
-
261
- expect(mockRolldownConfig).toHaveBeenCalledTimes(2)
262
- expect(mockRolldown).toHaveBeenCalledTimes(2)
263
- })
264
-
265
- it('should generate multiple DTS for subpath exports', async () => {
266
- mockBuildAllDts.mockReturnValue([
267
- {
268
- file: './lib/types/index.d.ts',
269
- input: 'src/index.ts',
270
- output: {
271
- dir: 'lib/types',
272
- entryFileNames: 'index.d.ts',
273
- format: 'es',
274
- },
275
- },
276
- {
277
- file: './lib/types/devtools/index.d.ts',
278
- input: 'src/devtools/index.ts',
279
- output: {
280
- dir: 'lib/types/devtools',
281
- entryFileNames: 'index.d.ts',
282
- format: 'es',
283
- },
284
- },
285
- ])
286
-
287
- vi.resetModules()
288
- const { runBuild } = await import('./build.js')
289
- const { statSync } = await import('node:fs')
290
- vi.clearAllMocks()
291
-
292
- mockRolldown.mockResolvedValue({
293
- write: mockBundleWrite,
294
- close: mockBundleClose,
295
- })
296
- mockBundleWrite.mockResolvedValue(undefined)
297
- mockBundleClose.mockResolvedValue(undefined)
298
- mockReaddirSync.mockReturnValue(['index.d.ts'])
299
- vi.mocked(statSync).mockReturnValue({ size: 100 } as ReturnType<
300
- typeof statSync
301
- >)
302
- mockRolldownConfig.mockImplementation((item: any) => ({
303
- input: 'src',
304
- output: {
305
- dir: 'lib',
306
- entryFileNames: 'index.js',
307
- format: item.format,
308
- },
309
- }))
310
- mockBuildAllDts.mockReturnValue([
311
- {
312
- file: './lib/types/index.d.ts',
313
- input: 'src/index.ts',
314
- output: {
315
- dir: 'lib/types',
316
- entryFileNames: 'index.d.ts',
317
- format: 'es',
318
- },
319
- },
320
- {
321
- file: './lib/types/devtools/index.d.ts',
322
- input: 'src/devtools/index.ts',
323
- output: {
324
- dir: 'lib/types/devtools',
325
- entryFileNames: 'index.d.ts',
326
- format: 'es',
327
- },
328
- },
329
- ])
330
-
331
- await runBuild()
332
-
333
- // 1 main build + 2 isolated DTS builds = 3 rolldown calls
334
- expect(mockRolldown).toHaveBeenCalledTimes(3)
335
- })
336
- })