@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,228 +0,0 @@
1
- import { existsSync } from 'node:fs'
2
- import { swapGlobals } from '@vitus-labs/tools-core'
3
- import type { RolldownPlugin } from 'rolldown'
4
- import { dts } from 'rolldown-plugin-dts'
5
- import filesize from 'rollup-plugin-filesize'
6
- import { visualizer } from 'rollup-plugin-visualizer'
7
- import { CONFIG, PKG, PLATFORMS } from '../config/index.js'
8
-
9
- const defineExtensions = (platform: string) => {
10
- const platformExtensions: string[] = []
11
-
12
- if ((PLATFORMS as readonly string[]).includes(platform)) {
13
- CONFIG.extensions.forEach((item: string) => {
14
- platformExtensions.push(`.${platform}${item}`)
15
- })
16
- }
17
-
18
- return platformExtensions.concat(CONFIG.extensions)
19
- }
20
-
21
- const mapPlatform = (
22
- platform: string,
23
- ): 'node' | 'browser' | 'neutral' | undefined => {
24
- if (platform === 'node') return 'node'
25
- if (platform === 'browser') return 'browser'
26
- return 'neutral'
27
- }
28
-
29
- const loadPlugins = ({ file }: { file: string }) => {
30
- const plugins: RolldownPlugin[] = []
31
-
32
- // generate visualised graphs in dist folder
33
- if (CONFIG.visualise) {
34
- const filePath = file.split('/')
35
- const fileName = filePath.pop()
36
-
37
- const visualiserOptions = {
38
- title: `${PKG.name} - ${fileName}`,
39
- filename: `${filePath.join('/')}/${
40
- CONFIG.visualise.outputDir
41
- }/${fileName}.html`,
42
- template: CONFIG.visualise.template,
43
- gzipSize: CONFIG.visualise.gzipSize,
44
- }
45
-
46
- plugins.push(visualizer(visualiserOptions) as RolldownPlugin)
47
- }
48
-
49
- if (CONFIG.filesize) {
50
- plugins.push(filesize() as RolldownPlugin)
51
- }
52
-
53
- return plugins
54
- }
55
-
56
- const buildDefineOptions = (env: string, platform: string) => {
57
- if (!CONFIG.replaceGlobals) return undefined
58
-
59
- const defineOptions: Record<string, string> = {
60
- __VERSION__: JSON.stringify(PKG.version),
61
- __NODE__: JSON.stringify(platform === 'node'),
62
- __WEB__: JSON.stringify(
63
- ['node', 'browser', 'universal'].includes(platform),
64
- ),
65
- __BROWSER__: JSON.stringify(platform === 'browser'),
66
- __NATIVE__: JSON.stringify(platform === 'native'),
67
- __CLIENT__: JSON.stringify(['native', 'browser'].includes(platform)),
68
- }
69
-
70
- if (env === 'production') {
71
- defineOptions['process.env.NODE_ENV'] = JSON.stringify(env)
72
- }
73
-
74
- return defineOptions
75
- }
76
-
77
- const rolldownConfig = ({
78
- file,
79
- format,
80
- env,
81
- platform,
82
- input: entryInput,
83
- }: Record<string, any>) => {
84
- const extensions = defineExtensions(platform)
85
- const builtinPlugins = loadPlugins({ file })
86
- const userPlugins = (CONFIG.plugins || []) as RolldownPlugin[]
87
- const define = buildDefineOptions(env, platform)
88
-
89
- const lastSlash = file.lastIndexOf('/')
90
- const dir = lastSlash >= 0 ? file.substring(0, lastSlash) : '.'
91
- const entryFileName = lastSlash >= 0 ? file.substring(lastSlash + 1) : file
92
-
93
- const external = CONFIG.bundleAll
94
- ? []
95
- : [...PKG.externalDependencies, ...CONFIG.external]
96
-
97
- const buildOutput = {
98
- input: entryInput || CONFIG.sourceDir,
99
- platform: mapPlatform(platform),
100
- tsconfig: CONFIG.typescript ? 'tsconfig.json' : undefined,
101
- resolve: {
102
- extensions,
103
- alias: CONFIG.alias || undefined,
104
- },
105
- transform: define ? { define } : undefined,
106
- output: {
107
- dir,
108
- entryFileNames: entryFileName,
109
- format,
110
- globals: swapGlobals(CONFIG.globals),
111
- sourcemap: true,
112
- sourcemapIgnoreList: (relativeSourcePath: string) =>
113
- relativeSourcePath.includes('node_modules'),
114
- exports: ['cjs', 'umd', 'iife'].includes(format)
115
- ? ('named' as const)
116
- : undefined,
117
- name: ['umd', 'iife'].includes(format) ? PKG.bundleName : undefined,
118
- esModule: true,
119
- minify: env === 'production',
120
- banner: CONFIG.banner || undefined,
121
- footer: CONFIG.footer || undefined,
122
- },
123
- external,
124
- treeshake: {
125
- moduleSideEffects: false,
126
- },
127
- plugins: [...builtinPlugins, ...userPlugins],
128
- }
129
-
130
- return buildOutput
131
- }
132
-
133
- const createDtsConfig = (typesFilePath: string, inputFile: string) => {
134
- const lastSlash = typesFilePath.lastIndexOf('/')
135
- const dir = lastSlash >= 0 ? typesFilePath.substring(0, lastSlash) : '.'
136
- const entryFileName =
137
- lastSlash >= 0 ? typesFilePath.substring(lastSlash + 1) : typesFilePath
138
-
139
- return {
140
- file: typesFilePath,
141
- input: inputFile,
142
- tsconfig: 'tsconfig.json',
143
- resolve: {
144
- extensions: CONFIG.extensions,
145
- },
146
- external: CONFIG.bundleAll
147
- ? []
148
- : [...PKG.externalDependencies, ...CONFIG.external],
149
- plugins: [
150
- ...(dts({
151
- tsconfig: 'tsconfig.json',
152
- emitDtsOnly: true,
153
- }) as RolldownPlugin[]),
154
- ],
155
- output: {
156
- dir,
157
- entryFileNames: entryFileName,
158
- chunkFileNames: '[name].d.ts',
159
- format: 'es' as const,
160
- sourcemap: false,
161
- esModule: true,
162
- },
163
- }
164
- }
165
-
166
- /** Check if exports object uses subpath keys */
167
- const isSubpathExports = (obj: Record<string, any>): boolean =>
168
- Object.keys(obj).some((k) => k === '.' || k.startsWith('./'))
169
-
170
- /** Resolve the actual source file extension (.ts, .tsx, etc.) */
171
- const resolveWithExtension = (path: string): string => {
172
- for (const ext of ['.ts', '.tsx', '.js', '.jsx']) {
173
- if (existsSync(`${path}${ext}`)) return `${path}${ext}`
174
- }
175
- return `${path}.ts`
176
- }
177
-
178
- /** Resolve input .ts file from a subpath export key using convention. */
179
- const resolveSubpathInput = (exportPath: string): string => {
180
- if (exportPath === '.') return `${CONFIG.sourceDir}/index.ts`
181
- const subpath = exportPath.slice(2)
182
- return `${CONFIG.sourceDir}/${subpath}`
183
- }
184
-
185
- const buildDts = (): ReturnType<typeof createDtsConfig> | null => {
186
- if (!CONFIG.typescript) return null
187
-
188
- // Simple case: no subpath exports
189
- const typesFilePath = PKG?.exports?.types || PKG.types || PKG.typings
190
- if (typesFilePath) {
191
- return createDtsConfig(typesFilePath, `${CONFIG.sourceDir}/index.ts`)
192
- }
193
-
194
- return null
195
- }
196
-
197
- /** Build DTS configs for all subpath exports that have a `types` field */
198
- const buildAllDts = (): ReturnType<typeof createDtsConfig>[] => {
199
- if (!CONFIG.typescript) return []
200
-
201
- const exportsOptions = PKG.exports
202
- if (
203
- !exportsOptions ||
204
- typeof exportsOptions !== 'object' ||
205
- !isSubpathExports(exportsOptions)
206
- ) {
207
- const single = buildDts()
208
- return single ? [single] : []
209
- }
210
-
211
- const results: ReturnType<typeof createDtsConfig>[] = []
212
- for (const [exportPath, exportConfig] of Object.entries(exportsOptions)) {
213
- if (!exportConfig || typeof exportConfig !== 'object') continue
214
- const typesPath = (exportConfig as Record<string, string>).types
215
- if (!typesPath) continue
216
- const resolved = resolveSubpathInput(exportPath)
217
- const inputFile =
218
- resolved.endsWith('.ts') || resolved.endsWith('.tsx')
219
- ? resolved
220
- : resolveWithExtension(resolved)
221
- results.push(createDtsConfig(typesPath, inputFile))
222
- }
223
-
224
- return results
225
- }
226
-
227
- export default rolldownConfig
228
- export { buildAllDts, buildDts }
@@ -1,375 +0,0 @@
1
- import { beforeEach, describe, expect, it, vi } from 'vitest'
2
-
3
- const { mockPKG } = vi.hoisted(() => ({
4
- mockPKG: {
5
- type: 'commonjs',
6
- name: '@test/pkg',
7
- main: 'lib/index.cjs',
8
- module: 'lib/index.js',
9
- exports: { import: './lib/index.js', require: './lib/index.cjs' },
10
- } as Record<string, any>,
11
- }))
12
-
13
- vi.mock('../config/index.js', () => ({
14
- PKG: mockPKG,
15
- CONFIG: { sourceDir: 'src' },
16
- PLATFORMS: ['browser', 'node', 'web', 'native'],
17
- }))
18
-
19
- describe('createBuildPipeline', () => {
20
- const defaultPKG = { ...mockPKG }
21
-
22
- beforeEach(() => {
23
- Object.assign(mockPKG, defaultPKG)
24
- // Remove any extra keys added by specific tests
25
- for (const key of Object.keys(mockPKG)) {
26
- if (!(key in defaultPKG)) delete mockPKG[key]
27
- }
28
- })
29
-
30
- describe('with CJS package', () => {
31
- let createBuildPipeline: () => any[]
32
-
33
- beforeEach(async () => {
34
- vi.resetModules()
35
- mockPKG.type = 'commonjs'
36
- const mod = await import('./createBuildPipeline.js')
37
- createBuildPipeline = mod.default
38
- })
39
-
40
- it('should create builds from package.json fields', () => {
41
- const builds = createBuildPipeline()
42
-
43
- expect(builds.length).toBeGreaterThan(0)
44
- const mainBuild = builds.find((b) => b.file === 'lib/index.cjs')
45
- expect(mainBuild).toBeDefined()
46
- expect(mainBuild.format).toBe('cjs')
47
- })
48
-
49
- it('should include module build as ES format', () => {
50
- const builds = createBuildPipeline()
51
-
52
- const moduleBuild = builds.find((b) => b.file === 'lib/index.js')
53
- expect(moduleBuild).toBeDefined()
54
- expect(moduleBuild.format).toBe('es')
55
- })
56
- })
57
-
58
- describe('with ES module package', () => {
59
- let createBuildPipeline: () => any[]
60
-
61
- beforeEach(async () => {
62
- vi.resetModules()
63
- mockPKG.type = 'module'
64
- const mod = await import('./createBuildPipeline.js')
65
- createBuildPipeline = mod.default
66
- })
67
-
68
- it('should use ES format for main build', () => {
69
- const builds = createBuildPipeline()
70
-
71
- const mainBuild = builds.find((b) => b.file === 'lib/index.cjs')
72
- if (mainBuild) {
73
- expect(mainBuild.format).toBe('es')
74
- }
75
- })
76
-
77
- it('should include exports options', () => {
78
- const builds = createBuildPipeline()
79
-
80
- expect(builds.length).toBeGreaterThan(0)
81
- })
82
- })
83
-
84
- describe('with string exports', () => {
85
- let createBuildPipeline: () => any[]
86
-
87
- beforeEach(async () => {
88
- vi.resetModules()
89
- mockPKG.type = 'module'
90
- mockPKG.exports = './lib/index.js'
91
- const mod = await import('./createBuildPipeline.js')
92
- createBuildPipeline = mod.default
93
- })
94
-
95
- it('should handle string exports', () => {
96
- const builds = createBuildPipeline()
97
-
98
- const exportBuild = builds.find((b) => b.file === './lib/index.js')
99
- expect(exportBuild).toBeDefined()
100
- expect(exportBuild.format).toBe('es')
101
- })
102
- })
103
-
104
- describe('with object exports having node field', () => {
105
- let createBuildPipeline: () => any[]
106
-
107
- beforeEach(async () => {
108
- vi.resetModules()
109
- mockPKG.type = 'module'
110
- mockPKG.exports = {
111
- import: './lib/index.js',
112
- require: './lib/index.cjs',
113
- node: './lib/node.js',
114
- default: './lib/default.js',
115
- }
116
- const mod = await import('./createBuildPipeline.js')
117
- createBuildPipeline = mod.default
118
- })
119
-
120
- it('should include node export and skip default when import exists', () => {
121
- const builds = createBuildPipeline()
122
-
123
- const nodeBuild = builds.find((b) => b.file === './lib/node.js')
124
- expect(nodeBuild).toBeDefined()
125
- expect(nodeBuild.platform).toBe('node')
126
-
127
- // default is skipped when import is present (avoids duplicate builds)
128
- const defaultBuild = builds.find((b) => b.file === './lib/default.js')
129
- expect(defaultBuild).toBeUndefined()
130
- })
131
- })
132
-
133
- describe('with no exports', () => {
134
- let createBuildPipeline: () => any[]
135
-
136
- beforeEach(async () => {
137
- vi.resetModules()
138
- mockPKG.type = 'module'
139
- delete mockPKG.exports
140
- const mod = await import('./createBuildPipeline.js')
141
- createBuildPipeline = mod.default
142
- })
143
-
144
- it('should return builds based on main/module fields only', () => {
145
- const builds = createBuildPipeline()
146
-
147
- expect(builds.length).toBeGreaterThan(0)
148
- })
149
- })
150
-
151
- describe('with passthrough exports like package.json', () => {
152
- let createBuildPipeline: () => any[]
153
-
154
- beforeEach(async () => {
155
- vi.resetModules()
156
- mockPKG.type = 'module'
157
- delete mockPKG.main
158
- delete mockPKG.module
159
- mockPKG.exports = {
160
- '.': {
161
- import: './lib/index.js',
162
- },
163
- './package.json': './package.json',
164
- }
165
- const mod = await import('./createBuildPipeline.js')
166
- createBuildPipeline = mod.default
167
- })
168
-
169
- it('should skip passthrough exports that are not JS files', () => {
170
- const builds = createBuildPipeline()
171
-
172
- expect(builds).toHaveLength(1)
173
- expect(builds[0].file).toBe('./lib/index.js')
174
- })
175
- })
176
-
177
- describe('with react-native build', () => {
178
- let createBuildPipeline: () => any[]
179
-
180
- beforeEach(async () => {
181
- vi.resetModules()
182
- mockPKG.type = 'commonjs'
183
- mockPKG['react-native'] = 'lib/native.js'
184
- mockPKG.module = 'lib/index.js'
185
- const mod = await import('./createBuildPipeline.js')
186
- createBuildPipeline = mod.default
187
- })
188
-
189
- it('should add native build when path differs from module', () => {
190
- const builds = createBuildPipeline()
191
-
192
- const nativeBuild = builds.find((b) => b.platform === 'native')
193
- expect(nativeBuild).toBeDefined()
194
- expect(nativeBuild.file).toBe('lib/native.js')
195
- })
196
- })
197
-
198
- describe('with browser-specific builds', () => {
199
- let createBuildPipeline: () => any[]
200
-
201
- beforeEach(async () => {
202
- vi.resetModules()
203
- mockPKG.type = 'commonjs'
204
- mockPKG.main = 'lib/index.cjs'
205
- mockPKG.module = 'lib/index.js'
206
- // browser remaps module output — hasDifferentBrowserBuild('main') returns true
207
- // because source ('lib/index.js') !== PKG['main'] ('lib/index.cjs')
208
- mockPKG.browser = {
209
- './lib/index.js': './lib/browser.js',
210
- }
211
- const mod = await import('./createBuildPipeline.js')
212
- createBuildPipeline = mod.default
213
- })
214
-
215
- it('should create browser-specific build variants', () => {
216
- const builds = createBuildPipeline()
217
-
218
- const browserBuild = builds.find((b) => b.platform === 'browser')
219
- expect(browserBuild).toBeDefined()
220
- expect(browserBuild.file).toBe('lib/browser.js')
221
- })
222
-
223
- it('should set node platform for main build when browser variant exists', () => {
224
- const builds = createBuildPipeline()
225
-
226
- const nodeBuild = builds.find(
227
- (b) => b.file === 'lib/index.cjs' && b.platform === 'node',
228
- )
229
- expect(nodeBuild).toBeDefined()
230
- })
231
- })
232
-
233
- describe('with subpath exports', () => {
234
- let createBuildPipeline: () => any[]
235
-
236
- beforeEach(async () => {
237
- vi.resetModules()
238
- mockPKG.type = 'module'
239
- delete mockPKG.main
240
- delete mockPKG.module
241
- mockPKG.exports = {
242
- '.': {
243
- types: './lib/types/index.d.ts',
244
- import: './lib/index.js',
245
- },
246
- './devtools': {
247
- types: './lib/types/devtools/index.d.ts',
248
- import: './lib/devtools/index.js',
249
- },
250
- './validation/zod': {
251
- types: './lib/types/validation/zod.d.ts',
252
- import: './lib/validation/zod.js',
253
- },
254
- }
255
- const mod = await import('./createBuildPipeline.js')
256
- createBuildPipeline = mod.default
257
- })
258
-
259
- it('should create builds for all subpath exports', () => {
260
- const builds = createBuildPipeline()
261
-
262
- expect(builds).toHaveLength(3)
263
- })
264
-
265
- it('should set correct input for root export', () => {
266
- const builds = createBuildPipeline()
267
- const root = builds.find((b) => b.file === './lib/index.js')
268
-
269
- expect(root).toBeDefined()
270
- expect(root.input).toBe('src/index.ts')
271
- expect(root.format).toBe('es')
272
- })
273
-
274
- it('should set correct input for subpath export', () => {
275
- const builds = createBuildPipeline()
276
- const devtools = builds.find((b) => b.file === './lib/devtools/index.js')
277
-
278
- expect(devtools).toBeDefined()
279
- expect(devtools.input).toBe('src/devtools')
280
- })
281
-
282
- it('should set correct input for nested subpath export', () => {
283
- const builds = createBuildPipeline()
284
- const zod = builds.find((b) => b.file === './lib/validation/zod.js')
285
-
286
- expect(zod).toBeDefined()
287
- expect(zod.input).toBe('src/validation/zod')
288
- })
289
- })
290
-
291
- describe('with subpath exports having multiple conditions', () => {
292
- let createBuildPipeline: () => any[]
293
-
294
- beforeEach(async () => {
295
- vi.resetModules()
296
- mockPKG.type = 'module'
297
- delete mockPKG.main
298
- delete mockPKG.module
299
- mockPKG.exports = {
300
- '.': {
301
- import: './lib/index.js',
302
- require: './lib/index.cjs',
303
- },
304
- }
305
- const mod = await import('./createBuildPipeline.js')
306
- createBuildPipeline = mod.default
307
- })
308
-
309
- it('should create builds for each condition', () => {
310
- const builds = createBuildPipeline()
311
-
312
- const esBuild = builds.find((b) => b.file === './lib/index.js')
313
- expect(esBuild).toBeDefined()
314
- expect(esBuild.format).toBe('es')
315
-
316
- const cjsBuild = builds.find((b) => b.file === './lib/index.cjs')
317
- expect(cjsBuild).toBeDefined()
318
- expect(cjsBuild.format).toBe('cjs')
319
- })
320
- })
321
-
322
- describe('with subpath string export', () => {
323
- let createBuildPipeline: () => any[]
324
-
325
- beforeEach(async () => {
326
- vi.resetModules()
327
- mockPKG.type = 'module'
328
- delete mockPKG.main
329
- delete mockPKG.module
330
- mockPKG.exports = {
331
- '.': './lib/index.js',
332
- './utils': './lib/utils.js',
333
- }
334
- const mod = await import('./createBuildPipeline.js')
335
- createBuildPipeline = mod.default
336
- })
337
-
338
- it('should handle string subpath exports', () => {
339
- const builds = createBuildPipeline()
340
-
341
- const root = builds.find((b) => b.file === './lib/index.js')
342
- expect(root).toBeDefined()
343
- expect(root.input).toBe('src/index.ts')
344
-
345
- const utils = builds.find((b) => b.file === './lib/utils.js')
346
- expect(utils).toBeDefined()
347
- expect(utils.input).toBe('src/utils')
348
- })
349
- })
350
-
351
- describe('with UMD builds', () => {
352
- let createBuildPipeline: () => any[]
353
-
354
- beforeEach(async () => {
355
- vi.resetModules()
356
- mockPKG.type = 'commonjs'
357
- mockPKG['umd:main'] = 'lib/index.umd.js'
358
- mockPKG.unpkg = 'lib/index.umd.min.js'
359
- const mod = await import('./createBuildPipeline.js')
360
- createBuildPipeline = mod.default
361
- })
362
-
363
- it('should include UMD build variants', () => {
364
- const builds = createBuildPipeline()
365
-
366
- const umdDev = builds.find((b) => b.file === 'lib/index.umd.js')
367
- expect(umdDev).toBeDefined()
368
- expect(umdDev.format).toBe('umd')
369
-
370
- const umdProd = builds.find((b) => b.file === 'lib/index.umd.min.js')
371
- expect(umdProd).toBeDefined()
372
- expect(umdProd.env).toBe('production')
373
- })
374
- })
375
- })