index.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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; // Modify source paths only for webpack, otherwise we do nothing
  52. if (map && useSourceMap) {
  53. map = (0, _utils.normalizeSourceMap)(map, this.rootContext);
  54. }
  55. result.stats.includedFiles.forEach(includedFile => {
  56. const normalizedIncludedFile = _path.default.normalize(includedFile); // Custom `importer` can return only `contents` so includedFile will be relative
  57. if (_path.default.isAbsolute(normalizedIncludedFile)) {
  58. this.addDependency(normalizedIncludedFile);
  59. }
  60. });
  61. callback(null, result.css.toString(), map);
  62. });
  63. }
  64. var _default = loader;
  65. exports.default = _default;