Skip to content

Commit 0b36a2c

Browse files
Jinjiangyyx990803
authored andcommitted
fix: removed module id for non-scoped component (#1146)
* removed module id for non-scoped component (close #853) * improved test case for style import twice * Update helpers.js
1 parent 1b554b6 commit 0b36a2c

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

lib/helpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function resolveLoaders (
9595
const templateCompilerOptions =
9696
'?' +
9797
JSON.stringify({
98-
id: moduleId,
98+
id: hasScoped ? moduleId : undefined,
9999
hasScoped,
100100
hasComment,
101101
optionsId,
@@ -310,7 +310,7 @@ module.exports = function createHelpers (
310310
// a marker for vue-style-loader to know that this is an import from a vue file
311311
optionsId,
312312
vue: true,
313-
id: moduleId,
313+
id: scoped ? moduleId : undefined,
314314
scoped: !!scoped,
315315
sourceMap: needCssSourceMap
316316
}) +
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template><div></div></template>
2+
<style src="./style-import.css"></style>
3+
<style src="./style-import-scoped.css" scoped></style>

test/fixtures/style-import-twice.vue

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<template><SubComponent></SubComponent></template>
2+
<style src="./style-import.css"></style>
3+
<style src="./style-import-scoped.css" scoped></style>
4+
<script>
5+
import SubComponent from './style-import-twice-sub.vue'
6+
export default {
7+
components: { SubComponent }
8+
}
9+
</script>

test/test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,19 @@ function interopDefault (module) {
121121
: module
122122
}
123123

124+
function initStylesForAllSubComponents (module) {
125+
if (module.components) {
126+
for (const name in module.components) {
127+
const sub = module.components[name]
128+
const instance = {}
129+
if (sub && sub.beforeCreate) {
130+
sub.beforeCreate.forEach(hook => hook.call(instance))
131+
}
132+
initStylesForAllSubComponents(sub)
133+
}
134+
}
135+
}
136+
124137
describe('vue-loader', () => {
125138
it('basic', done => {
126139
test({
@@ -256,6 +269,23 @@ describe('vue-loader', () => {
256269
})
257270
})
258271

272+
it('style import for a same file twice', done => {
273+
test({
274+
entry: 'style-import-twice.vue'
275+
}, (window, module) => {
276+
initStylesForAllSubComponents(module)
277+
const styles = window.document.querySelectorAll('style')
278+
expect(styles.length).to.equal(3)
279+
expect(styles[0].textContent).to.contain('h1 { color: red;\n}')
280+
// import with scoped
281+
const id = 'data-v-' + genId('style-import-twice.vue')
282+
expect(styles[1].textContent).to.contain('h1[' + id + '] { color: green;\n}')
283+
const id2 = 'data-v-' + genId('style-import-twice-sub.vue')
284+
expect(styles[2].textContent).to.contain('h1[' + id2 + '] { color: green;\n}')
285+
done()
286+
})
287+
})
288+
259289
it('template import', done => {
260290
test({
261291
entry: 'template-import.vue'

0 commit comments

Comments
 (0)