UNPKG

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