index.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. var loaderUtils = require("loader-utils");
  6. module.exports = function(content) {
  7. this.cacheable && this.cacheable();
  8. if(!this.emitFile) throw new Error("emitFile is required from module system");
  9. var query = loaderUtils.getOptions(this) || {};
  10. var configKey = query.config || "fileLoader";
  11. var options = this.options[configKey] || {};
  12. var config = {
  13. publicPath: false,
  14. name: "[hash].[ext]"
  15. };
  16. // options takes precedence over config
  17. Object.keys(options).forEach(function(attr) {
  18. config[attr] = options[attr];
  19. });
  20. // query takes precedence over config and options
  21. Object.keys(query).forEach(function(attr) {
  22. config[attr] = query[attr];
  23. });
  24. var url = loaderUtils.interpolateName(this, config.name, {
  25. context: config.context || this.options.context,
  26. content: content,
  27. regExp: config.regExp
  28. });
  29. var outputPath = url;
  30. var publicPath = "__webpack_public_path__ + " + JSON.stringify(url);
  31. if (config.outputPath) {
  32. // support functions as outputPath to generate them dynamically
  33. outputPath = typeof config.outputPath === "function"
  34. ? config.outputPath(url)
  35. : config.outputPath + url
  36. }
  37. if (config.publicPath) {
  38. // support functions as publicPath to generate them dynamically
  39. publicPath = JSON.stringify(
  40. typeof config.publicPath === "function"
  41. ? config.publicPath(url)
  42. : config.publicPath + url
  43. );
  44. }
  45. if (query.emitFile === undefined || query.emitFile) {
  46. this.emitFile(outputPath, content);
  47. }
  48. return "module.exports = " + publicPath + ";";
  49. }
  50. module.exports.raw = true;