UNPKG

1.35 kBJavaScriptView Raw
1const HandleCSSLoader = require('webpack-handle-css-loader')
2
3const LANGS = ['css', 'stylus', 'styl', 'sass', 'scss', 'less']
4
5exports.vue = function (options) {
6 const handleLoader = new HandleCSSLoader(options)
7 return handleLoader.vue()
8}
9
10// Generate loaders for standalone style files (outside of .vue)
11exports.standalone = function (config, options) {
12 const handleLoader = new HandleCSSLoader(options)
13
14 for (const lang of LANGS) {
15 const rule = handleLoader[lang]()
16 const context = config.module
17 .rule(lang)
18 .test(rule.test)
19 .include
20 .add(filepath => {
21 // Not ends with `.module.xxx`
22 return !/\.module\.[a-z]+$/.test(filepath)
23 })
24 .end()
25
26 rule.use.forEach(use => {
27 context
28 .use(use.loader)
29 .loader(use.loader)
30 .options(use.options)
31 })
32 }
33
34 handleLoader.set('cssModules', true)
35
36 const cssModulesLangs = LANGS.map(lang => [lang, new RegExp(`\\.module\\.${lang}`)])
37
38 for (const cssModulesLang of cssModulesLangs) {
39 const [lang, test] = cssModulesLang
40
41 const rule = handleLoader[lang](test)
42 const context = config.module
43 .rule(`${lang}-module`)
44 .test(rule.test)
45
46 rule.use.forEach(use => {
47 context
48 .use(use.loader)
49 .loader(use.loader)
50 .options(use.options)
51 })
52 }
53}