1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = loader;
|
7 |
|
8 | var _options = _interopRequireDefault(require("./options.json"));
|
9 |
|
10 | var _utils = require("./utils");
|
11 |
|
12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | function 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 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | this._module.userRequest = (0, _utils.getNewUserRequest)(this._module.userRequest);
|
39 | |
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | if (this._module.factoryMeta) {
|
47 | this._module.factoryMeta.sideEffectFree = false;
|
48 | }
|
49 |
|
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 |