Skip to content

Commit 3efc4f7

Browse files
committed
fix: duplicate server style imports
1 parent cb2b9d5 commit 3efc4f7

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

packages/vite/src/plugins/ssr-styles.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { pathToFileURL } from 'node:url'
22
import type { Plugin } from 'vite'
33
import { dirname, relative } from 'pathe'
4-
import { genImport, genObjectFromRawEntries } from 'knitwork'
4+
import { genArrayFromRaw, genImport, genObjectFromRawEntries } from 'knitwork'
55
import { filename as _filename } from 'pathe/utils'
66
import { parseQuery, parseURL } from 'ufo'
77
import type { Component } from '@nuxt/schema'
@@ -79,12 +79,15 @@ export function SSRStylesPlugin (options: SSRStylesPluginOptions): Plugin {
7979

8080
const baseDir = dirname(base)
8181

82+
// prevent duplicate files
83+
const uniqueFiles = [...new Set(files.map(css => this.getFileName(css)))]
84+
const styleImports = uniqueFiles.map((file, i) => ({ name: `style_${i}`, path: `./${relative(baseDir, file)}` }))
8285
emitted[file] = this.emitFile({
8386
type: 'asset',
8487
name: `${fileName}-styles.mjs`,
8588
source: [
86-
...files.map((css, i) => `import style_${i} from './${relative(baseDir, this.getFileName(css))}';`),
87-
`export default [${files.map((_, i) => `style_${i}`).join(', ')}]`,
89+
...styleImports.map(i => genImport(i.path, i.name)),
90+
`export default ${genArrayFromRaw(styleImports.map(i => i.name))}`,
8891
].join('\n'),
8992
})
9093
}

0 commit comments

Comments
 (0)