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 loggerListener = {
|
38 | error(message) {
|
39 | logger.error(message);
|
40 | },
|
41 | warn(message) {
|
42 | logger.warn(message);
|
43 | },
|
44 | info(message) {
|
45 | logger.log(message);
|
46 | },
|
47 | debug(message) {
|
48 | logger.debug(message);
|
49 | }
|
50 | };
|
51 | implementation.logger.addListener(loggerListener);
|
52 | let result;
|
53 | try {
|
54 | result = await implementation.render(data, lessOptions);
|
55 | } catch (error) {
|
56 | if (error.filename) {
|
57 |
|
58 |
|
59 | this.addDependency(_path.default.normalize(error.filename));
|
60 | }
|
61 | callback((0, _utils.errorFactory)(error));
|
62 | return;
|
63 | } finally {
|
64 |
|
65 | implementation.logger.removeListener(loggerListener);
|
66 | delete lessOptions.pluginManager.webpackLoaderContext;
|
67 | delete lessOptions.pluginManager;
|
68 | }
|
69 | const {
|
70 | css,
|
71 | imports
|
72 | } = result;
|
73 | imports.forEach(item => {
|
74 | if ((0, _utils.isUnsupportedUrl)(item)) {
|
75 | return;
|
76 | }
|
77 |
|
78 |
|
79 |
|
80 | const normalizedItem = _path.default.normalize(item);
|
81 |
|
82 |
|
83 | if (_path.default.isAbsolute(normalizedItem)) {
|
84 | this.addDependency(normalizedItem);
|
85 | }
|
86 | });
|
87 | let map = typeof result.map === "string" ? JSON.parse(result.map) : result.map;
|
88 | if (map && useSourceMap) {
|
89 | map = (0, _utils.normalizeSourceMap)(map, this.rootContext);
|
90 | }
|
91 | callback(null, css, map);
|
92 | }
|
93 | var _default = lessLoader;
|
94 | exports.default = _default; |
\ | No newline at end of file |