getOptions.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. 'use strict';
  2. var loaderUtils = require('loader-utils');
  3. var clone = require('clone');
  4. var createWebpackLessPlugin = require('./createWebpackLessPlugin');
  5. /**
  6. * Retrieves the options from the loaderContext, makes a deep copy of it and normalizes it for further consumption.
  7. *
  8. * @param {LoaderContext} loaderContext
  9. */
  10. function getOptions(loaderContext) {
  11. var options = Object.assign({
  12. plugins: [],
  13. relativeUrls: true,
  14. compress: Boolean(loaderContext.minimize)
  15. }, clone(loaderUtils.getOptions(loaderContext)));
  16. // We need to set the filename because otherwise our WebpackFileManager will receive an undefined path for the entry
  17. options.filename = loaderContext.resource;
  18. // When no paths are given, we use the webpack resolver
  19. if ('paths' in options === false) {
  20. // It's safe to mutate the array now because it has already been cloned
  21. options.plugins.push(createWebpackLessPlugin(loaderContext));
  22. }
  23. if (options.sourceMap) {
  24. if (typeof options.sourceMap === 'boolean') {
  25. options.sourceMap = {};
  26. }
  27. if ('outputSourceFiles' in options.sourceMap === false) {
  28. // Include source files as `sourceContents` as sane default since this makes source maps "just work" in most cases
  29. options.sourceMap.outputSourceFiles = true;
  30. }
  31. }
  32. return options;
  33. }
  34. module.exports = getOptions;