|
1 | 1 | import { pathToFileURL } from 'node:url' |
2 | 2 | import type { Plugin } from 'vite' |
3 | 3 | import { dirname, relative } from 'pathe' |
4 | | -import { genImport, genObjectFromRawEntries } from 'knitwork' |
| 4 | +import { genArrayFromRaw, genImport, genObjectFromRawEntries } from 'knitwork' |
5 | 5 | import { filename as _filename } from 'pathe/utils' |
6 | 6 | import { parseQuery, parseURL } from 'ufo' |
7 | 7 | import type { Component } from '@nuxt/schema' |
@@ -79,12 +79,15 @@ export function SSRStylesPlugin (options: SSRStylesPluginOptions): Plugin { |
79 | 79 |
|
80 | 80 | const baseDir = dirname(base) |
81 | 81 |
|
| 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)}` })) |
82 | 85 | emitted[file] = this.emitFile({ |
83 | 86 | type: 'asset', |
84 | 87 | name: `${fileName}-styles.mjs`, |
85 | 88 | 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))}`, |
88 | 91 | ].join('\n'), |
89 | 92 | }) |
90 | 93 | } |
|
0 commit comments