index.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = loader;
  6. var _options = _interopRequireDefault(require("./options.json"));
  7. var _utils = require("./utils");
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. /*
  10. MIT License http://www.opensource.org/licenses/mit-license.php
  11. Author Tobias Koppers @sokra
  12. */
  13. function loader() {
  14. const options = this.getOptions(_options.default);
  15. const callback = this.async();
  16. let exposes;
  17. try {
  18. exposes = (0, _utils.getExposes)(options.exposes);
  19. } catch (error) {
  20. callback(error);
  21. return;
  22. }
  23. /*
  24. * Workaround until module.libIdent() in webpack/webpack handles this correctly.
  25. *
  26. * Fixes:
  27. * - https://github.com/webpack-contrib/expose-loader/issues/55
  28. * - https://github.com/webpack-contrib/expose-loader/issues/49
  29. */
  30. this._module.userRequest = (0, _utils.getNewUserRequest)(this._module.userRequest);
  31. /*
  32. * Adding side effects
  33. *
  34. * Fixes:
  35. * - https://github.com/webpack-contrib/expose-loader/issues/120
  36. */
  37. if (this._module.factoryMeta) {
  38. this._module.factoryMeta.sideEffectFree = false;
  39. }
  40. // Change the request from an /absolute/path.js to a relative ./path.js.
  41. // This prevents `[chunkhash]` values from changing when running webpack builds in different directories.
  42. const newRequest = (0, _utils.contextify)(this, this.context, this.remainingRequest);
  43. const stringifiedNewRequest = (0, _utils.stringifyRequest)(this, `-!${newRequest}`);
  44. let code = `var ___EXPOSE_LOADER_IMPORT___ = require(${stringifiedNewRequest});\n`;
  45. const getGlobalThis = options.globalObject || `require(${(0, _utils.stringifyRequest)(this, require.resolve("./runtime/getGlobalThis.js"))})`;
  46. code += `var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = ${getGlobalThis};\n`;
  47. code += `var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___;\n`;
  48. for (const expose of exposes) {
  49. const {
  50. globalName,
  51. moduleLocalName,
  52. override
  53. } = expose;
  54. const globalNameInterpolated = globalName.map(item => (0, _utils.interpolateName)(this, item, {}));
  55. if (typeof moduleLocalName !== "undefined") {
  56. code += `var ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___ = ___EXPOSE_LOADER_IMPORT___.${moduleLocalName}\n`;
  57. }
  58. let propertyString = "___EXPOSE_LOADER_GLOBAL_THIS___";
  59. for (let i = 0; i < globalName.length; i++) {
  60. if (i > 0) {
  61. code += `if (typeof ${propertyString} === 'undefined') ${propertyString} = {};\n`;
  62. }
  63. propertyString += `[${JSON.stringify(globalNameInterpolated[i])}]`;
  64. }
  65. if (!override) {
  66. code += `if (typeof ${propertyString} === 'undefined') `;
  67. }
  68. code += typeof moduleLocalName !== "undefined" ? `${propertyString} = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___;\n` : `${propertyString} = ___EXPOSE_LOADER_IMPORT___;\n`;
  69. if (!override) {
  70. if (this.mode === "development") {
  71. code += `else throw new Error('[exposes-loader] The "${globalName.join(".")}" value exists in the global scope, it may not be safe to overwrite it, use the "override" option')\n`;
  72. }
  73. }
  74. }
  75. code += `module.exports = ___EXPOSE_LOADER_IMPORT___;\n`;
  76. callback(null, code);
  77. }