-
Notifications
You must be signed in to change notification settings - Fork 390
Expand file tree
/
Copy pathloader.js
More file actions
270 lines (232 loc) · 6.69 KB
/
Copy pathloader.js
File metadata and controls
270 lines (232 loc) · 6.69 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* eslint-disable operator-linebreak */
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
// base on css-loader@6.7.1
const postcss = require('postcss')
const postcssPkg = require('postcss/package.json')
const { satisfies } = require('semver')
const CssSyntaxError = require('./CssSyntaxError')
const Warning = require('./Warning')
const schema = require('./options.json')
const { icssParser, importParser, urlParser } = require('./plugins')
const {
normalizeOptions,
shouldUseModulesPlugins,
shouldUseImportPlugin,
shouldUseURLPlugin,
shouldUseIcssPlugin,
getPreRequester,
getExportCode,
getFilter,
getImportCode,
getModuleCode,
getModulesPlugins,
normalizeSourceMap,
sort,
combineRequests,
stringifyRequest
} = require('./utils')
const createHelpers = require('../helpers')
module.exports = async function loader (content, map, meta) {
const rawOptions = this.getOptions(schema)
const plugins = []
const callback = this.async()
const mpx = this.getMpx()
const externals = mpx.externals
const root = mpx.projectRoot
const sourceMap = this.sourceMap
const isRN = ['ios', 'android', 'harmony'].includes(mpx.mode)
let options
try {
options = normalizeOptions(Object.assign({}, rawOptions, { sourceMap }), this)
} catch (error) {
callback(error)
return
}
const replacements = []
const exports = []
if (shouldUseModulesPlugins(options)) {
plugins.push(...getModulesPlugins(options, this))
}
const importPluginImports = []
const importPluginApi = []
let isSupportAbsoluteURL = false
// TODO enable by default in the next major release
if (
this._compilation &&
this._compilation.options &&
this._compilation.options.experiments &&
this._compilation.options.experiments.buildHttp
) {
isSupportAbsoluteURL = true
}
const isSupportDataURL =
options.esModule && Boolean('fsStartTime' in this._compiler)
if (shouldUseImportPlugin(options)) {
const { getRequestString } = createHelpers(this)
plugins.push(
importParser({
isSupportAbsoluteURL: false,
isSupportDataURL: false,
externals,
root,
isCSSStyleSheet: options.exportType === 'css-style-sheet',
loaderContext: this,
imports: importPluginImports,
api: importPluginApi,
filter: options.import.filter,
urlHandler: (url) => {
url = combineRequests(getPreRequester(this)(options.importLoaders), url)
if (isRN) {
return stringifyRequest(this, url)
}
return getRequestString('styles', { src: url }, {
isStatic: true,
issuerResource: this.resource,
fromImport: true
})
}
})
)
}
const urlPluginImports = []
if (shouldUseURLPlugin(options)) {
const needToResolveURL = !options.esModule
plugins.push(
urlParser({
isSupportAbsoluteURL,
isSupportDataURL,
externals,
root,
imports: urlPluginImports,
replacements,
context: this.context,
rootContext: this.rootContext,
filter: getFilter(options.url.filter, this.resourcePath),
resolver: needToResolveURL
? this.getResolve({ mainFiles: [], extensions: [] })
: // eslint-disable-next-line no-undefined
undefined,
urlHandler: (url) => stringifyRequest(this, url)
// Support data urls as input in new URL added in webpack@5.38.0
})
)
}
const icssPluginImports = []
const icssPluginApi = []
const needToUseIcssPlugin = shouldUseIcssPlugin(options)
if (needToUseIcssPlugin) {
plugins.push(
icssParser({
loaderContext: this,
imports: icssPluginImports,
api: icssPluginApi,
replacements,
exports,
urlHandler: (url) =>
stringifyRequest(
this,
combineRequests(getPreRequester(this)(options.importLoaders), url)
)
})
)
}
if (this.minimize && !isRN) {
const cssnano = require('cssnano')
const minimizeOptions = rawOptions.minimize || {}
const presetOptimisation = Object.assign(
{},
minimizeOptions.optimisation
)
let cssnanoConfig = {
preset: ['cssnano-preset-default', presetOptimisation]
}
if (minimizeOptions.advanced) {
cssnanoConfig = {
preset: ['cssnano-preset-advanced', presetOptimisation]
}
}
plugins.push(cssnano(cssnanoConfig))
}
// Reuse CSS AST (PostCSS AST e.g 'postcss-loader') to avoid reparsing
if (meta) {
const { ast } = meta
if (
ast &&
ast.type === 'postcss' &&
satisfies(ast.version, `^${postcssPkg.version}`)
) {
// eslint-disable-next-line no-param-reassign
content = ast.root
}
}
const { resourcePath } = this
let result
try {
result = await postcss(plugins).process(content, {
hideNothingWarning: true,
from: resourcePath,
to: resourcePath,
map: options.sourceMap
? {
prev: map ? normalizeSourceMap(map, resourcePath) : null,
inline: false,
annotation: false
}
: false
})
} catch (error) {
if (error.file) {
this.addDependency(error.file)
}
callback(
error.name === 'CssSyntaxError' ? new CssSyntaxError(error) : error
)
return
}
for (const warning of result.warnings()) {
this.emitWarning(new Warning(warning))
}
const imports = []
.concat(icssPluginImports.sort(sort))
.concat(importPluginImports.sort())
.concat(urlPluginImports.sort(sort))
const api = []
.concat(importPluginApi.sort(sort))
.concat(icssPluginApi.sort(sort))
if (options.modules.exportOnlyLocals !== true) {
imports.unshift({
type: 'api_import',
importName: '___CSS_LOADER_API_IMPORT___',
url: stringifyRequest(this, '!!' + require.resolve('./runtime/api'))
})
if (options.sourceMap) {
imports.unshift({
importName: '___CSS_LOADER_API_SOURCEMAP_IMPORT___',
url: stringifyRequest(this, '!!' + require.resolve('./runtime/sourceMaps'))
})
} else {
imports.unshift({
importName: '___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___',
url: stringifyRequest(this, '!!' + require.resolve('./runtime/noSourceMaps'))
})
}
}
const importCode = getImportCode(imports, options)
let moduleCode
try {
moduleCode = getModuleCode(result, api, replacements, options, isRN, this)
} catch (error) {
callback(error)
return
}
const exportCode = getExportCode(
exports,
replacements,
needToUseIcssPlugin,
options
)
callback(null, `${importCode}${moduleCode}${exportCode}`)
}