1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = loader;
|
7 |
|
8 | var _postcss = _interopRequireDefault(require("postcss"));
|
9 |
|
10 | var _package = _interopRequireDefault(require("postcss/package.json"));
|
11 |
|
12 | var _semver = require("semver");
|
13 |
|
14 | var _CssSyntaxError = _interopRequireDefault(require("./CssSyntaxError"));
|
15 |
|
16 | var _Warning = _interopRequireDefault(require("./Warning"));
|
17 |
|
18 | var _options = _interopRequireDefault(require("./options.json"));
|
19 |
|
20 | var _plugins = require("./plugins");
|
21 |
|
22 | var _utils = require("./utils");
|
23 |
|
24 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | async function loader(content, map, meta) {
|
31 | const rawOptions = this.getOptions(_options.default);
|
32 | const plugins = [];
|
33 | const callback = this.async();
|
34 | let options;
|
35 |
|
36 | try {
|
37 | options = (0, _utils.normalizeOptions)(rawOptions, this);
|
38 | } catch (error) {
|
39 | callback(error);
|
40 | return;
|
41 | }
|
42 |
|
43 | const replacements = [];
|
44 | const exports = [];
|
45 |
|
46 | if ((0, _utils.shouldUseModulesPlugins)(options)) {
|
47 | plugins.push(...(0, _utils.getModulesPlugins)(options, this));
|
48 | }
|
49 |
|
50 | const importPluginImports = [];
|
51 | const importPluginApi = [];
|
52 |
|
53 | if ((0, _utils.shouldUseImportPlugin)(options)) {
|
54 | plugins.push((0, _plugins.importParser)({
|
55 | isCSSStyleSheet: options.exportType === "css-style-sheet",
|
56 | loaderContext: this,
|
57 | imports: importPluginImports,
|
58 | api: importPluginApi,
|
59 | filter: options.import.filter,
|
60 | urlHandler: url => (0, _utils.stringifyRequest)(this, (0, _utils.combineRequests)((0, _utils.getPreRequester)(this)(options.importLoaders), url))
|
61 | }));
|
62 | }
|
63 |
|
64 | const urlPluginImports = [];
|
65 |
|
66 | if ((0, _utils.shouldUseURLPlugin)(options)) {
|
67 | const needToResolveURL = !options.esModule;
|
68 | const isSupportDataURLInNewURL = options.esModule && Boolean("fsStartTime" in this._compiler);
|
69 | plugins.push((0, _plugins.urlParser)({
|
70 | imports: urlPluginImports,
|
71 | replacements,
|
72 | context: this.context,
|
73 | rootContext: this.rootContext,
|
74 | filter: (0, _utils.getFilter)(options.url.filter, this.resourcePath),
|
75 | needToResolveURL,
|
76 | resolver: needToResolveURL ? this.getResolve({
|
77 | mainFiles: [],
|
78 | extensions: []
|
79 | }) :
|
80 | undefined,
|
81 | urlHandler: url => (0, _utils.stringifyRequest)(this, url),
|
82 |
|
83 | isSupportDataURLInNewURL
|
84 | }));
|
85 | }
|
86 |
|
87 | const icssPluginImports = [];
|
88 | const icssPluginApi = [];
|
89 | const needToUseIcssPlugin = (0, _utils.shouldUseIcssPlugin)(options);
|
90 |
|
91 | if (needToUseIcssPlugin) {
|
92 | plugins.push((0, _plugins.icssParser)({
|
93 | loaderContext: this,
|
94 | imports: icssPluginImports,
|
95 | api: icssPluginApi,
|
96 | replacements,
|
97 | exports,
|
98 | urlHandler: url => (0, _utils.stringifyRequest)(this, (0, _utils.combineRequests)((0, _utils.getPreRequester)(this)(options.importLoaders), url))
|
99 | }));
|
100 | }
|
101 |
|
102 |
|
103 | if (meta) {
|
104 | const {
|
105 | ast
|
106 | } = meta;
|
107 |
|
108 | if (ast && ast.type === "postcss" && (0, _semver.satisfies)(ast.version, `^${_package.default.version}`)) {
|
109 |
|
110 | content = ast.root;
|
111 | }
|
112 | }
|
113 |
|
114 | const {
|
115 | resourcePath
|
116 | } = this;
|
117 | let result;
|
118 |
|
119 | try {
|
120 | result = await (0, _postcss.default)(plugins).process(content, {
|
121 | hideNothingWarning: true,
|
122 | from: resourcePath,
|
123 | to: resourcePath,
|
124 | map: options.sourceMap ? {
|
125 | prev: map ? (0, _utils.normalizeSourceMap)(map, resourcePath) : null,
|
126 | inline: false,
|
127 | annotation: false
|
128 | } : false
|
129 | });
|
130 | } catch (error) {
|
131 | if (error.file) {
|
132 | this.addDependency(error.file);
|
133 | }
|
134 |
|
135 | callback(error.name === "CssSyntaxError" ? new _CssSyntaxError.default(error) : error);
|
136 | return;
|
137 | }
|
138 |
|
139 | for (const warning of result.warnings()) {
|
140 | this.emitWarning(new _Warning.default(warning));
|
141 | }
|
142 |
|
143 | const imports = [].concat(icssPluginImports.sort(_utils.sort)).concat(importPluginImports.sort(_utils.sort)).concat(urlPluginImports.sort(_utils.sort));
|
144 | const api = [].concat(importPluginApi.sort(_utils.sort)).concat(icssPluginApi.sort(_utils.sort));
|
145 |
|
146 | if (options.modules.exportOnlyLocals !== true) {
|
147 | imports.unshift({
|
148 | type: "api_import",
|
149 | importName: "___CSS_LOADER_API_IMPORT___",
|
150 | url: (0, _utils.stringifyRequest)(this, require.resolve("./runtime/api"))
|
151 | });
|
152 |
|
153 | if (options.sourceMap) {
|
154 | imports.unshift({
|
155 | importName: "___CSS_LOADER_API_SOURCEMAP_IMPORT___",
|
156 | url: (0, _utils.stringifyRequest)(this, require.resolve("./runtime/sourceMaps"))
|
157 | });
|
158 | } else {
|
159 | imports.unshift({
|
160 | importName: "___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___",
|
161 | url: (0, _utils.stringifyRequest)(this, require.resolve("./runtime/noSourceMaps"))
|
162 | });
|
163 | }
|
164 | }
|
165 |
|
166 | const importCode = (0, _utils.getImportCode)(imports, options);
|
167 | let moduleCode;
|
168 |
|
169 | try {
|
170 | moduleCode = (0, _utils.getModuleCode)(result, api, replacements, options, this);
|
171 | } catch (error) {
|
172 | callback(error);
|
173 | return;
|
174 | }
|
175 |
|
176 | const exportCode = (0, _utils.getExportCode)(exports, replacements, needToUseIcssPlugin, options);
|
177 | callback(null, `${importCode}${moduleCode}${exportCode}`);
|
178 | } |
\ | No newline at end of file |