Projekt

Obecné

Profil

Stáhnout (3.2 KB) Statistiky
| Větev: | Revize:
1
/*
2
	MIT License http://www.opensource.org/licenses/mit-license.php
3
	Author Tobias Koppers @sokra
4
*/
5
"use strict";
6

    
7
const path = require("path");
8
const ParserHelpers = require("./ParserHelpers");
9

    
10
class CommonJsStuffPlugin {
11
	apply(compiler) {
12
		compiler.hooks.compilation.tap(
13
			"CommonJsStuffPlugin",
14
			(compilation, { normalModuleFactory }) => {
15
				const handler = (parser, parserOptions) => {
16
					parser.hooks.expression
17
						.for("require.main.require")
18
						.tap(
19
							"CommonJsStuffPlugin",
20
							ParserHelpers.expressionIsUnsupported(
21
								parser,
22
								"require.main.require is not supported by webpack."
23
							)
24
						);
25
					parser.hooks.expression
26
						.for("module.parent.require")
27
						.tap(
28
							"CommonJsStuffPlugin",
29
							ParserHelpers.expressionIsUnsupported(
30
								parser,
31
								"module.parent.require is not supported by webpack."
32
							)
33
						);
34
					parser.hooks.expression
35
						.for("require.main")
36
						.tap(
37
							"CommonJsStuffPlugin",
38
							ParserHelpers.toConstantDependencyWithWebpackRequire(
39
								parser,
40
								"__webpack_require__.c[__webpack_require__.s]"
41
							)
42
						);
43
					parser.hooks.expression
44
						.for("module.loaded")
45
						.tap("CommonJsStuffPlugin", expr => {
46
							parser.state.module.buildMeta.moduleConcatenationBailout =
47
								"module.loaded";
48
							return ParserHelpers.toConstantDependency(
49
								parser,
50
								"module.l"
51
							)(expr);
52
						});
53
					parser.hooks.expression
54
						.for("module.id")
55
						.tap("CommonJsStuffPlugin", expr => {
56
							parser.state.module.buildMeta.moduleConcatenationBailout =
57
								"module.id";
58
							return ParserHelpers.toConstantDependency(
59
								parser,
60
								"module.i"
61
							)(expr);
62
						});
63
					parser.hooks.expression
64
						.for("module.exports")
65
						.tap("CommonJsStuffPlugin", () => {
66
							const module = parser.state.module;
67
							const isHarmony =
68
								module.buildMeta && module.buildMeta.exportsType;
69
							if (!isHarmony) return true;
70
						});
71
					parser.hooks.evaluateIdentifier
72
						.for("module.hot")
73
						.tap(
74
							"CommonJsStuffPlugin",
75
							ParserHelpers.evaluateToIdentifier("module.hot", false)
76
						);
77
					parser.hooks.expression
78
						.for("module")
79
						.tap("CommonJsStuffPlugin", () => {
80
							const module = parser.state.module;
81
							const isHarmony =
82
								module.buildMeta && module.buildMeta.exportsType;
83
							let moduleJsPath = path.join(
84
								__dirname,
85
								"..",
86
								"buildin",
87
								isHarmony ? "harmony-module.js" : "module.js"
88
							);
89
							if (module.context) {
90
								moduleJsPath = path.relative(
91
									parser.state.module.context,
92
									moduleJsPath
93
								);
94
								if (!/^[A-Z]:/i.test(moduleJsPath)) {
95
									moduleJsPath = `./${moduleJsPath.replace(/\\/g, "/")}`;
96
								}
97
							}
98
							return ParserHelpers.addParsedVariableToModule(
99
								parser,
100
								"module",
101
								`require(${JSON.stringify(moduleJsPath)})(module)`
102
							);
103
						});
104
				};
105

    
106
				normalModuleFactory.hooks.parser
107
					.for("javascript/auto")
108
					.tap("CommonJsStuffPlugin", handler);
109
				normalModuleFactory.hooks.parser
110
					.for("javascript/dynamic")
111
					.tap("CommonJsStuffPlugin", handler);
112
			}
113
		);
114
	}
115
}
116
module.exports = CommonJsStuffPlugin;
(15-15/144)