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.

42 lines
1.1 KiB

4 years ago
  1. 'use strict';
  2. var path = require('path');
  3. var loaderUtils = require('loader-utils'),
  4. camelcase = require('camelcase'),
  5. defaults = require('lodash.defaults');
  6. var PACKAGE_NAME = require('../package.json').name;
  7. var process = require('./process');
  8. /**
  9. * Webpack loader that manipulates the source-map of a preceding loader.
  10. * @this {object} The loader context
  11. * @param {string} content The content
  12. * @param {object} sourceMap The source-map
  13. * @returns {string|String}
  14. */
  15. function loader(content, sourceMap) {
  16. /* jshint validthis:true */
  17. // loader result is cacheable
  18. this.cacheable();
  19. // webpack 1: prefer loader query, else options object
  20. // webpack 2: prefer loader options
  21. // webpack 3: deprecate loader.options object
  22. // webpack 4: loader.options no longer defined
  23. var options = defaults(
  24. {sep: path.sep},
  25. loaderUtils.getOptions(this),
  26. this.options && this.options[camelcase(PACKAGE_NAME)]
  27. );
  28. // process the source-map
  29. var outputMap = process(this, options, sourceMap);
  30. // need to use callback when there are multiple arguments
  31. this.callback(null, content, outputMap);
  32. }
  33. module.exports = loader;