1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
/* eslint-disable class-methods-use-this */
var less = require('less');
var loaderUtils = require('loader-utils');
var pify = require('pify');
var stringifyLoader = require.resolve('./stringifyLoader.js');
var trailingSlash = /[/\\]$/;
var isLessCompatible = /\.(le|c)ss$/;
// Less automatically adds a .less file extension if no extension was given.
// This is problematic if there is a module request like @import "~some-module";
// because in this case Less will call our file manager with `~some-module.less`.
// Since dots in module names are highly discouraged, we can safely assume that
// this is an error and we need to remove the .less extension again.
// However, we must not match something like @import "~some-module/file.less";
var matchMalformedModuleFilename = /(~[^/\\]+)\.less$/;
// This somewhat changed in Less 3.x. Now the file name comes without the
// automatically added extension whereas the extension is passed in as `options.ext`.
// So, if the file name matches this regexp, we simply ignore the proposed extension.
var isModuleName = /^~[^/\\]+$/;
/**
* Creates a Less plugin that uses webpack's resolving engine that is provided by the loaderContext.
*
* @param {LoaderContext} loaderContext
* @param {string=} root
* @returns {LessPlugin}
*/
function createWebpackLessPlugin(loaderContext) {
var fs = loaderContext.fs;
var resolve = pify(loaderContext.resolve.bind(loaderContext));
var loadModule = pify(loaderContext.loadModule.bind(loaderContext));
var readFile = pify(fs.readFile.bind(fs));
var WebpackFileManager = function (_less$FileManager) {
_inherits(WebpackFileManager, _less$FileManager);
function WebpackFileManager() {
_classCallCheck(this, WebpackFileManager);
return _possibleConstructorReturn(this, (WebpackFileManager.__proto__ || Object.getPrototypeOf(WebpackFileManager)).apply(this, arguments));
}
_createClass(WebpackFileManager, [{
key: 'supports',
value: function supports() {
// Our WebpackFileManager handles all the files
return true;
}
// Sync resolving is used at least by the `data-uri` function.
// This file manager doesn't know how to do it, so let's delegate it
// to the default file manager of Less.
// We could probably use loaderContext.resolveSync, but it's deprecated,
// see https://webpack.js.org/api/loaders/#this-resolvesync
}, {
key: 'supportsSync',
value: function supportsSync() {
return false;
}
}, {
key: 'loadFile',
value: function loadFile(filename, currentDirectory, options) {
var url = void 0;
if (less.version[0] >= 3) {
if (options.ext && !isModuleName.test(filename)) {
url = this.tryAppendExtension(filename, options.ext);
} else {
url = filename;
}
} else {
url = filename.replace(matchMalformedModuleFilename, '$1');
}
var moduleRequest = loaderUtils.urlToRequest(url, url.charAt(0) === '/' ? '' : null);
// Less is giving us trailing slashes, but the context should have no trailing slash
var context = currentDirectory.replace(trailingSlash, '');
var resolvedFilename = void 0;
return resolve(context, moduleRequest).then(function (f) {
resolvedFilename = f;
loaderContext.addDependency(resolvedFilename);
if (isLessCompatible.test(resolvedFilename)) {
return readFile(resolvedFilename).then(function (contents) {
return contents.toString('utf8');
});
}
return loadModule([stringifyLoader, resolvedFilename].join('!')).then(JSON.parse);
}).then(function (contents) {
return {
contents,
filename: resolvedFilename
};
});
}
}]);
return WebpackFileManager;
}(less.FileManager);
return {
install(lessInstance, pluginManager) {
pluginManager.addFileManager(new WebpackFileManager());
},
minVersion: [2, 1, 1]
};
}
module.exports = createWebpackLessPlugin;