Skip to content

Commit

Permalink
Merge pull request #7656 from mohsen1/DefinePlugin-type
Browse files Browse the repository at this point in the history
Add DefinePlugin JSDoc types
  • Loading branch information
sokra committed Jul 15, 2018
2 parents 2702273 + 84d57e3 commit db1475c
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions lib/DefinePlugin.js
Expand Up @@ -9,6 +9,11 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
const ParserHelpers = require("./ParserHelpers");
const NullFactory = require("./NullFactory");

/** @typedef {import("./Compiler")} Compiler */
/** @typedef {import("./Parser")} Parser */
/** @typedef {null|undefined|RegExp|Function|string|number} CodeValuePrimitive */
/** @typedef {CodeValuePrimitive|Record<string, CodeValuePrimitive>|RuntimeValue} CodeValue */

class RuntimeValue {
constructor(fn, fileDependencies) {
this.fn = fn;
Expand Down Expand Up @@ -37,6 +42,12 @@ const stringifyObj = (obj, parser) => {
);
};

/**
* Convert code to a string that evaluates
* @param {CodeValue} code Code to evaluate
* @param {Parser} parser Parser
* @returns {string} code converted to string that evaluates
*/
const toCode = (code, parser) => {
if (code === null) {
return "null";
Expand All @@ -60,6 +71,10 @@ const toCode = (code, parser) => {
};

class DefinePlugin {
/**
* Create a new define plugin
* @param {Record<string, CodeValue>} definitions A map of global object definitions
*/
constructor(definitions) {
this.definitions = definitions;
}
Expand All @@ -68,6 +83,11 @@ class DefinePlugin {
return new RuntimeValue(fn, fileDependencies);
}

/**
* Apply the plugin
* @param {Compiler} compiler Webpack compiler
* @returns {void}
*/
apply(compiler) {
const definitions = this.definitions;
compiler.hooks.compilation.tap(
Expand All @@ -79,7 +99,18 @@ class DefinePlugin {
new ConstDependency.Template()
);

/**
* Handler
* @param {Parser} parser Parser
* @returns {void}
*/
const handler = parser => {
/**
* Walk definitions
* @param {Object} definitions Definitions map
* @param {string} prefix Prefix string
* @returns {void}
*/
const walkDefinitions = (definitions, prefix) => {
Object.keys(definitions).forEach(key => {
const code = definitions[key];
Expand All @@ -98,6 +129,12 @@ class DefinePlugin {
});
};

/**
* Apply define key
* @param {string} prefix Prefix
* @param {string} key Key
* @returns {void}
*/
const applyDefineKey = (prefix, key) => {
const splittedKey = key.split(".");
splittedKey.slice(1).forEach((_, i) => {
Expand All @@ -108,6 +145,12 @@ class DefinePlugin {
});
};

/**
* Apply Code
* @param {string} key Key
* @param {CodeValue} code Code
* @returns {void}
*/
const applyDefine = (key, code) => {
const isTypeof = /^typeof\s+/.test(key);
if (isTypeof) key = key.replace(/^typeof\s+/, "");
Expand Down Expand Up @@ -181,6 +224,12 @@ class DefinePlugin {
});
};

/**
* Apply Object
* @param {string} key Key
* @param {Object} obj Object
* @returns {void}
*/
const applyObjectDefine = (key, obj) => {
parser.hooks.canRename
.for(key)
Expand Down

0 comments on commit db1475c

Please sign in to comment.