-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
Copy pathstyle-loader.js
133 lines (110 loc) · 3.1 KB
/
style-loader.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
import path from 'path'
import ExtractCssChunksPlugin from 'extract-css-chunks-webpack-plugin'
import { wrapArray } from '@nuxt/utils'
import PostcssConfig from './postcss'
export default class StyleLoader {
constructor(buildContext, { isServer, perfLoader }) {
this.buildContext = buildContext
this.isServer = isServer
this.perfLoader = perfLoader
if (buildContext.options.build.postcss) {
this.postcssConfig = new PostcssConfig(buildContext)
}
}
get extractCSS() {
return this.buildContext.buildOptions.extractCSS
}
get exportOnlyLocals() {
return Boolean(this.isServer && this.extractCSS)
}
normalize(loaders) {
loaders = wrapArray(loaders)
return loaders.map(loader => (typeof loader === 'string' ? { loader } : loader))
}
styleResource(ext) {
const { buildOptions: { styleResources }, options: { rootDir } } = this.buildContext
const extResource = styleResources[ext]
// style-resources-loader
// https://github.com/yenshih/style-resources-loader
if (!extResource) {
return
}
const patterns = wrapArray(extResource).map(p => path.resolve(rootDir, p))
return {
loader: 'style-resources-loader',
options: Object.assign(
{ patterns },
styleResources.options || {}
)
}
}
postcss() {
// postcss-loader
// https://github.com/postcss/postcss-loader
if (!this.postcssConfig) {
return
}
const config = this.postcssConfig.config()
if (!config) {
return
}
return {
loader: 'postcss-loader',
options: Object.assign({ sourceMap: this.buildContext.buildOptions.cssSourceMap }, config)
}
}
css(options) {
options.exportOnlyLocals = this.exportOnlyLocals
const cssLoader = { loader: 'css-loader', options }
if (options.exportOnlyLocals) {
return [cssLoader]
}
return [this.styleLoader(), cssLoader]
}
cssModules(options) {
return this.css(Object.assign(options, { modules: true }))
}
extract() {
if (this.extractCSS) {
return {
loader: ExtractCssChunksPlugin.loader,
options: {
// TODO: https://github.com/faceyspacey/extract-css-chunks-webpack-plugin/issues/132
reloadAll: true
}
}
}
}
styleLoader() {
return this.extract() || {
loader: 'vue-style-loader',
options: this.buildContext.buildOptions.loaders.vueStyle
}
}
apply(ext, loaders = []) {
const { css, cssModules } = this.buildContext.buildOptions.loaders
const customLoaders = [].concat(
this.postcss(),
this.normalize(loaders),
this.styleResource(ext)
).filter(Boolean)
css.importLoaders = cssModules.importLoaders = customLoaders.length
return [
// This matches <style module>
{
resourceQuery: /module/,
use: this.perfLoader.css().concat(
this.cssModules(cssModules),
customLoaders
)
},
// This matches plain <style> or <style scoped>
{
use: this.perfLoader.css().concat(
this.css(css),
customLoaders
)
}
]
}
}