createWebpackLessPlugin.js 5.5 KB

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