loader.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. // This queue makes sure node-sass leaves one thread available for executing
  9. // fs tasks when running the custom importer code.
  10. // This can be removed as soon as node-sass implements a fix for this.
  11. const threadPoolSize = process.env.UV_THREADPOOL_SIZE || 4;
  12. let asyncSassJobQueue = null;
  13. /**
  14. * The sass-loader makes node-sass available to webpack modules.
  15. *
  16. * @this {LoaderContext}
  17. * @param {string} content
  18. */
  19. function sassLoader(content) {
  20. if (asyncSassJobQueue === null) {
  21. const sass = require("node-sass");
  22. const sassVersion = /^(\d+)/.exec(require("node-sass/package.json").version).pop();
  23. if (Number(sassVersion) < 4) {
  24. throw new Error(
  25. "The installed version of `node-sass` is not compatible (expected: >= 4, actual: " + sassVersion + ")."
  26. );
  27. }
  28. asyncSassJobQueue = async.queue(sass.render, threadPoolSize - 1);
  29. }
  30. const callback = this.async();
  31. const isSync = typeof callback !== "function";
  32. const self = this;
  33. const resourcePath = this.resourcePath;
  34. function addNormalizedDependency(file) {
  35. // node-sass returns POSIX paths
  36. self.dependency(path.normalize(file));
  37. }
  38. if (isSync) {
  39. throw new Error("Synchronous compilation is not supported anymore. See https://github.com/webpack-contrib/sass-loader/issues/333");
  40. }
  41. const options = normalizeOptions(this, content, webpackImporter(
  42. resourcePath,
  43. pify(this.resolve.bind(this)),
  44. addNormalizedDependency
  45. ));
  46. // Skip empty files, otherwise it will stop webpack, see issue #21
  47. if (options.data.trim() === "") {
  48. callback(null, "");
  49. return;
  50. }
  51. // start the actual rendering
  52. asyncSassJobQueue.push(options, (err, result) => {
  53. if (err) {
  54. formatSassError(err, this.resourcePath);
  55. err.file && this.dependency(err.file);
  56. callback(err);
  57. return;
  58. }
  59. if (result.map && result.map !== "{}") {
  60. result.map = JSON.parse(result.map);
  61. // result.map.file is an optional property that provides the output filename.
  62. // Since we don't know the final filename in the webpack build chain yet, it makes no sense to have it.
  63. delete result.map.file;
  64. // The first source is 'stdin' according to node-sass because we've used the data input.
  65. // Now let's override that value with the correct relative path.
  66. // Since we specified options.sourceMap = path.join(process.cwd(), "/sass.map"); in normalizeOptions,
  67. // we know that this path is relative to process.cwd(). This is how node-sass works.
  68. result.map.sources[0] = path.relative(process.cwd(), resourcePath);
  69. // node-sass returns POSIX paths, that's why we need to transform them back to native paths.
  70. // This fixes an error on windows where the source-map module cannot resolve the source maps.
  71. // @see https://github.com/webpack-contrib/sass-loader/issues/366#issuecomment-279460722
  72. result.map.sourceRoot = path.normalize(result.map.sourceRoot);
  73. result.map.sources = result.map.sources.map(path.normalize);
  74. } else {
  75. result.map = null;
  76. }
  77. result.stats.includedFiles.forEach(addNormalizedDependency);
  78. callback(null, result.css.toString(), result.map);
  79. });
  80. }
  81. module.exports = sassLoader;