index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _path = _interopRequireDefault(require("path"));
  7. var _schemaUtils = require("schema-utils");
  8. var _loaderUtils = require("loader-utils");
  9. var _options = _interopRequireDefault(require("./options.json"));
  10. var _utils = require("./utils");
  11. var _SassError = _interopRequireDefault(require("./SassError"));
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. /**
  14. * The sass-loader makes node-sass and dart-sass available to webpack modules.
  15. *
  16. * @this {object}
  17. * @param {string} content
  18. */
  19. async function loader(content) {
  20. const options = (0, _loaderUtils.getOptions)(this);
  21. (0, _schemaUtils.validate)(_options.default, options, {
  22. name: "Sass Loader",
  23. baseDataPath: "options"
  24. });
  25. const callback = this.async();
  26. const implementation = (0, _utils.getSassImplementation)(this, options.implementation);
  27. if (!implementation) {
  28. callback();
  29. return;
  30. }
  31. const useSourceMap = typeof options.sourceMap === "boolean" ? options.sourceMap : this.sourceMap;
  32. const sassOptions = await (0, _utils.getSassOptions)(this, options, content, implementation, useSourceMap);
  33. const shouldUseWebpackImporter = typeof options.webpackImporter === "boolean" ? options.webpackImporter : true;
  34. if (shouldUseWebpackImporter) {
  35. const {
  36. includePaths
  37. } = sassOptions;
  38. sassOptions.importer.push((0, _utils.getWebpackImporter)(this, implementation, includePaths));
  39. }
  40. const render = (0, _utils.getRenderFunctionFromSassImplementation)(implementation);
  41. render(sassOptions, (error, result) => {
  42. if (error) {
  43. // There are situations when the `file` property do not exist
  44. if (error.file) {
  45. // `node-sass` returns POSIX paths
  46. this.addDependency(_path.default.normalize(error.file));
  47. }
  48. callback(new _SassError.default(error));
  49. return;
  50. }
  51. let map = result.map ? JSON.parse(result.map) : null;
  52. // Modify source paths only for webpack, otherwise we do nothing
  53. if (map && useSourceMap) {
  54. map = (0, _utils.normalizeSourceMap)(map, this.rootContext);
  55. }
  56. result.stats.includedFiles.forEach(includedFile => {
  57. const normalizedIncludedFile = _path.default.normalize(includedFile);
  58. // Custom `importer` can return only `contents` so includedFile will be relative
  59. if (_path.default.isAbsolute(normalizedIncludedFile)) {
  60. this.addDependency(normalizedIncludedFile);
  61. }
  62. });
  63. callback(null, result.css.toString(), map);
  64. });
  65. }
  66. var _default = exports.default = loader;