index.js
5.59 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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = loader;
var _postcss = _interopRequireDefault(require("postcss"));
var _package = _interopRequireDefault(require("postcss/package.json"));
var _semver = require("semver");
var _CssSyntaxError = _interopRequireDefault(require("./CssSyntaxError"));
var _Warning = _interopRequireDefault(require("./Warning"));
var _options = _interopRequireDefault(require("./options.json"));
var _plugins = require("./plugins");
var _utils = require("./utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
async function loader(content, map, meta) {
const rawOptions = this.getOptions(_options.default);
const plugins = [];
const callback = this.async();
let options;
try {
options = (0, _utils.normalizeOptions)(rawOptions, this);
} catch (error) {
callback(error);
return;
}
const replacements = [];
const exports = [];
if ((0, _utils.shouldUseModulesPlugins)(options)) {
plugins.push(...(0, _utils.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 ((0, _utils.shouldUseImportPlugin)(options)) {
plugins.push((0, _plugins.importParser)({
isSupportAbsoluteURL: false,
isSupportDataURL: false,
isCSSStyleSheet: options.exportType === "css-style-sheet",
loaderContext: this,
imports: importPluginImports,
api: importPluginApi,
filter: options.import.filter,
urlHandler: url => (0, _utils.stringifyRequest)(this, (0, _utils.combineRequests)((0, _utils.getPreRequester)(this)(options.importLoaders), url))
}));
}
const urlPluginImports = [];
if ((0, _utils.shouldUseURLPlugin)(options)) {
const needToResolveURL = !options.esModule;
plugins.push((0, _plugins.urlParser)({
isSupportAbsoluteURL,
isSupportDataURL,
imports: urlPluginImports,
replacements,
context: this.context,
rootContext: this.rootContext,
filter: (0, _utils.getFilter)(options.url.filter, this.resourcePath),
resolver: needToResolveURL ? this.getResolve({
mainFiles: [],
extensions: []
}) :
// eslint-disable-next-line no-undefined
undefined,
urlHandler: url => (0, _utils.stringifyRequest)(this, url)
// Support data urls as input in new URL added in webpack@5.38.0
}));
}
const icssPluginImports = [];
const icssPluginApi = [];
const needToUseIcssPlugin = (0, _utils.shouldUseIcssPlugin)(options);
if (needToUseIcssPlugin) {
plugins.push((0, _plugins.icssParser)({
loaderContext: this,
imports: icssPluginImports,
api: icssPluginApi,
replacements,
exports,
urlHandler: url => (0, _utils.stringifyRequest)(this, (0, _utils.combineRequests)((0, _utils.getPreRequester)(this)(options.importLoaders), url))
}));
}
// Reuse CSS AST (PostCSS AST e.g 'postcss-loader') to avoid reparsing
if (meta) {
const {
ast
} = meta;
if (ast && ast.type === "postcss" && (0, _semver.satisfies)(ast.version, `^${_package.default.version}`)) {
// eslint-disable-next-line no-param-reassign
content = ast.root;
}
}
const {
resourcePath
} = this;
let result;
try {
result = await (0, _postcss.default)(plugins).process(content, {
hideNothingWarning: true,
from: resourcePath,
to: resourcePath,
map: options.sourceMap ? {
prev: map ? (0, _utils.normalizeSourceMap)(map, resourcePath) : null,
inline: false,
annotation: false
} : false
});
} catch (error) {
if (error.file) {
this.addDependency(error.file);
}
callback(error.name === "CssSyntaxError" ? new _CssSyntaxError.default(error) : error);
return;
}
for (const warning of result.warnings()) {
this.emitWarning(new _Warning.default(warning));
}
const imports = [].concat(icssPluginImports.sort(_utils.sort)).concat(importPluginImports.sort(_utils.sort)).concat(urlPluginImports.sort(_utils.sort));
const api = [].concat(importPluginApi.sort(_utils.sort)).concat(icssPluginApi.sort(_utils.sort));
if (options.modules.exportOnlyLocals !== true) {
imports.unshift({
type: "api_import",
importName: "___CSS_LOADER_API_IMPORT___",
url: (0, _utils.stringifyRequest)(this, require.resolve("./runtime/api"))
});
if (options.sourceMap) {
imports.unshift({
importName: "___CSS_LOADER_API_SOURCEMAP_IMPORT___",
url: (0, _utils.stringifyRequest)(this, require.resolve("./runtime/sourceMaps"))
});
} else {
imports.unshift({
importName: "___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___",
url: (0, _utils.stringifyRequest)(this, require.resolve("./runtime/noSourceMaps"))
});
}
}
const importCode = (0, _utils.getImportCode)(imports, options);
let moduleCode;
try {
moduleCode = (0, _utils.getModuleCode)(result, api, replacements, options, this);
} catch (error) {
callback(error);
return;
}
const exportCode = (0, _utils.getExportCode)(exports, replacements, needToUseIcssPlugin, options);
callback(null, `${importCode}${moduleCode}${exportCode}`);
}