diff --git a/packages/plugin-vue/src/main.ts b/packages/plugin-vue/src/main.ts
index 446a2073..ecca81e5 100644
--- a/packages/plugin-vue/src/main.ts
+++ b/packages/plugin-vue/src/main.ts
@@ -12,6 +12,7 @@ import {
createDescriptor,
getDescriptor,
getPrevDescriptor,
+ getSrcDescriptor,
setSrcDescriptor,
} from './utils/descriptorCache'
import {
@@ -434,13 +435,45 @@ async function genStyleCode(
if (descriptor.styles.length) {
for (let i = 0; i < descriptor.styles.length; i++) {
const style = descriptor.styles[i]
+ let indexQuery = i
if (style.src) {
- await linkSrcToDescriptor(
- style.src,
- descriptor,
- pluginContext,
- style.scoped,
- )
+ const resolvedSrc =
+ (await pluginContext.resolve(style.src, descriptor.filename))?.id ||
+ style.src
+ const alreadyDescriptor = getSrcDescriptor(resolvedSrc, {
+ scoped: style.scoped,
+ src: descriptor.id,
+ })
+
+ if (alreadyDescriptor) {
+ const foundIndex = await alreadyDescriptor.styles.reduce(
+ async (acc, { scoped, src }, index) => {
+ const prevRes = await acc
+
+ if (~prevRes) return prevRes
+ if (scoped !== style.scoped) return prevRes
+ if (!src) return prevRes
+
+ const _resolvedSrc =
+ (await pluginContext.resolve(src, alreadyDescriptor.filename))
+ ?.id || src
+
+ if (_resolvedSrc !== resolvedSrc) return prevRes
+
+ return index
+ },
+ Promise.resolve(-1),
+ )
+
+ if (~foundIndex) indexQuery = foundIndex
+ } else {
+ await linkSrcToDescriptor(
+ style.src,
+ descriptor,
+ pluginContext,
+ style.scoped,
+ )
+ }
}
const src = style.src || descriptor.filename
// do not include module in default query, since we use it to indicate
@@ -453,7 +486,7 @@ async function genStyleCode(
: ''
const directQuery = customElement ? `&inline` : ``
const scopedQuery = style.scoped ? `&scoped=${descriptor.id}` : ``
- const query = `?vue&type=style&index=${i}${srcQuery}${directQuery}${scopedQuery}`
+ const query = `?vue&type=style&index=${indexQuery}${srcQuery}${directQuery}${scopedQuery}`
const styleRequest = src + query + attrsQuery
if (style.module) {
if (customElement) {
diff --git a/playground/vue-custom-id/package.json b/playground/vue-custom-id/package.json
index aad14496..40430877 100644
--- a/playground/vue-custom-id/package.json
+++ b/playground/vue-custom-id/package.json
@@ -2,6 +2,7 @@
"name": "@vitejs/test-vue-custom-id",
"private": true,
"version": "0.0.0",
+ "type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
diff --git a/playground/vue/src-import/SrcImport.vue b/playground/vue/src-import/SrcImport.vue
index d70e1f48..3cadcbee 100644
--- a/playground/vue/src-import/SrcImport.vue
+++ b/playground/vue/src-import/SrcImport.vue
@@ -1,4 +1,8 @@
+
+
+
+
diff --git a/playground/vue/src-import/bar.css b/playground/vue/src-import/bar.css
new file mode 100644
index 00000000..e69de29b
diff --git a/playground/vue/src-import/foo.css b/playground/vue/src-import/foo.css
new file mode 100644
index 00000000..e69de29b
diff --git a/playground/vue/src-import/script.ts b/playground/vue/src-import/script.ts
index 1a9f7ec3..f4ef2682 100644
--- a/playground/vue/src-import/script.ts
+++ b/playground/vue/src-import/script.ts
@@ -1,6 +1,8 @@
import { defineComponent } from 'vue'
import SrcImportStyle from './srcImportStyle.vue'
import SrcImportStyle2 from './srcImportStyle2.vue'
+import SrcImportScopedStyle from './srcImportScopedStyle.vue'
+import SrcImportScopedStyle2 from './srcImportScopedStyle2.vue'
import SrcImportModuleStyle from './srcImportModuleStyle.vue'
import SrcImportModuleStyle2 from './srcImportModuleStyle2.vue'
@@ -8,6 +10,8 @@ export default defineComponent({
components: {
SrcImportStyle,
SrcImportStyle2,
+ SrcImportScopedStyle,
+ SrcImportScopedStyle2,
SrcImportModuleStyle,
SrcImportModuleStyle2,
},
diff --git a/playground/vue/src-import/srcImportScopedStyle.vue b/playground/vue/src-import/srcImportScopedStyle.vue
new file mode 100644
index 00000000..de917698
--- /dev/null
+++ b/playground/vue/src-import/srcImportScopedStyle.vue
@@ -0,0 +1,7 @@
+
+
+