Skip to content

Commit

Permalink
feat(tests): Add test cases for asset name handling
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Oct 18, 2023
1 parent 0f61f77 commit b1e4978
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
42 changes: 39 additions & 3 deletions __tests__/appconfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { build } from 'vite'
// ok as this is just for tests
// eslint-disable-next-line n/no-extraneous-import
import type { RollupOutput, OutputChunk } from 'rollup'
import type { RollupOutput, OutputOptions, OutputChunk } from 'rollup'
import { build, resolveConfig } from 'vite'
import { describe, it, expect } from 'vitest'
import { createAppConfig } from '../lib/appConfig'
import { fileURLToPath } from 'url'
import { resolve } from 'path'
import { LibraryOptions } from '../lib/libConfig'

const __dirname = fileURLToPath(new URL('.', import.meta.url))

Expand All @@ -32,4 +32,40 @@ describe('app config', () => {
expect(code.includes('process.env')).toBe(false)
expect(code).toMatchSnapshot()
})

it('moves CSS assets to css/', async () => {
const resolved = await createConfig('build', 'development')

const output = resolved.build.rollupOptions.output as OutputOptions
expect(typeof output?.assetFileNames).toBe('function')
const assetFileNames = output?.assetFileNames as ((chunkInfo: unknown) => string)
expect(assetFileNames({ name: 'some.css' })).toBe('css/@nextcloud-vite-config-[name].css')
expect(assetFileNames({ name: 'other/file.css' })).toBe('css/@nextcloud-vite-config-[name].css')
})

it('moves image assets to img/', async () => {
const resolved = await createConfig('build', 'development')

const output = resolved.build.rollupOptions.output as OutputOptions
expect(typeof output?.assetFileNames).toBe('function')
const assetFileNames = output?.assetFileNames as ((chunkInfo: unknown) => string)
expect(assetFileNames({ name: 'some.png' })).toBe('img/[name][extname]')
expect(assetFileNames({ name: 'some.svg' })).toBe('img/[name][extname]')
expect(assetFileNames({ name: 'some.jpg' })).toBe('img/[name][extname]')
expect(assetFileNames({ name: 'some.ico' })).toBe('img/[name][extname]')
})

it('allow custom asset names', async () => {
const resolved = await createConfig('build', 'development', { assetFileNames: (({ name }) => name === 'main.css' ? 'css/app-styles.css' : undefined) as never })

const output = resolved.build.rollupOptions.output as OutputOptions
expect(typeof output?.assetFileNames).toBe('function')
const assetFileNames = output?.assetFileNames as ((chunkInfo: unknown) => string)
expect(assetFileNames({ name: 'main.css' })).toBe('css/app-styles.css')
expect(assetFileNames({ name: 'foo.css' })).toBe('css/@nextcloud-vite-config-[name].css')
})

const createConfig = async (command: 'build' | 'serve' = 'build', mode: 'development' | 'production' = 'production', options?: LibraryOptions) => await resolveConfig(await createAppConfig({
main: 'src/main.js',
}, options)({ command, mode, ssrBuild: false }), command)
})
24 changes: 24 additions & 0 deletions __tests__/libconfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ describe('library config', () => {
// and minify disabled
expect(resolved.build.minify).toBe(false)
})

it('keep name without inlining the CSS assets ', async () => {
const resolved = await createConfig('build', 'development')

expect(resolved.build.rollupOptions.output?.[0].assetFileNames).not.toBe(undefined)
expect(resolved.build.rollupOptions.output?.[0].assetFileNames({ name: 'some.css' })).toBe('[name].css')
expect(resolved.build.rollupOptions.output?.[0].assetFileNames({ name: 'some.nfo' })).toBe('assets/[name]-[hash][extname]')
})

it('move CSS files to asset directory when inlining CSS', async () => {
const resolved = await createConfig('build', 'development', { inlineCSS: true })

expect(resolved.build.rollupOptions.output?.[0].assetFileNames).not.toBe(undefined)
expect(resolved.build.rollupOptions.output?.[0].assetFileNames({ name: 'some.css' })).toBe('assets/[name]-[hash][extname]')
expect(resolved.build.rollupOptions.output?.[0].assetFileNames({ name: 'some.nfo' })).toBe('assets/[name]-[hash][extname]')
})

it('allow custom asset names', async () => {
const resolved = await createConfig('build', 'development', { assetFileNames: (({ name }) => name === 'foo.css' ? 'bar.css' : undefined) as never })

expect(resolved.build.rollupOptions.output?.[0].assetFileNames).not.toBe(undefined)
expect(resolved.build.rollupOptions.output?.[0].assetFileNames({ name: 'foo.css' })).toBe('bar.css')
expect(resolved.build.rollupOptions.output?.[0].assetFileNames({ name: 'baz.css' })).toBe('[name].css')
})
})

const createConfig = async (command: 'build' | 'serve' = 'build', mode: 'development' | 'production' = 'production', options?: LibraryOptions) => await resolveConfig(await createLibConfig({
Expand Down

0 comments on commit b1e4978

Please sign in to comment.