getSassOptions.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _os = _interopRequireDefault(require("os"));
  7. var _path = _interopRequireDefault(require("path"));
  8. var _cloneDeep = _interopRequireDefault(require("clone-deep"));
  9. var _proxyCustomImporters = _interopRequireDefault(require("./proxyCustomImporters"));
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. function isProductionLikeMode(loaderContext) {
  12. return loaderContext.mode === 'production' || !loaderContext.mode || loaderContext.minimize;
  13. }
  14. /**
  15. * Derives the sass options from the loader context and normalizes its values with sane defaults.
  16. *
  17. * @param {object} loaderContext
  18. * @param {object} loaderOptions
  19. * @param {string} content
  20. * @param {object} implementation
  21. * @returns {Object}
  22. */
  23. function getSassOptions(loaderContext, loaderOptions, content, implementation) {
  24. const options = (0, _cloneDeep.default)(loaderOptions.sassOptions ? typeof loaderOptions.sassOptions === 'function' ? loaderOptions.sassOptions(loaderContext) || {} : loaderOptions.sassOptions : {});
  25. const isDartSass = implementation.info.includes('dart-sass');
  26. if (isDartSass) {
  27. const shouldTryToResolveFibers = !options.fiber && options.fiber !== false;
  28. if (shouldTryToResolveFibers) {
  29. let fibers;
  30. try {
  31. fibers = require.resolve('fibers');
  32. } catch (_error) {// Nothing
  33. }
  34. if (fibers) {
  35. // eslint-disable-next-line global-require, import/no-dynamic-require
  36. options.fiber = require(fibers);
  37. }
  38. } else if (options.fiber === false) {
  39. // Don't pass the `fiber` option for `sass` (`Dart Sass`)
  40. delete options.fiber;
  41. }
  42. } else {
  43. // Don't pass the `fiber` option for `node-sass`
  44. delete options.fiber;
  45. }
  46. options.data = loaderOptions.prependData ? typeof loaderOptions.prependData === 'function' ? loaderOptions.prependData(loaderContext) + _os.default.EOL + content : loaderOptions.prependData + _os.default.EOL + content : content; // opt.outputStyle
  47. if (!options.outputStyle && isProductionLikeMode(loaderContext)) {
  48. options.outputStyle = 'compressed';
  49. }
  50. const useSourceMap = typeof loaderOptions.sourceMap === 'boolean' ? loaderOptions.sourceMap : loaderContext.sourceMap; // opt.sourceMap
  51. // Not using the `this.sourceMap` flag because css source maps are different
  52. // @see https://github.com/webpack/css-loader/pull/40
  53. if (useSourceMap) {
  54. // Deliberately overriding the sourceMap option here.
  55. // node-sass won't produce source maps if the data option is used and options.sourceMap is not a string.
  56. // In case it is a string, options.sourceMap should be a path where the source map is written.
  57. // But since we're using the data option, the source map will not actually be written, but
  58. // all paths in sourceMap.sources will be relative to that path.
  59. // Pretty complicated... :(
  60. options.sourceMap = _path.default.join(process.cwd(), '/sass.map');
  61. if ('sourceMapRoot' in options === false) {
  62. options.sourceMapRoot = process.cwd();
  63. }
  64. if ('omitSourceMapUrl' in options === false) {
  65. // The source map url doesn't make sense because we don't know the output path
  66. // The css-loader will handle that for us
  67. options.omitSourceMapUrl = true;
  68. }
  69. if ('sourceMapContents' in options === false) {
  70. // If sourceMapContents option is not set, set it to true otherwise maps will be empty/null
  71. // when exported by webpack-extract-text-plugin.
  72. options.sourceMapContents = true;
  73. }
  74. }
  75. const {
  76. resourcePath
  77. } = loaderContext;
  78. const ext = _path.default.extname(resourcePath); // If we are compiling sass and indentedSyntax isn't set, automatically set it.
  79. if (ext && ext.toLowerCase() === '.sass' && 'indentedSyntax' in options === false) {
  80. options.indentedSyntax = true;
  81. } else {
  82. options.indentedSyntax = Boolean(options.indentedSyntax);
  83. } // Allow passing custom importers to `node-sass`. Accepts `Function` or an array of `Function`s.
  84. options.importer = options.importer ? (0, _proxyCustomImporters.default)(options.importer, resourcePath) : [];
  85. options.includePaths = (options.includePaths || []).concat(_path.default.dirname(resourcePath));
  86. return options;
  87. }
  88. var _default = getSassOptions;
  89. exports.default = _default;