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