1
|
/*
|
2
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
3
|
Author Tobias Koppers @sokra
|
4
|
*/
|
5
|
"use strict";
|
6
|
|
7
|
const ConstDependency = require("./dependencies/ConstDependency");
|
8
|
const ParserHelpers = require("./ParserHelpers");
|
9
|
|
10
|
const NullFactory = require("./NullFactory");
|
11
|
|
12
|
/* eslint-disable camelcase */
|
13
|
const REPLACEMENTS = {
|
14
|
__webpack_require__: "__webpack_require__",
|
15
|
__webpack_public_path__: "__webpack_require__.p",
|
16
|
__webpack_modules__: "__webpack_require__.m",
|
17
|
__webpack_chunk_load__: "__webpack_require__.e",
|
18
|
__non_webpack_require__: "require",
|
19
|
__webpack_nonce__: "__webpack_require__.nc",
|
20
|
"require.onError": "__webpack_require__.oe"
|
21
|
};
|
22
|
const NO_WEBPACK_REQUIRE = {
|
23
|
__non_webpack_require__: true
|
24
|
};
|
25
|
const REPLACEMENT_TYPES = {
|
26
|
__webpack_public_path__: "string",
|
27
|
__webpack_require__: "function",
|
28
|
__webpack_modules__: "object",
|
29
|
__webpack_chunk_load__: "function",
|
30
|
__webpack_nonce__: "string"
|
31
|
};
|
32
|
/* eslint-enable camelcase */
|
33
|
|
34
|
class APIPlugin {
|
35
|
apply(compiler) {
|
36
|
compiler.hooks.compilation.tap(
|
37
|
"APIPlugin",
|
38
|
(compilation, { normalModuleFactory }) => {
|
39
|
compilation.dependencyFactories.set(ConstDependency, new NullFactory());
|
40
|
compilation.dependencyTemplates.set(
|
41
|
ConstDependency,
|
42
|
new ConstDependency.Template()
|
43
|
);
|
44
|
|
45
|
const handler = parser => {
|
46
|
Object.keys(REPLACEMENTS).forEach(key => {
|
47
|
parser.hooks.expression
|
48
|
.for(key)
|
49
|
.tap(
|
50
|
"APIPlugin",
|
51
|
NO_WEBPACK_REQUIRE[key]
|
52
|
? ParserHelpers.toConstantDependency(
|
53
|
parser,
|
54
|
REPLACEMENTS[key]
|
55
|
)
|
56
|
: ParserHelpers.toConstantDependencyWithWebpackRequire(
|
57
|
parser,
|
58
|
REPLACEMENTS[key]
|
59
|
)
|
60
|
);
|
61
|
const type = REPLACEMENT_TYPES[key];
|
62
|
if (type) {
|
63
|
parser.hooks.evaluateTypeof
|
64
|
.for(key)
|
65
|
.tap("APIPlugin", ParserHelpers.evaluateToString(type));
|
66
|
}
|
67
|
});
|
68
|
};
|
69
|
|
70
|
normalModuleFactory.hooks.parser
|
71
|
.for("javascript/auto")
|
72
|
.tap("APIPlugin", handler);
|
73
|
normalModuleFactory.hooks.parser
|
74
|
.for("javascript/dynamic")
|
75
|
.tap("APIPlugin", handler);
|
76
|
normalModuleFactory.hooks.parser
|
77
|
.for("javascript/esm")
|
78
|
.tap("APIPlugin", handler);
|
79
|
}
|
80
|
);
|
81
|
}
|
82
|
}
|
83
|
|
84
|
module.exports = APIPlugin;
|