forked from quasarframework/quasar
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inject.style-rules.js
161 lines (140 loc) · 4.88 KB
/
inject.style-rules.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const ExtractLoader = require('mini-css-extract-plugin').loader
const merge = require('webpack-merge')
const path = require('path')
const appPaths = require('../app-paths')
const cssVariables = require('../helpers/css-variables')
const postCssConfig = require(appPaths.resolve.app('.postcssrc.js'))
const quasarCssPaths = [ path.join('node_modules', 'quasar'), path.join('node_modules', '@quasar') ]
function injectRule (chain, pref, lang, test, loader, loaderOptions) {
const baseRule = chain.module.rule(lang).test(test)
// rules for <style lang="module">
const modulesRule = baseRule.oneOf('modules-query').resourceQuery(/module/)
create(modulesRule, true)
// rules for *.module.* files
const modulesExtRule = baseRule.oneOf('modules-ext').test(/\.module\.\w+$/)
create(modulesExtRule, true)
const normalRule = baseRule.oneOf('normal')
create(normalRule, false)
function create (rule, modules) {
if (pref.serverExtract !== true) {
if (pref.extract) {
rule.use('mini-css-extract')
.loader(ExtractLoader)
.options({ publicPath: '../' })
}
else {
rule.use('vue-style-loader')
.loader('vue-style-loader')
.options({
sourceMap: pref.sourceMap
})
}
}
const cssLoaderOptions = {
sourceMap: pref.sourceMap,
onlyLocals: pref.serverExtract,
importLoaders:
(pref.serverExtract ? 0 : 1) + // stylePostLoader injected by vue-loader
1 + // postCSS loader
(!pref.extract && pref.minify ? 1 : 0) + // postCSS with cssnano
(loader ? (loader === 'stylus-loader' || loader === 'sass-loader' ? 2 : 1) : 0)
}
if (modules) {
Object.assign(cssLoaderOptions, {
modules: {
localIdentName: '[name]_[local]_[hash:base64:5]'
}
})
}
rule.use('css-loader')
.loader('css-loader')
.options(cssLoaderOptions)
if (!pref.extract && pref.minify) {
// needs to be applied separately,
// otherwise it messes up RTL
rule.use('cssnano')
.loader('postcss-loader')
.options({
sourceMap: pref.sourceMap,
plugins: [
require('cssnano')({
preset: [ 'default', {
mergeLonghand: false,
convertValues: false,
cssDeclarationSorter: false,
reduceTransforms: false
} ]
})
]
})
}
const postCssOpts = { sourceMap: pref.sourceMap, ...postCssConfig }
if (pref.rtl) {
const postcssRTL = require('postcss-rtl')
const postcssRTLOptions = pref.rtl === true ? {} : pref.rtl
if (
typeof postCssConfig.plugins !== 'function' &&
(postcssRTLOptions.fromRTL === true || typeof postcssRTLOptions === 'function')
) {
postCssConfig.plugins = postCssConfig.plugins || []
postCssOpts.plugins = ctx => {
const plugins = [ ...postCssConfig.plugins ]
const isClientCSS = quasarCssPaths.every(item => ctx.resourcePath.indexOf(item) === -1)
plugins.push(postcssRTL(
typeof postcssRTLOptions === 'function'
? postcssRTLOptions(isClientCSS, ctx.resourcePath)
: {
...postcssRTLOptions,
fromRTL: isClientCSS
}
))
return plugins
}
}
else {
postCssOpts.plugins.push(postcssRTL(postcssRTLOptions))
}
}
rule.use('postcss-loader')
.loader('postcss-loader')
.options(postCssOpts)
if (loader) {
rule.use(loader)
.loader(loader)
.options({
sourceMap: pref.sourceMap,
...loaderOptions
})
if (loader === 'stylus-loader') {
rule.use('quasar-stylus-variables-loader')
.loader(cssVariables.loaders.styl)
}
else if (loader === 'sass-loader') {
if (loaderOptions && loaderOptions.sassOptions && loaderOptions.sassOptions.indentedSyntax) {
rule.use('quasar-sass-variables-loader')
.loader(cssVariables.loaders.sass)
}
else {
rule.use('quasar-scss-variables-loader')
.loader(cssVariables.loaders.scss)
}
}
}
}
}
module.exports = function (chain, pref) {
injectRule(chain, pref, 'css', /\.css$/)
injectRule(chain, pref, 'stylus', /\.styl(us)?$/, 'stylus-loader', {
preferPathResolver: 'webpack',
...pref.stylusLoaderOptions
})
injectRule(chain, pref, 'scss', /\.scss$/, 'sass-loader', merge(
{ sassOptions: { outputStyle: /* required for RTL */ 'expanded' } },
pref.scssLoaderOptions
))
injectRule(chain, pref, 'sass', /\.sass$/, 'sass-loader', merge(
{ sassOptions: { indentedSyntax: true, outputStyle: /* required for RTL */ 'expanded' } },
pref.sassLoaderOptions
))
injectRule(chain, pref, 'less', /\.less$/, 'less-loader', pref.lessLoaderOptions)
}