Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(appconfig): cssCodeSplit is enabled by default so handle CSS entries #243

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions __tests__/appconfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ describe('app config', () => {
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')
expect(assetFileNames({ name: 'some.css' })).toMatch(/^css\/[^/]+\.css/)
expect(assetFileNames({ name: 'other/file.css' })).toMatch(/^css\/[^/]+\.css/)
})

it('moves image assets to img/', async () => {
Expand Down Expand Up @@ -79,13 +79,28 @@ describe('app config', () => {
})

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 resolved = await createConfig('build', 'development', { assetFileNames: (({ name }) => name === 'main.png' ? 'img/main.png' : 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')
expect(assetFileNames({ name: 'main.png' })).toBe('img/main.png')
expect(assetFileNames({ name: 'foo.png' })).toBe('img/[name][extname]')
})

it('extracts CSS by default with CSS entry points', async () => {
const resolved = await createConfig('build', 'development')

expect(resolved.build.cssCodeSplit).toBe(true)
expect(resolved.plugins.find(({ name }) => name === 'css-entry-points-plugin')).not.toBeUndefined()
})

it('does not add CSS entry points with CSS inject', async () => {
const resolved = await createConfig('build', 'development', { inlineCSS: true })

expect(resolved.build.cssCodeSplit).toBe(true)
// not found!
expect(resolved.plugins.find(({ name }) => name === 'css-entry-points-plugin')).toBeUndefined()
})

describe('inlining css', () => {
Expand Down
2 changes: 1 addition & 1 deletion lib/appConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
...(typeof options.inlineCSS === 'object' ? options.inlineCSS : {}),
})
plugins.push(...[plugin].flat())
} else if (userConfig.build?.cssCodeSplit) {
} else if (userConfig.build?.cssCodeSplit !== false) {
// If not inlining CSS and using `cssCodeSplit` we need this plugin to fix https://github.com/vitejs/vite/issues/17527
plugins.push(CSSEntryPointsPlugin({ createEmptyEntryPoints: options.createEmptyCSSEntryPoints }))
}
Expand All @@ -152,7 +152,7 @@
EmptyJSDirPlugin(
typeof options.emptyOutputDirectory === 'object'
? options.emptyOutputDirectory
: undefined

Check warning on line 155 in lib/appConfig.ts

View workflow job for this annotation

GitHub Actions / NPM lint

Missing trailing comma
),
)
}
Expand Down
Loading