index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _url = _interopRequireDefault(require("url"));
  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. /**
  12. * The sass-loader makes node-sass and dart-sass available to webpack modules.
  13. *
  14. * @this {object}
  15. * @param {string} content
  16. */
  17. async function loader(content) {
  18. const options = this.getOptions(_options.default);
  19. const callback = this.async();
  20. let implementation;
  21. try {
  22. implementation = (0, _utils.getSassImplementation)(this, options.implementation);
  23. } catch (error) {
  24. callback(error);
  25. return;
  26. }
  27. const useSourceMap = typeof options.sourceMap === "boolean" ? options.sourceMap : this.sourceMap;
  28. const sassOptions = await (0, _utils.getSassOptions)(this, options, content, implementation, useSourceMap);
  29. const shouldUseWebpackImporter = typeof options.webpackImporter === "boolean" ? options.webpackImporter : true;
  30. if (shouldUseWebpackImporter) {
  31. const isModernAPI = options.api === "modern";
  32. if (!isModernAPI) {
  33. const {
  34. includePaths
  35. } = sassOptions;
  36. sassOptions.importer.push((0, _utils.getWebpackImporter)(this, implementation, includePaths));
  37. } else {
  38. sassOptions.importers.push((0, _utils.getModernWebpackImporter)(this, implementation));
  39. }
  40. }
  41. let compile;
  42. try {
  43. compile = (0, _utils.getCompileFn)(implementation, options);
  44. } catch (error) {
  45. callback(error);
  46. return;
  47. }
  48. let result;
  49. try {
  50. result = await compile(sassOptions, options);
  51. } catch (error) {
  52. // There are situations when the `file`/`span.url` property do not exist
  53. // Modern API
  54. if (error.span && typeof error.span.url !== "undefined") {
  55. this.addDependency(_url.default.fileURLToPath(error.span.url));
  56. }
  57. // Legacy API
  58. else if (typeof error.file !== "undefined") {
  59. // `node-sass` returns POSIX paths
  60. this.addDependency(_path.default.normalize(error.file));
  61. }
  62. callback((0, _utils.errorFactory)(error));
  63. return;
  64. }
  65. let map =
  66. // Modern API, then legacy API
  67. result.sourceMap ? result.sourceMap : result.map ? JSON.parse(result.map) : null;
  68. // Modify source paths only for webpack, otherwise we do nothing
  69. if (map && useSourceMap) {
  70. map = (0, _utils.normalizeSourceMap)(map, this.rootContext);
  71. }
  72. // Modern API
  73. if (typeof result.loadedUrls !== "undefined") {
  74. result.loadedUrls.filter(url => url.protocol === "file:").forEach(includedFile => {
  75. const normalizedIncludedFile = _url.default.fileURLToPath(includedFile);
  76. // Custom `importer` can return only `contents` so includedFile will be relative
  77. if (_path.default.isAbsolute(normalizedIncludedFile)) {
  78. this.addDependency(normalizedIncludedFile);
  79. }
  80. });
  81. }
  82. // Legacy API
  83. else if (typeof result.stats !== "undefined" && typeof result.stats.includedFiles !== "undefined") {
  84. result.stats.includedFiles.forEach(includedFile => {
  85. const normalizedIncludedFile = _path.default.normalize(includedFile);
  86. // Custom `importer` can return only `contents` so includedFile will be relative
  87. if (_path.default.isAbsolute(normalizedIncludedFile)) {
  88. this.addDependency(normalizedIncludedFile);
  89. }
  90. });
  91. }
  92. callback(null, result.css.toString(), map);
  93. }
  94. var _default = loader;
  95. exports.default = _default;