-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathindex.js
107 lines (85 loc) · 3.06 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
import schema from "./options.json";
import {
getExposes,
contextify,
getNewUserRequest,
stringifyRequest,
interpolateName,
} from "./utils";
export default function loader() {
const options = this.getOptions(schema);
const callback = this.async();
let exposes;
try {
exposes = getExposes(options.exposes);
} catch (error) {
callback(error);
return;
}
/*
* Workaround until module.libIdent() in webpack/webpack handles this correctly.
*
* Fixes:
* - https://github.com/webpack-contrib/expose-loader/issues/55
* - https://github.com/webpack-contrib/expose-loader/issues/49
*/
this._module.userRequest = getNewUserRequest(this._module.userRequest);
/*
* Adding side effects
*
* Fixes:
* - https://github.com/webpack-contrib/expose-loader/issues/120
*/
if (this._module.factoryMeta) {
this._module.factoryMeta.sideEffectFree = false;
}
// Change the request from an /absolute/path.js to a relative ./path.js.
// This prevents `[chunkhash]` values from changing when running webpack builds in different directories.
const newRequest = contextify(this, this.context, this.remainingRequest);
const stringifiedNewRequest = stringifyRequest(this, `-!${newRequest}`);
let code = `var ___EXPOSE_LOADER_IMPORT___ = require(${stringifiedNewRequest});\n`;
const getGlobalThis =
options.globalObject ||
`require(${stringifyRequest(
this,
require.resolve("./runtime/getGlobalThis.js"),
)})`;
code += `var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = ${getGlobalThis};\n`;
code += `var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___;\n`;
for (const expose of exposes) {
const { globalName, moduleLocalName, override } = expose;
const globalNameInterpolated = globalName.map((item) =>
interpolateName(this, item, {}),
);
if (typeof moduleLocalName !== "undefined") {
code += `var ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___ = ___EXPOSE_LOADER_IMPORT___.${moduleLocalName}\n`;
}
let propertyString = "___EXPOSE_LOADER_GLOBAL_THIS___";
for (let i = 0; i < globalName.length; i++) {
if (i > 0) {
code += `if (typeof ${propertyString} === 'undefined') ${propertyString} = {};\n`;
}
propertyString += `[${JSON.stringify(globalNameInterpolated[i])}]`;
}
if (!override) {
code += `if (typeof ${propertyString} === 'undefined') `;
}
code +=
typeof moduleLocalName !== "undefined"
? `${propertyString} = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___;\n`
: `${propertyString} = ___EXPOSE_LOADER_IMPORT___;\n`;
if (!override) {
if (this.mode === "development") {
code += `else throw new Error('[expose-loader] The "${globalName.join(
".",
)}" value exists in the global scope, it may not be safe to overwrite it, use the "override" option')\n`;
}
}
}
code += `module.exports = ___EXPOSE_LOADER_IMPORT___;\n`;
callback(null, code);
}