[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = void 0;
|
---|
| 7 |
|
---|
| 8 | var _path = _interopRequireDefault(require("path"));
|
---|
| 9 |
|
---|
| 10 | var _options = _interopRequireDefault(require("./options.json"));
|
---|
| 11 |
|
---|
| 12 | var _utils = require("./utils");
|
---|
| 13 |
|
---|
| 14 | var _LessError = _interopRequireDefault(require("./LessError"));
|
---|
| 15 |
|
---|
| 16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
| 17 |
|
---|
| 18 | async function lessLoader(source) {
|
---|
| 19 | const options = this.getOptions(_options.default);
|
---|
| 20 | const callback = this.async();
|
---|
| 21 | const implementation = (0, _utils.getLessImplementation)(this, options.implementation);
|
---|
| 22 |
|
---|
| 23 | if (!implementation) {
|
---|
| 24 | callback(new Error(`The Less implementation "${options.implementation}" not found`));
|
---|
| 25 | return;
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | const lessOptions = (0, _utils.getLessOptions)(this, options, implementation);
|
---|
| 29 | const useSourceMap = typeof options.sourceMap === "boolean" ? options.sourceMap : this.sourceMap;
|
---|
| 30 |
|
---|
| 31 | if (useSourceMap) {
|
---|
| 32 | lessOptions.sourceMap = {
|
---|
| 33 | outputSourceFiles: true
|
---|
| 34 | };
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | let data = source;
|
---|
| 38 |
|
---|
| 39 | if (typeof options.additionalData !== "undefined") {
|
---|
| 40 | data = typeof options.additionalData === "function" ? `${await options.additionalData(data, this)}` : `${options.additionalData}\n${data}`;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | let result;
|
---|
| 44 |
|
---|
| 45 | try {
|
---|
| 46 | result = await implementation.render(data, lessOptions);
|
---|
| 47 | } catch (error) {
|
---|
| 48 | if (error.filename) {
|
---|
| 49 | // `less` returns forward slashes on windows when `webpack` resolver return an absolute windows path in `WebpackFileManager`
|
---|
| 50 | // Ref: https://github.com/webpack-contrib/less-loader/issues/357
|
---|
| 51 | this.addDependency(_path.default.normalize(error.filename));
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | callback(new _LessError.default(error));
|
---|
| 55 | return;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | delete lessOptions.pluginManager.webpackLoaderContext;
|
---|
| 59 | delete lessOptions.pluginManager;
|
---|
| 60 | const {
|
---|
| 61 | css,
|
---|
| 62 | imports
|
---|
| 63 | } = result;
|
---|
| 64 | imports.forEach(item => {
|
---|
| 65 | if ((0, _utils.isUnsupportedUrl)(item)) {
|
---|
| 66 | return;
|
---|
| 67 | } // `less` return forward slashes on windows when `webpack` resolver return an absolute windows path in `WebpackFileManager`
|
---|
| 68 | // Ref: https://github.com/webpack-contrib/less-loader/issues/357
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 | this.addDependency(_path.default.normalize(item));
|
---|
| 72 | });
|
---|
| 73 | let map = typeof result.map === "string" ? JSON.parse(result.map) : result.map;
|
---|
| 74 |
|
---|
| 75 | if (map && useSourceMap) {
|
---|
| 76 | map = (0, _utils.normalizeSourceMap)(map, this.rootContext);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | callback(null, css, map);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | var _default = lessLoader;
|
---|
| 83 | exports.default = _default; |
---|