1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 | var _path = _interopRequireDefault(require("path"));
|
8 | var _options = _interopRequireDefault(require("./options.json"));
|
9 | var _utils = require("./utils");
|
10 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
11 | async function lessLoader(source) {
|
12 | const options = this.getOptions(_options.default);
|
13 | const callback = this.async();
|
14 | let implementation;
|
15 | try {
|
16 | implementation = (0, _utils.getLessImplementation)(this, options.implementation);
|
17 | } catch (error) {
|
18 | callback(error);
|
19 | return;
|
20 | }
|
21 | if (!implementation) {
|
22 | callback(new Error(`The Less implementation "${options.implementation}" not found`));
|
23 | return;
|
24 | }
|
25 | const lessOptions = (0, _utils.getLessOptions)(this, options, implementation);
|
26 | const useSourceMap = typeof options.sourceMap === "boolean" ? options.sourceMap : this.sourceMap;
|
27 | if (useSourceMap) {
|
28 | lessOptions.sourceMap = {
|
29 | outputSourceFiles: true
|
30 | };
|
31 | }
|
32 | let data = source;
|
33 | if (typeof options.additionalData !== "undefined") {
|
34 | data = typeof options.additionalData === "function" ? `${await options.additionalData(data, this)}` : `${options.additionalData}\n${data}`;
|
35 | }
|
36 | const logger = this.getLogger("less-loader");
|
37 | const loaderContext = this;
|
38 | const loggerListener = {
|
39 | error(message) {
|
40 |
|
41 | if (options.lessLogAsWarnOrErr) {
|
42 | loaderContext.emitError(new Error(message));
|
43 | } else {
|
44 | logger.error(message);
|
45 | }
|
46 | },
|
47 | warn(message) {
|
48 |
|
49 | if (options.lessLogAsWarnOrErr) {
|
50 | loaderContext.emitWarning(new Error(message));
|
51 | } else {
|
52 | logger.warn(message);
|
53 | }
|
54 | },
|
55 | info(message) {
|
56 | logger.log(message);
|
57 | },
|
58 | debug(message) {
|
59 | logger.debug(message);
|
60 | }
|
61 | };
|
62 | implementation.logger.addListener(loggerListener);
|
63 | let result;
|
64 | try {
|
65 | result = await implementation.render(data, lessOptions);
|
66 | } catch (error) {
|
67 | if (error.filename) {
|
68 |
|
69 |
|
70 | this.addDependency(_path.default.normalize(error.filename));
|
71 | }
|
72 | callback((0, _utils.errorFactory)(error));
|
73 | return;
|
74 | } finally {
|
75 |
|
76 | implementation.logger.removeListener(loggerListener);
|
77 | delete lessOptions.pluginManager.webpackLoaderContext;
|
78 | delete lessOptions.pluginManager;
|
79 | }
|
80 | const {
|
81 | css,
|
82 | imports
|
83 | } = result;
|
84 | imports.forEach(item => {
|
85 | if ((0, _utils.isUnsupportedUrl)(item)) {
|
86 | return;
|
87 | }
|
88 |
|
89 |
|
90 |
|
91 | const normalizedItem = _path.default.normalize(item);
|
92 |
|
93 |
|
94 | if (_path.default.isAbsolute(normalizedItem)) {
|
95 | this.addDependency(normalizedItem);
|
96 | }
|
97 | });
|
98 | let map = typeof result.map === "string" ? JSON.parse(result.map) : result.map;
|
99 | if (map && useSourceMap) {
|
100 | map = (0, _utils.normalizeSourceMap)(map, this.rootContext);
|
101 | }
|
102 | callback(null, css, map);
|
103 | }
|
104 | var _default = exports.default = lessLoader; |
\ | No newline at end of file |