Skip to content

Commit

Permalink
fix: Move fonts to css/fonts and fix css path resolving
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Feb 6, 2024
1 parent 55fb69c commit 177ef1b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
12 changes: 12 additions & 0 deletions __tests__/appconfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ describe('app config', () => {
expect(assetFileNames({ name: 'some.ico' })).toBe('img/[name][extname]')
})

it('moves fonts to css/fonts', 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.woff' })).toBe('css/fonts/[name][extname]')
expect(assetFileNames({ name: 'some.woff2' })).toBe('css/fonts/[name][extname]')
expect(assetFileNames({ name: 'some.otf' })).toBe('css/fonts/[name][extname]')
expect(assetFileNames({ name: 'some.ttf' })).toBe('css/fonts/[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 })

Expand Down
9 changes: 8 additions & 1 deletion lib/appConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import type { UserConfig, UserConfigFn } from 'vite'
import type { BaseOptions, NodePolyfillsOptions } from './baseConfig.js'

import { relative } from 'node:path'
import { mergeConfig } from 'vite'
import { createBaseConfig } from './baseConfig.js'

Expand Down Expand Up @@ -113,7 +114,11 @@ export const createAppConfig = (entries: { [entryAlias: string]: string }, optio
// we need to set the base path, so module preloading works correctly
// currently this is hidden behind the `experimental` options.
experimental: {
renderBuiltUrl(filename) {
renderBuiltUrl(filename, { hostType }) {
if (hostType === 'css') {
// CSS `url()` does not support any dynamic path, so we use relative path to the css files
return relative('../css', `../${filename}`)
}
return {
// already contains the "js/" prefix as it is our output file configuration
runtime: `OC.filePath('${appName}', '', '${filename}')`,
Expand Down Expand Up @@ -145,6 +150,8 @@ export const createAppConfig = (entries: { [entryAlias: string]: string }, optio
return 'img/[name][extname]'
} else if (/css/i.test(extType)) {
return `css/${appNameSanitized}-[name].css`
} else if (/woff2?|ttf|otf/i.test(extType)) {
return 'css/fonts/[name][extname]'
}
return 'dist/[name]-[hash][extname]'
},
Expand Down

0 comments on commit 177ef1b

Please sign in to comment.