Skip to content

Commit 627c826

Browse files
committedDec 18, 2019
feat: css modules
1 parent 4f9fd84 commit 627c826

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed
 

‎src/cssModules.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export function genCSSModulesCode(
2+
id: string,
3+
index: number,
4+
request: string,
5+
moduleName: string | boolean,
6+
needsHotReload: boolean
7+
): string {
8+
const styleVar = `style${index}`
9+
let code = `\nimport ${styleVar} from ${request}`
10+
11+
// inject variable
12+
const name = typeof moduleName === 'string' ? moduleName : '$style'
13+
code += `\ncssModules["${name}"] = ${styleVar}`
14+
15+
if (needsHotReload) {
16+
code += `
17+
if (module.hot) {
18+
module.hot.accept(${request}, () => {
19+
cssModules["${name}"] = ${styleVar}
20+
__VUE_HMR_RUNTIME__.rerender("${id}")
21+
})
22+
}`
23+
}
24+
25+
return code
26+
}

‎src/index.ts

+20-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from '@vue/compiler-sfc'
1414
import { selectBlock } from './select'
1515
import { genHotReloadCode } from './hotReload'
16+
import { genCSSModulesCode } from './cssModules'
1617

1718
const VueLoaderPlugin = require('./plugin')
1819

@@ -97,7 +98,7 @@ const loader: webpack.loader.Loader = function(source) {
9798
const needsHotReload =
9899
!isServer &&
99100
!isProduction &&
100-
(descriptor.script || descriptor.template) &&
101+
!!(descriptor.script || descriptor.template) &&
101102
options.hotReload !== false
102103

103104
// template
@@ -121,12 +122,12 @@ const loader: webpack.loader.Loader = function(source) {
121122
const query = `?vue&type=script${attrsQuery}${inheritQuery}`
122123
const scriptRequest = stringifyRequest(src + query)
123124
scriptImport =
124-
`import script from ${scriptRequest}\n` +
125-
`export * from ${scriptRequest}` // support named exports
125+
`import script from ${scriptRequest}\n` + `export * from ${scriptRequest}` // support named exports
126126
}
127127

128128
// styles
129129
let stylesCode = ``
130+
let hasCSSModules = false
130131
if (descriptor.styles.length) {
131132
descriptor.styles.forEach((style: SFCStyleBlock, i: number) => {
132133
const src = style.src || resourcePath
@@ -137,8 +138,22 @@ const loader: webpack.loader.Loader = function(source) {
137138
const idQuery = style.scoped ? `&id=${id}` : ``
138139
const query = `?vue&type=style&index=${i}${idQuery}${attrsQuery}${inheritQuery}`
139140
const styleRequest = stringifyRequest(src + query)
140-
// TODO CSS Modules & SSR
141-
stylesCode += `import ${styleRequest}`
141+
if (style.module) {
142+
if (!hasCSSModules) {
143+
stylesCode += `const cssModules = script.__cssModules = {}`
144+
hasCSSModules = true
145+
}
146+
stylesCode += genCSSModulesCode(
147+
id,
148+
i,
149+
styleRequest,
150+
style.module,
151+
needsHotReload
152+
)
153+
} else {
154+
stylesCode += `\nimport ${styleRequest}`
155+
}
156+
// TODO SSR critical CSS collection
142157
})
143158
}
144159

0 commit comments

Comments
 (0)
Please sign in to comment.