You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.8 KiB

4 years ago
  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. * Please note: If loaderContext.query is an options object, it will be re-used across multiple invocations.
  18. * That's why we must not modify the object directly.
  19. *
  20. * @param {LoaderContext} loaderContext
  21. * @param {string} loaderOptions
  22. * @param {object} content
  23. * @returns {Object}
  24. */
  25. function getSassOptions(loaderContext, loaderOptions, content) {
  26. const options = (0, _cloneDeep.default)(loaderOptions);
  27. const {
  28. resourcePath
  29. } = loaderContext; // allow opt.functions to be configured WRT loaderContext
  30. if (typeof options.functions === 'function') {
  31. options.functions = options.functions(loaderContext);
  32. }
  33. let {
  34. data
  35. } = options;
  36. if (typeof options.data === 'function') {
  37. data = options.data(loaderContext);
  38. }
  39. options.data = data ? data + _os.default.EOL + content : content; // opt.outputStyle
  40. if (!options.outputStyle && isProductionLikeMode(loaderContext)) {
  41. options.outputStyle = 'compressed';
  42. } // opt.sourceMap
  43. // Not using the `this.sourceMap` flag because css source maps are different
  44. // @see https://github.com/webpack/css-loader/pull/40
  45. if (options.sourceMap) {
  46. // Deliberately overriding the sourceMap option here.
  47. // node-sass won't produce source maps if the data option is used and options.sourceMap is not a string.
  48. // In case it is a string, options.sourceMap should be a path where the source map is written.
  49. // But since we're using the data option, the source map will not actually be written, but
  50. // all paths in sourceMap.sources will be relative to that path.
  51. // Pretty complicated... :(
  52. options.sourceMap = _path.default.join(process.cwd(), '/sass.map');
  53. if ('sourceMapRoot' in options === false) {
  54. options.sourceMapRoot = process.cwd();
  55. }
  56. if ('omitSourceMapUrl' in options === false) {
  57. // The source map url doesn't make sense because we don't know the output path
  58. // The css-loader will handle that for us
  59. options.omitSourceMapUrl = true;
  60. }
  61. if ('sourceMapContents' in options === false) {
  62. // If sourceMapContents option is not set, set it to true otherwise maps will be empty/null
  63. // when exported by webpack-extract-text-plugin.
  64. options.sourceMapContents = true;
  65. }
  66. } // indentedSyntax is a boolean flag.
  67. const ext = _path.default.extname(resourcePath); // If we are compiling sass and indentedSyntax isn't set, automatically set it.
  68. if (ext && ext.toLowerCase() === '.sass' && 'indentedSyntax' in options === false) {
  69. options.indentedSyntax = true;
  70. } else {
  71. options.indentedSyntax = Boolean(options.indentedSyntax);
  72. } // Allow passing custom importers to `node-sass`. Accepts `Function` or an array of `Function`s.
  73. options.importer = options.importer ? (0, _proxyCustomImporters.default)(options.importer, resourcePath) : []; // `node-sass` uses `includePaths` to resolve `@import` paths. Append the currently processed file.
  74. options.includePaths = options.includePaths || [];
  75. options.includePaths.push(_path.default.dirname(resourcePath));
  76. return options;
  77. }
  78. var _default = getSassOptions;
  79. exports.default = _default;