Skip to content

Commit db1475c

Browse files
authored
Merge pull request webpack#7656 from mohsen1/DefinePlugin-type
Add DefinePlugin JSDoc types
2 parents 2702273 + 84d57e3 commit db1475c

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

lib/DefinePlugin.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
99
const ParserHelpers = require("./ParserHelpers");
1010
const NullFactory = require("./NullFactory");
1111

12+
/** @typedef {import("./Compiler")} Compiler */
13+
/** @typedef {import("./Parser")} Parser */
14+
/** @typedef {null|undefined|RegExp|Function|string|number} CodeValuePrimitive */
15+
/** @typedef {CodeValuePrimitive|Record<string, CodeValuePrimitive>|RuntimeValue} CodeValue */
16+
1217
class RuntimeValue {
1318
constructor(fn, fileDependencies) {
1419
this.fn = fn;
@@ -37,6 +42,12 @@ const stringifyObj = (obj, parser) => {
3742
);
3843
};
3944

45+
/**
46+
* Convert code to a string that evaluates
47+
* @param {CodeValue} code Code to evaluate
48+
* @param {Parser} parser Parser
49+
* @returns {string} code converted to string that evaluates
50+
*/
4051
const toCode = (code, parser) => {
4152
if (code === null) {
4253
return "null";
@@ -60,6 +71,10 @@ const toCode = (code, parser) => {
6071
};
6172

6273
class DefinePlugin {
74+
/**
75+
* Create a new define plugin
76+
* @param {Record<string, CodeValue>} definitions A map of global object definitions
77+
*/
6378
constructor(definitions) {
6479
this.definitions = definitions;
6580
}
@@ -68,6 +83,11 @@ class DefinePlugin {
6883
return new RuntimeValue(fn, fileDependencies);
6984
}
7085

86+
/**
87+
* Apply the plugin
88+
* @param {Compiler} compiler Webpack compiler
89+
* @returns {void}
90+
*/
7191
apply(compiler) {
7292
const definitions = this.definitions;
7393
compiler.hooks.compilation.tap(
@@ -79,7 +99,18 @@ class DefinePlugin {
7999
new ConstDependency.Template()
80100
);
81101

102+
/**
103+
* Handler
104+
* @param {Parser} parser Parser
105+
* @returns {void}
106+
*/
82107
const handler = parser => {
108+
/**
109+
* Walk definitions
110+
* @param {Object} definitions Definitions map
111+
* @param {string} prefix Prefix string
112+
* @returns {void}
113+
*/
83114
const walkDefinitions = (definitions, prefix) => {
84115
Object.keys(definitions).forEach(key => {
85116
const code = definitions[key];
@@ -98,6 +129,12 @@ class DefinePlugin {
98129
});
99130
};
100131

132+
/**
133+
* Apply define key
134+
* @param {string} prefix Prefix
135+
* @param {string} key Key
136+
* @returns {void}
137+
*/
101138
const applyDefineKey = (prefix, key) => {
102139
const splittedKey = key.split(".");
103140
splittedKey.slice(1).forEach((_, i) => {
@@ -108,6 +145,12 @@ class DefinePlugin {
108145
});
109146
};
110147

148+
/**
149+
* Apply Code
150+
* @param {string} key Key
151+
* @param {CodeValue} code Code
152+
* @returns {void}
153+
*/
111154
const applyDefine = (key, code) => {
112155
const isTypeof = /^typeof\s+/.test(key);
113156
if (isTypeof) key = key.replace(/^typeof\s+/, "");
@@ -181,6 +224,12 @@ class DefinePlugin {
181224
});
182225
};
183226

227+
/**
228+
* Apply Object
229+
* @param {string} key Key
230+
* @param {Object} obj Object
231+
* @returns {void}
232+
*/
184233
const applyObjectDefine = (key, obj) => {
185234
parser.hooks.canRename
186235
.for(key)

0 commit comments

Comments
 (0)