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

feat(css-modules): added multiple modules support; re-write tests & add more css module scenarios #2045

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 9 additions & 2 deletions src/cssModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ export function genCSSModulesCode(

// inject variable
const name = typeof moduleName === 'string' ? moduleName : '$style'
code += `\ncssModules["${name}"] = ${styleVar}`

const moduleAdd = `
if(cssModules["${name}"]){
Object.assign(cssModules["${name}"], ${styleVar});
} else {
cssModules["${name}"] = ${styleVar};
}`
code += `${moduleAdd}`

if (needsHotReload) {
code += `
if (module.hot) {
module.hot.accept(${request}, () => {
cssModules["${name}"] = ${styleVar}
${moduleAdd}
__VUE_HMR_RUNTIME__.rerender("${id}")
})
}`
Expand Down
4 changes: 2 additions & 2 deletions test/advanced.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ test('support rules with oneOf', async () => {
)
expect(style).toContain('comp-a h2 {\n color: #f00;\n}')

const { window, instance } = await run('css-modules-simple.vue')
const { window, instance } = await run('css-modules/default.vue')

const className = instance.$style.red
expect(className).toMatch(/^red_\w{5}/)
expect(className).toMatch(/^red_[\w-+]{5}/)
style = normalizeNewline(window.document.querySelector('style')!.textContent!)
expect(style).toContain('.' + className + ' {\n color: red;\n}')
})
Expand Down
20 changes: 0 additions & 20 deletions test/fixtures/css-modules.vue

This file was deleted.

16 changes: 16 additions & 0 deletions test/fixtures/css-modules/default-extend-composes-css.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<style module>
.black {
composes: red from './red.css';
font-weight: bold;
}
</style>

<script>
/* composes directly import a class in style */
export default {
}
</script>

<template>
<div>{{ $style.black }}</div>
</template>
17 changes: 17 additions & 0 deletions test/fixtures/css-modules/default-extend-diffclass.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<style module>
.black {
color: #000000;
}
</style>

<script>
import CssModuleSimple from './default.vue'

export default {
extends: CssModuleSimple
}
</script>

<template>
<div>{{ $style.red }} | {{ $style.black }}</div>
</template>
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
</style>

<script>
import CssModuleSimple from './css-modules-simple.vue'
import CssModuleSimple from './default.vue'

export default {
extends: CssModuleSimple
}
</script>

<template>
<div :class="$style.red"></div>
</template>
<div>{{ $style.red }}</div>
</template>
19 changes: 19 additions & 0 deletions test/fixtures/css-modules/default-multiple.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<style module>
.red {
color: red;
}
</style>

<style module>
.blue {
color: blue;
}
</style>

<script>
export default {}
</script>

<template>
<div>{{ $style }}</div>
</template>
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
<script>
export default {}
</script>

<template>
<div>{{ $style }}</div>
</template>
19 changes: 19 additions & 0 deletions test/fixtures/css-modules/named-multiple-diffname-diffclass.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<style module="name1">
.red {
color: red;
}
</style>

<style module="name2">
.blue {
color: blue;
}
</style>

<script>
export default {}
</script>

<template>
<div>{{ name1 }} | {{ name2 }}</div>
</template>
19 changes: 19 additions & 0 deletions test/fixtures/css-modules/named-multiple-diffname-sameclass.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<style module="name1">
.red {
color: red;
}
</style>

<style module="name2">
.red {
color: red;
}
</style>

<script>
export default {}
</script>

<template>
<div>{{ name1 }} | {{ name2 }}</div>
</template>
19 changes: 19 additions & 0 deletions test/fixtures/css-modules/named-multiple-samename-diffclass.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<style module="named">
.red {
color: red;
}
</style>

<style module="named">
.blue {
color: blue;
}
</style>

<script>
export default {}
</script>

<template>
<div>{{ named }}</div>
</template>
19 changes: 19 additions & 0 deletions test/fixtures/css-modules/named-multiple-samename-sameclass.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<style module="named">
.red {
color: red;
}
</style>

<style module="named">
.red {
color: blue;
}
</style>

<script>
export default {}
</script>

<template>
<div>{{ named }}</div>
</template>
13 changes: 13 additions & 0 deletions test/fixtures/css-modules/named.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<style module="named">
.red {
color: red;
}
</style>

<script>
export default {}
</script>

<template>
<div>{{ named }}</div>
</template>
3 changes: 3 additions & 0 deletions test/fixtures/css-modules/red.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.red {
color: red;
}
Loading