index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 = _interopRequireDefault(require("schema-utils"));
  8. var _loaderUtils = require("loader-utils");
  9. var _options = _interopRequireDefault(require("./options.json"));
  10. var _getSassImplementation = _interopRequireDefault(require("./getSassImplementation"));
  11. var _getSassOptions = _interopRequireDefault(require("./getSassOptions"));
  12. var _webpackImporter = _interopRequireDefault(require("./webpackImporter"));
  13. var _getRenderFunctionFromSassImplementation = _interopRequireDefault(require("./getRenderFunctionFromSassImplementation"));
  14. var _SassError = _interopRequireDefault(require("./SassError"));
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. /**
  17. * The sass-loader makes node-sass and dart-sass available to webpack modules.
  18. *
  19. * @this {object}
  20. * @param {string} content
  21. */
  22. function loader(content) {
  23. const options = (0, _loaderUtils.getOptions)(this) || {};
  24. (0, _schemaUtils.default)(_options.default, options, {
  25. name: 'Sass Loader',
  26. baseDataPath: 'options'
  27. });
  28. const implementation = (0, _getSassImplementation.default)(options.implementation);
  29. const callback = this.async();
  30. const addNormalizedDependency = file => {
  31. // node-sass returns POSIX paths
  32. this.addDependency(_path.default.normalize(file));
  33. };
  34. const sassOptions = (0, _getSassOptions.default)(this, options, content, implementation);
  35. const shouldUseWebpackImporter = typeof options.webpackImporter === 'boolean' ? options.webpackImporter : true;
  36. if (shouldUseWebpackImporter) {
  37. const resolve = this.getResolve({
  38. mainFields: ['sass', 'style', 'main', '...'],
  39. mainFiles: ['_index', 'index', '...'],
  40. extensions: ['.scss', '.sass', '.css', '...']
  41. });
  42. sassOptions.importer.push((0, _webpackImporter.default)(this.resourcePath, resolve, addNormalizedDependency));
  43. } // Skip empty files, otherwise it will stop webpack, see issue #21
  44. if (sassOptions.data.trim() === '') {
  45. callback(null, '');
  46. return;
  47. }
  48. const render = (0, _getRenderFunctionFromSassImplementation.default)(implementation);
  49. render(sassOptions, (error, result) => {
  50. if (error) {
  51. if (error.file) {
  52. addNormalizedDependency(error.file);
  53. }
  54. callback(new _SassError.default(error, this.resourcePath));
  55. return;
  56. }
  57. if (result.map && result.map !== '{}') {
  58. // eslint-disable-next-line no-param-reassign
  59. result.map = JSON.parse(result.map); // result.map.file is an optional property that provides the output filename.
  60. // Since we don't know the final filename in the webpack build chain yet, it makes no sense to have it.
  61. // eslint-disable-next-line no-param-reassign
  62. delete result.map.file; // One of the sources is 'stdin' according to dart-sass/node-sass because we've used the data input.
  63. // Now let's override that value with the correct relative path.
  64. // Since we specified options.sourceMap = path.join(process.cwd(), "/sass.map"); in getSassOptions,
  65. // we know that this path is relative to process.cwd(). This is how node-sass works.
  66. // eslint-disable-next-line no-param-reassign
  67. const stdinIndex = result.map.sources.findIndex(source => source.includes('stdin'));
  68. if (stdinIndex !== -1) {
  69. // eslint-disable-next-line no-param-reassign
  70. result.map.sources[stdinIndex] = _path.default.relative(process.cwd(), this.resourcePath);
  71. } // node-sass returns POSIX paths, that's why we need to transform them back to native paths.
  72. // This fixes an error on windows where the source-map module cannot resolve the source maps.
  73. // @see https://github.com/webpack-contrib/sass-loader/issues/366#issuecomment-279460722
  74. // eslint-disable-next-line no-param-reassign
  75. result.map.sourceRoot = _path.default.normalize(result.map.sourceRoot); // eslint-disable-next-line no-param-reassign
  76. result.map.sources = result.map.sources.map(_path.default.normalize);
  77. } else {
  78. // eslint-disable-next-line no-param-reassign
  79. result.map = null;
  80. }
  81. result.stats.includedFiles.forEach(addNormalizedDependency);
  82. callback(null, result.css.toString(), result.map);
  83. });
  84. }
  85. var _default = loader;
  86. exports.default = _default;