loader.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. "use strict";
  2. const path = require("path");
  3. const async = require("neo-async");
  4. const formatSassError = require("./formatSassError");
  5. const webpackImporter = require("./webpackImporter");
  6. const normalizeOptions = require("./normalizeOptions");
  7. const pify = require("pify");
  8. const semver = require("semver");
  9. let nodeSassJobQueue = null;
  10. /**
  11. * The sass-loader makes node-sass available to webpack modules.
  12. *
  13. * @this {LoaderContext}
  14. * @param {string} content
  15. */
  16. function sassLoader(content) {
  17. const callback = this.async();
  18. const isSync = typeof callback !== "function";
  19. const self = this;
  20. const resourcePath = this.resourcePath;
  21. function addNormalizedDependency(file) {
  22. // node-sass returns POSIX paths
  23. self.dependency(path.normalize(file));
  24. }
  25. if (isSync) {
  26. throw new Error("Synchronous compilation is not supported anymore. See https://github.com/webpack-contrib/sass-loader/issues/333");
  27. }
  28. const options = normalizeOptions(this, content, webpackImporter(
  29. resourcePath,
  30. pify(this.resolve.bind(this)),
  31. addNormalizedDependency
  32. ));
  33. // Skip empty files, otherwise it will stop webpack, see issue #21
  34. if (options.data.trim() === "") {
  35. callback(null, "");
  36. return;
  37. }
  38. const render = getRenderFuncFromSassImpl(options.implementation || require("node-sass"));
  39. render(options, (err, result) => {
  40. if (err) {
  41. formatSassError(err, this.resourcePath);
  42. err.file && this.dependency(err.file);
  43. callback(err);
  44. return;
  45. }
  46. if (result.map && result.map !== "{}") {
  47. result.map = JSON.parse(result.map);
  48. // result.map.file is an optional property that provides the output filename.
  49. // Since we don't know the final filename in the webpack build chain yet, it makes no sense to have it.
  50. delete result.map.file;
  51. // The first source is 'stdin' according to node-sass because we've used the data input.
  52. // Now let's override that value with the correct relative path.
  53. // Since we specified options.sourceMap = path.join(process.cwd(), "/sass.map"); in normalizeOptions,
  54. // we know that this path is relative to process.cwd(). This is how node-sass works.
  55. result.map.sources[0] = path.relative(process.cwd(), resourcePath);
  56. // node-sass returns POSIX paths, that's why we need to transform them back to native paths.
  57. // This fixes an error on windows where the source-map module cannot resolve the source maps.
  58. // @see https://github.com/webpack-contrib/sass-loader/issues/366#issuecomment-279460722
  59. result.map.sourceRoot = path.normalize(result.map.sourceRoot);
  60. result.map.sources = result.map.sources.map(path.normalize);
  61. } else {
  62. result.map = null;
  63. }
  64. result.stats.includedFiles.forEach(addNormalizedDependency);
  65. callback(null, result.css.toString(), result.map);
  66. });
  67. }
  68. /**
  69. * Verifies that the implementation and version of Sass is supported by this loader.
  70. *
  71. * @param {Object} module
  72. * @returns {Function}
  73. */
  74. function getRenderFuncFromSassImpl(module) {
  75. const info = module.info;
  76. const components = info.split("\t");
  77. if (components.length < 2) {
  78. throw new Error("Unknown Sass implementation \"" + info + "\".");
  79. }
  80. const implementation = components[0];
  81. const version = components[1];
  82. if (!semver.valid(version)) {
  83. throw new Error("Invalid Sass version \"" + version + "\".");
  84. }
  85. if (implementation === "dart-sass") {
  86. if (!semver.satisfies(version, "^1.3.0")) {
  87. throw new Error("Dart Sass version " + version + " is incompatible with ^1.3.0.");
  88. }
  89. return module.render.bind(module);
  90. } else if (implementation === "node-sass") {
  91. if (!semver.satisfies(version, "^4.0.0")) {
  92. throw new Error("Node Sass version " + version + " is incompatible with ^4.0.0.");
  93. }
  94. // There is an issue with node-sass when async custom importers are used
  95. // See https://github.com/sass/node-sass/issues/857#issuecomment-93594360
  96. // We need to use a job queue to make sure that one thread is always available to the UV lib
  97. if (nodeSassJobQueue === null) {
  98. const threadPoolSize = Number(process.env.UV_THREADPOOL_SIZE || 4);
  99. nodeSassJobQueue = async.queue(module.render.bind(module), threadPoolSize - 1);
  100. }
  101. return nodeSassJobQueue.push.bind(nodeSassJobQueue);
  102. }
  103. throw new Error("Unknown Sass implementation \"" + implementation + "\".");
  104. }
  105. module.exports = sassLoader;