3
3
*
4
4
* SPDX-License-Identifier: AGPL-3.0-or-later
5
5
*/
6
-
7
- import { build } from 'vite'
8
6
// ok as this is just for tests
9
7
// eslint-disable-next-line n/no-extraneous-import
10
- import type { RollupOutput , OutputChunk } from 'rollup'
8
+ import type { RollupOutput , OutputOptions , OutputChunk } from 'rollup'
9
+ import { build , resolveConfig } from 'vite'
11
10
import { describe , it , expect } from 'vitest'
12
11
import { createAppConfig } from '../lib/appConfig'
13
12
import { fileURLToPath } from 'url'
14
13
import { resolve } from 'path'
14
+ import { LibraryOptions } from '../lib/libConfig'
15
15
16
16
const __dirname = fileURLToPath ( new URL ( '.' , import . meta. url ) )
17
17
@@ -32,4 +32,40 @@ describe('app config', () => {
32
32
expect ( code . includes ( 'process.env' ) ) . toBe ( false )
33
33
expect ( code ) . toMatchSnapshot ( )
34
34
} )
35
+
36
+ it ( 'moves CSS assets to css/' , async ( ) => {
37
+ const resolved = await createConfig ( 'build' , 'development' )
38
+
39
+ const output = resolved . build . rollupOptions . output as OutputOptions
40
+ expect ( typeof output ?. assetFileNames ) . toBe ( 'function' )
41
+ const assetFileNames = output ?. assetFileNames as ( ( chunkInfo : unknown ) => string )
42
+ expect ( assetFileNames ( { name : 'some.css' } ) ) . toBe ( 'css/@nextcloud-vite-config-[name].css' )
43
+ expect ( assetFileNames ( { name : 'other/file.css' } ) ) . toBe ( 'css/@nextcloud-vite-config-[name].css' )
44
+ } )
45
+
46
+ it ( 'moves image assets to img/' , async ( ) => {
47
+ const resolved = await createConfig ( 'build' , 'development' )
48
+
49
+ const output = resolved . build . rollupOptions . output as OutputOptions
50
+ expect ( typeof output ?. assetFileNames ) . toBe ( 'function' )
51
+ const assetFileNames = output ?. assetFileNames as ( ( chunkInfo : unknown ) => string )
52
+ expect ( assetFileNames ( { name : 'some.png' } ) ) . toBe ( 'img/[name][extname]' )
53
+ expect ( assetFileNames ( { name : 'some.svg' } ) ) . toBe ( 'img/[name][extname]' )
54
+ expect ( assetFileNames ( { name : 'some.jpg' } ) ) . toBe ( 'img/[name][extname]' )
55
+ expect ( assetFileNames ( { name : 'some.ico' } ) ) . toBe ( 'img/[name][extname]' )
56
+ } )
57
+
58
+ it ( 'allow custom asset names' , async ( ) => {
59
+ const resolved = await createConfig ( 'build' , 'development' , { assetFileNames : ( ( { name } ) => name === 'main.css' ? 'css/app-styles.css' : undefined ) as never } )
60
+
61
+ const output = resolved . build . rollupOptions . output as OutputOptions
62
+ expect ( typeof output ?. assetFileNames ) . toBe ( 'function' )
63
+ const assetFileNames = output ?. assetFileNames as ( ( chunkInfo : unknown ) => string )
64
+ expect ( assetFileNames ( { name : 'main.css' } ) ) . toBe ( 'css/app-styles.css' )
65
+ expect ( assetFileNames ( { name : 'foo.css' } ) ) . toBe ( 'css/@nextcloud-vite-config-[name].css' )
66
+ } )
67
+
68
+ const createConfig = async ( command : 'build' | 'serve' = 'build' , mode : 'development' | 'production' = 'production' , options ?: LibraryOptions ) => await resolveConfig ( await createAppConfig ( {
69
+ main : 'src/main.js' ,
70
+ } , options ) ( { command, mode, ssrBuild : false } ) , command )
35
71
} )
0 commit comments