From b1e4978df0e7805f03156911b63b51ab5b243aad Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Thu, 12 Oct 2023 01:36:15 +0200 Subject: [PATCH] feat(tests): Add test cases for asset name handling Signed-off-by: Ferdinand Thiessen --- __tests__/appconfig.spec.ts | 42 ++++++++++++++++++++++++++++++++++--- __tests__/libconfig.spec.ts | 24 +++++++++++++++++++++ 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/__tests__/appconfig.spec.ts b/__tests__/appconfig.spec.ts index 88f4175..2261546 100644 --- a/__tests__/appconfig.spec.ts +++ b/__tests__/appconfig.spec.ts @@ -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)) @@ -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) }) diff --git a/__tests__/libconfig.spec.ts b/__tests__/libconfig.spec.ts index 9b8c3ab..2ab1c55 100644 --- a/__tests__/libconfig.spec.ts +++ b/__tests__/libconfig.spec.ts @@ -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({