Skip to content

Commit f7153a1

Browse files
authored
Merge pull request #717 from nextcloud-libraries/fix/make-rolldown-compatible
fix: make configuration rolldown compatible
2 parents 011394b + fc0a308 commit f7153a1

5 files changed

Lines changed: 36 additions & 24 deletions

File tree

__tests__/appconfig.spec.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ describe('app config', () => {
5050
const output = resolved.build.rollupOptions.output as OutputOptions
5151
expect(typeof output?.assetFileNames).toBe('function')
5252
const assetFileNames = output?.assetFileNames as ((chunkInfo: unknown) => string)
53-
expect(assetFileNames({ name: 'some.css' })).toMatch(/^css\/[^/]+\.css/)
54-
expect(assetFileNames({ name: 'other/file.css' })).toMatch(/^css\/[^/]+\.css/)
53+
expect(assetFileNames({ names: ['some.css'] })).toMatch(/^css\/[^/]+\.css/)
54+
expect(assetFileNames({ names: ['other/file.css'] })).toMatch(/^css\/[^/]+\.css/)
5555
})
5656

5757
it('moves image assets to img/', async () => {
@@ -60,10 +60,10 @@ describe('app config', () => {
6060
const output = resolved.build.rollupOptions.output as OutputOptions
6161
expect(typeof output?.assetFileNames).toBe('function')
6262
const assetFileNames = output?.assetFileNames as ((chunkInfo: unknown) => string)
63-
expect(assetFileNames({ name: 'some.png' })).toBe('img/[name][extname]')
64-
expect(assetFileNames({ name: 'some.svg' })).toBe('img/[name][extname]')
65-
expect(assetFileNames({ name: 'some.jpg' })).toBe('img/[name][extname]')
66-
expect(assetFileNames({ name: 'some.ico' })).toBe('img/[name][extname]')
63+
expect(assetFileNames({ names: ['some.png'] })).toBe('img/[name][extname]')
64+
expect(assetFileNames({ names: ['some.svg'] })).toBe('img/[name][extname]')
65+
expect(assetFileNames({ names: ['some.jpg'] })).toBe('img/[name][extname]')
66+
expect(assetFileNames({ names: ['some.ico'] })).toBe('img/[name][extname]')
6767
})
6868

6969
it('moves fonts to css/fonts', async () => {
@@ -72,20 +72,20 @@ describe('app config', () => {
7272
const output = resolved.build.rollupOptions.output as OutputOptions
7373
expect(typeof output?.assetFileNames).toBe('function')
7474
const assetFileNames = output?.assetFileNames as ((chunkInfo: unknown) => string)
75-
expect(assetFileNames({ name: 'some.woff' })).toBe('css/fonts/[name][extname]')
76-
expect(assetFileNames({ name: 'some.woff2' })).toBe('css/fonts/[name][extname]')
77-
expect(assetFileNames({ name: 'some.otf' })).toBe('css/fonts/[name][extname]')
78-
expect(assetFileNames({ name: 'some.ttf' })).toBe('css/fonts/[name][extname]')
75+
expect(assetFileNames({ names: ['some.woff'] })).toBe('css/fonts/[name][extname]')
76+
expect(assetFileNames({ names: ['some.woff2'] })).toBe('css/fonts/[name][extname]')
77+
expect(assetFileNames({ names: ['some.otf'] })).toBe('css/fonts/[name][extname]')
78+
expect(assetFileNames({ names: ['some.ttf'] })).toBe('css/fonts/[name][extname]')
7979
})
8080

8181
it('allow custom asset names', async () => {
82-
const resolved = await createConfig('build', 'development', { assetFileNames: (({ name }) => name === 'main.png' ? 'img/main.png' : undefined) as never })
82+
const resolved = await createConfig('build', 'development', { assetFileNames: ({ names }) => names[0] === 'main.png' ? 'img/main.png' : undefined })
8383

8484
const output = resolved.build.rollupOptions.output as OutputOptions
8585
expect(typeof output?.assetFileNames).toBe('function')
8686
const assetFileNames = output?.assetFileNames as ((chunkInfo: unknown) => string)
87-
expect(assetFileNames({ name: 'main.png' })).toBe('img/main.png')
88-
expect(assetFileNames({ name: 'foo.png' })).toBe('img/[name][extname]')
87+
expect(assetFileNames({ names: ['main.png'] })).toBe('img/main.png')
88+
expect(assetFileNames({ names: ['foo.png'] })).toBe('img/[name][extname]')
8989
})
9090

9191
it('extracts CSS by default with CSS entry points', async () => {

lib/appConfig.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/**
22
* SPDX-FileCopyrightText: 2023 Ferdinand Thiessen <opensource@fthiessen.de>
3-
*
43
* SPDX-License-Identifier: AGPL-3.0-or-later
54
*/
65

@@ -12,6 +11,7 @@ import { readFileSync } from 'node:fs'
1211
import { relative } from 'node:path'
1312
import { cwd } from 'node:process'
1413
import { mergeConfig } from 'vite'
14+
import * as vite from 'vite'
1515
import { createBaseConfig } from './baseConfig.js'
1616
import { findAppinfo } from './utils/appinfo.js'
1717

@@ -223,7 +223,8 @@ export const createAppConfig = (entries: { [entryAlias: string]: string }, optio
223223
}
224224
}
225225

226-
const extType = assetInfo.name.split('.').pop()
226+
const [name] = assetInfo.names
227+
const extType = name.split('.').pop()
227228
if (/png|jpe?g|svg|gif|tiff|bmp|ico/i.test(extType)) {
228229
return 'img/[name][extname]'
229230
} else if (/css/i.test(extType)) {
@@ -243,9 +244,19 @@ export const createAppConfig = (entries: { [entryAlias: string]: string }, optio
243244
chunkFileNames: () => {
244245
return 'js/[name]-[hash].chunk.mjs'
245246
},
246-
manualChunks: {
247-
...(options?.coreJS ? { polyfill: ['core-js'] } : {}),
248-
},
247+
...(
248+
options?.coreJS
249+
? (
250+
'rolldownVite' in vite
251+
? {
252+
advancedChunks: {
253+
groups: [{ name: 'polyfill', test: /core-js/ }],
254+
},
255+
}
256+
: { polyfill: ['core-js'] }
257+
)
258+
: {}
259+
),
249260
},
250261
},
251262
},

lib/baseConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export interface BaseOptions {
5757
* but if returns undefined, then this config defaults is be used.
5858
*
5959
* @example Move CSS styles to `styles/style.css` instead of the default `css/[entrypoint-name].css`:
60-
* (chunkInfo) => chunkInfo.name.endsWith('.css') ? 'styles/style.css' : undefined
60+
* (chunkInfo) => chunkInfo.names[0].endsWith('.css') ? 'styles/style.css' : undefined
6161
*/
6262
assetFileNames?: (chunkInfo: Rollup.PreRenderedAsset) => string | undefined,
6363
/**

lib/libConfig.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* SPDX-License-Identifier: AGPL-3.0-or-later
55
*/
66

7-
import type { LibraryFormats, UserConfig, UserConfigFn, BuildOptions, Plugin } from 'vite'
7+
import type { LibraryFormats, UserConfig, UserConfigFn, BuildOptions, Plugin, Rollup } from 'vite'
88
import type { BaseOptions } from './baseConfig.js'
99

1010
import { mergeConfig } from 'vite'
@@ -108,7 +108,7 @@ export const createLibConfig = (entries: { [entryAlias: string]: string }, optio
108108
// Make sure we get a user config and not a promise or a user config function
109109
const userConfig = await Promise.resolve(typeof options.config === 'function' ? options.config(env) : options.config)
110110

111-
const assetFileNames = (assetInfo) => {
111+
const assetFileNames = (assetInfo: Rollup.PreRenderedAsset) => {
112112
// Allow to customize the asset file names
113113
if (options.assetFileNames) {
114114
const customName = options.assetFileNames(assetInfo)
@@ -117,7 +117,8 @@ export const createLibConfig = (entries: { [entryAlias: string]: string }, optio
117117
}
118118
}
119119

120-
const extType = assetInfo.name.split('.').pop()
120+
const [name] = assetInfo.names
121+
const extType = name.split('.').pop()
121122
if (!options.inlineCSS && /css/i.test(extType)) {
122123
return '[name].css'
123124
}

lib/plugins/CSSEntryPoints.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/**
22
* SPDX-FileCopyrightText: 2024 Ferdinand Thiessen <opensource@fthiessen.de>
3-
*
43
* SPDX-License-Identifier: MIT
54
*/
65

@@ -44,7 +43,8 @@ export function CSSEntryPointsPlugin(options?: CSSEntryPointsPluginOptions) {
4443
// If the original assets name option is a function we need to call it otherwise just use the template string
4544
const name = typeof config === 'function' ? config(info) : config
4645
// Only handle CSS files not extracted by this plugin
47-
if (info.name.endsWith('.css') && !String(info.source).startsWith('/* extracted by css-entry-points-plugin */')) {
46+
const [assetName] = info.names
47+
if (assetName.endsWith('.css') && !String(info.source).startsWith('/* extracted by css-entry-points-plugin */')) {
4848
// The new name should have the same path but instead of the .css extension it is .chunk.css
4949
return name.replace(/(.css|.\[ext\]|\[extname\])$/, '.chunk.css')
5050
}

0 commit comments

Comments
 (0)