1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = loader;
|
---|
7 |
|
---|
8 | var _path = _interopRequireDefault(require("path"));
|
---|
9 |
|
---|
10 | var _semver = require("semver");
|
---|
11 |
|
---|
12 | var _package = _interopRequireDefault(require("postcss/package.json"));
|
---|
13 |
|
---|
14 | var _Warning = _interopRequireDefault(require("./Warning"));
|
---|
15 |
|
---|
16 | var _Error = _interopRequireDefault(require("./Error"));
|
---|
17 |
|
---|
18 | var _options = _interopRequireDefault(require("./options.json"));
|
---|
19 |
|
---|
20 | var _utils = require("./utils");
|
---|
21 |
|
---|
22 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
23 |
|
---|
24 | let hasExplicitDependencyOnPostCSS = false;
|
---|
25 | /**
|
---|
26 | * **PostCSS Loader**
|
---|
27 | *
|
---|
28 | * Loads && processes CSS with [PostCSS](https://github.com/postcss/postcss)
|
---|
29 | *
|
---|
30 | * @method loader
|
---|
31 | *
|
---|
32 | * @param {String} content Source
|
---|
33 | * @param {Object} sourceMap Source Map
|
---|
34 | * @param {Object} meta Meta
|
---|
35 | *
|
---|
36 | * @return {callback} callback Result
|
---|
37 | */
|
---|
38 |
|
---|
39 | async function loader(content, sourceMap, meta) {
|
---|
40 | const options = this.getOptions(_options.default);
|
---|
41 | const callback = this.async();
|
---|
42 | const configOption = typeof options.postcssOptions === "undefined" || typeof options.postcssOptions.config === "undefined" ? true : options.postcssOptions.config;
|
---|
43 | const postcssFactory = (0, _utils.getPostcssImplementation)(this, options.implementation);
|
---|
44 |
|
---|
45 | if (!postcssFactory) {
|
---|
46 | callback(new Error(`The Postcss implementation "${options.implementation}" not found`));
|
---|
47 | return;
|
---|
48 | }
|
---|
49 |
|
---|
50 | let loadedConfig;
|
---|
51 |
|
---|
52 | if (configOption) {
|
---|
53 | try {
|
---|
54 | loadedConfig = await (0, _utils.loadConfig)(this, configOption, options.postcssOptions);
|
---|
55 | } catch (error) {
|
---|
56 | callback(error);
|
---|
57 | return;
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | const useSourceMap = typeof options.sourceMap !== "undefined" ? options.sourceMap : this.sourceMap;
|
---|
62 | const {
|
---|
63 | plugins,
|
---|
64 | processOptions
|
---|
65 | } = await (0, _utils.getPostcssOptions)(this, loadedConfig, options.postcssOptions);
|
---|
66 |
|
---|
67 | if (useSourceMap) {
|
---|
68 | processOptions.map = {
|
---|
69 | inline: false,
|
---|
70 | annotation: false,
|
---|
71 | ...processOptions.map
|
---|
72 | };
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (sourceMap && processOptions.map) {
|
---|
76 | processOptions.map.prev = (0, _utils.normalizeSourceMap)(sourceMap, this.context);
|
---|
77 | }
|
---|
78 |
|
---|
79 | let root; // Reuse PostCSS AST from other loaders
|
---|
80 |
|
---|
81 | if (meta && meta.ast && meta.ast.type === "postcss" && (0, _semver.satisfies)(meta.ast.version, `^${_package.default.version}`)) {
|
---|
82 | ({
|
---|
83 | root
|
---|
84 | } = meta.ast);
|
---|
85 | }
|
---|
86 |
|
---|
87 | if (!root && options.execute) {
|
---|
88 | // eslint-disable-next-line no-param-reassign
|
---|
89 | content = (0, _utils.exec)(content, this);
|
---|
90 | }
|
---|
91 |
|
---|
92 | let result;
|
---|
93 | let processor;
|
---|
94 |
|
---|
95 | try {
|
---|
96 | processor = postcssFactory(plugins);
|
---|
97 | result = await processor.process(root || content, processOptions);
|
---|
98 | } catch (error) {
|
---|
99 | // Check postcss versions to avoid using PostCSS 7.
|
---|
100 | // For caching reasons, we use the readFileSync and existsSync functions from the context,
|
---|
101 | // not the functions from the `fs` module.
|
---|
102 | if (!hasExplicitDependencyOnPostCSS && processor && processor.version && processor.version.startsWith("7.")) {
|
---|
103 | // The `findPackageJsonDir` function returns `string` or `null`.
|
---|
104 | // This is used to do for caching, that is, an explicit comparison with `undefined`
|
---|
105 | // is used to make the condition body run once.
|
---|
106 | const packageJSONDir = (0, _utils.findPackageJSONDir)(process.cwd(), this.fs.statSync);
|
---|
107 |
|
---|
108 | if (packageJSONDir) {
|
---|
109 | let bufferOfPackageJSON;
|
---|
110 |
|
---|
111 | try {
|
---|
112 | bufferOfPackageJSON = this.fs.readFileSync(_path.default.resolve(packageJSONDir, "package.json"), "utf8");
|
---|
113 | } catch (_error) {// Nothing
|
---|
114 | }
|
---|
115 |
|
---|
116 | if (bufferOfPackageJSON) {
|
---|
117 | let pkg;
|
---|
118 |
|
---|
119 | try {
|
---|
120 | pkg = JSON.parse(bufferOfPackageJSON);
|
---|
121 | } catch (_error) {// Nothing
|
---|
122 | }
|
---|
123 |
|
---|
124 | if (pkg) {
|
---|
125 | if (!pkg.dependencies.postcss && !pkg.devDependencies.postcss) {
|
---|
126 | this.emitWarning(new Error("Add postcss as project dependency. postcss is not a peer dependency for postcss-loader. " + "Use `npm install postcss` or `yarn add postcss`"));
|
---|
127 | } else {
|
---|
128 | hasExplicitDependencyOnPostCSS = true;
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | if (error.file) {
|
---|
136 | this.addDependency(error.file);
|
---|
137 | }
|
---|
138 |
|
---|
139 | if (error.name === "CssSyntaxError") {
|
---|
140 | callback(new _Error.default(error));
|
---|
141 | } else {
|
---|
142 | callback(error);
|
---|
143 | }
|
---|
144 |
|
---|
145 | return;
|
---|
146 | }
|
---|
147 |
|
---|
148 | for (const warning of result.warnings()) {
|
---|
149 | this.emitWarning(new _Warning.default(warning));
|
---|
150 | }
|
---|
151 |
|
---|
152 | for (const message of result.messages) {
|
---|
153 | // eslint-disable-next-line default-case
|
---|
154 | switch (message.type) {
|
---|
155 | case "dependency":
|
---|
156 | this.addDependency(message.file);
|
---|
157 | break;
|
---|
158 |
|
---|
159 | case "build-dependency":
|
---|
160 | this.addBuildDependency(message.file);
|
---|
161 | break;
|
---|
162 |
|
---|
163 | case "missing-dependency":
|
---|
164 | this.addMissingDependency(message.file);
|
---|
165 | break;
|
---|
166 |
|
---|
167 | case "context-dependency":
|
---|
168 | this.addContextDependency(message.file);
|
---|
169 | break;
|
---|
170 |
|
---|
171 | case "dir-dependency":
|
---|
172 | this.addContextDependency(message.dir);
|
---|
173 | break;
|
---|
174 |
|
---|
175 | case "asset":
|
---|
176 | if (message.content && message.file) {
|
---|
177 | this.emitFile(message.file, message.content, message.sourceMap, message.info);
|
---|
178 | }
|
---|
179 |
|
---|
180 | }
|
---|
181 | } // eslint-disable-next-line no-undefined
|
---|
182 |
|
---|
183 |
|
---|
184 | let map = result.map ? result.map.toJSON() : undefined;
|
---|
185 |
|
---|
186 | if (map && useSourceMap) {
|
---|
187 | map = (0, _utils.normalizeSourceMapAfterPostcss)(map, this.context);
|
---|
188 | }
|
---|
189 |
|
---|
190 | const ast = {
|
---|
191 | type: "postcss",
|
---|
192 | version: result.processor.version,
|
---|
193 | root: result.root
|
---|
194 | };
|
---|
195 | callback(null, result.css, map, {
|
---|
196 | ast
|
---|
197 | });
|
---|
198 | } |
---|