index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. var path = require('path');
  6. function accesorString(value) {
  7. var childProperties = value.split(".");
  8. var length = childProperties.length;
  9. var propertyString = "global";
  10. var result = "";
  11. for(var i = 0; i < length; i++) {
  12. if(i > 0)
  13. result += "if(!" + propertyString + ") " + propertyString + " = {};\n";
  14. propertyString += "[" + JSON.stringify(childProperties[i]) + "]";
  15. }
  16. result += "module.exports = " + propertyString;
  17. return result;
  18. }
  19. module.exports = function() {};
  20. module.exports.pitch = function(remainingRequest) {
  21. // Change the request from an /abolute/path.js to a relative ./path.js
  22. // This prevents [chunkhash] values from changing when running webpack
  23. // builds in different directories.
  24. const newRequestPath = remainingRequest.replace(
  25. this.resourcePath,
  26. '.' + path.sep + path.relative(this.context, this.resourcePath)
  27. );
  28. this.cacheable && this.cacheable();
  29. if(!this.query) throw new Error("query parameter is missing");
  30. /*
  31. * Workaround until module.libIdent() in webpack/webpack handles this correctly.
  32. *
  33. * fixes:
  34. * - https://github.com/webpack-contrib/expose-loader/issues/55
  35. * - https://github.com/webpack-contrib/expose-loader/issues/49
  36. */
  37. this._module.userRequest = this._module.userRequest + '-exposed';
  38. return accesorString(this.query.substr(1)) + " = " +
  39. "require(" + JSON.stringify("-!" + newRequestPath) + ");";
  40. };