2013-09-13 17:17:57 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2018-07-30 23:08:51 +08:00
|
|
|
|
2016-12-31 00:34:29 +08:00
|
|
|
"use strict";
|
2013-09-13 17:17:57 +08:00
|
|
|
|
2019-10-22 15:27:52 +08:00
|
|
|
const RuntimeGlobals = require("./RuntimeGlobals");
|
|
|
|
const ConstDependency = require("./dependencies/ConstDependency");
|
|
|
|
const BasicEvaluatedExpression = require("./javascript/BasicEvaluatedExpression");
|
2018-07-03 16:24:29 +08:00
|
|
|
const {
|
|
|
|
approve,
|
|
|
|
evaluateToString,
|
2018-11-06 02:03:12 +08:00
|
|
|
toConstantDependency
|
2019-10-22 15:27:52 +08:00
|
|
|
} = require("./javascript/JavascriptParserHelpers");
|
2014-06-16 21:18:49 +08:00
|
|
|
|
2018-07-04 07:46:51 +08:00
|
|
|
/** @typedef {import("./Compiler")} Compiler */
|
2020-08-29 00:46:56 +08:00
|
|
|
/** @typedef {import("estree").Expression} Expression */
|
2019-10-11 21:46:57 +08:00
|
|
|
/** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
|
2020-03-21 21:01:38 +08:00
|
|
|
/** @typedef {null|undefined|RegExp|Function|string|number|boolean|bigint|undefined} CodeValuePrimitive */
|
|
|
|
/** @typedef {RecursiveArrayOrRecord<CodeValuePrimitive|RuntimeValue>} CodeValue */
|
2018-07-04 07:46:51 +08:00
|
|
|
|
2018-03-19 11:20:44 +08:00
|
|
|
class RuntimeValue {
|
|
|
|
constructor(fn, fileDependencies) {
|
|
|
|
this.fn = fn;
|
|
|
|
this.fileDependencies = fileDependencies || [];
|
|
|
|
}
|
|
|
|
|
|
|
|
exec(parser) {
|
2019-01-09 20:23:26 +08:00
|
|
|
const buildInfo = parser.state.module.buildInfo;
|
2018-10-25 03:34:03 +08:00
|
|
|
if (this.fileDependencies === true) {
|
2019-01-09 20:23:26 +08:00
|
|
|
buildInfo.cacheable = false;
|
2018-10-25 03:34:03 +08:00
|
|
|
} else {
|
|
|
|
for (const fileDependency of this.fileDependencies) {
|
2019-01-09 20:23:26 +08:00
|
|
|
buildInfo.fileDependencies.add(fileDependency);
|
2018-10-25 03:34:03 +08:00
|
|
|
}
|
2018-03-19 11:20:44 +08:00
|
|
|
}
|
|
|
|
|
2018-10-31 16:21:03 +08:00
|
|
|
return this.fn({ module: parser.state.module });
|
2018-03-19 11:20:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-29 00:46:56 +08:00
|
|
|
/**
|
|
|
|
* @param {any[]|{[k: string]: any}} obj obj
|
|
|
|
* @param {JavascriptParser} parser Parser
|
|
|
|
* @param {number} ecmaVersion EcmaScript version
|
|
|
|
* @param {boolean|null=} asiSafe asi safe
|
|
|
|
* @returns {string} code converted to string that evaluates
|
|
|
|
*/
|
|
|
|
const stringifyObj = (obj, parser, ecmaVersion, asiSafe) => {
|
|
|
|
let code;
|
|
|
|
let arr = Array.isArray(obj);
|
|
|
|
if (arr) {
|
|
|
|
code = `[${obj
|
|
|
|
.map(code => toCode(code, parser, ecmaVersion, null))
|
|
|
|
.join(",")}]`;
|
|
|
|
} else {
|
|
|
|
code = `{${Object.keys(obj)
|
2018-02-25 09:00:20 +08:00
|
|
|
.map(key => {
|
|
|
|
const code = obj[key];
|
2020-08-29 00:46:56 +08:00
|
|
|
return (
|
|
|
|
JSON.stringify(key) + ":" + toCode(code, parser, ecmaVersion, null)
|
|
|
|
);
|
2018-02-25 09:00:20 +08:00
|
|
|
})
|
2020-08-29 00:46:56 +08:00
|
|
|
.join(",")}}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (asiSafe) {
|
|
|
|
case null:
|
|
|
|
return code;
|
|
|
|
case true:
|
|
|
|
return arr ? code : `(${code})`;
|
|
|
|
case false:
|
|
|
|
return arr ? `;${code}` : `;(${code})`;
|
|
|
|
default:
|
|
|
|
return `Object(${code})`;
|
|
|
|
}
|
2017-11-08 18:32:05 +08:00
|
|
|
};
|
|
|
|
|
2018-05-20 15:32:59 +08:00
|
|
|
/**
|
|
|
|
* Convert code to a string that evaluates
|
2018-06-23 05:17:55 +08:00
|
|
|
* @param {CodeValue} code Code to evaluate
|
2018-07-17 17:32:15 +08:00
|
|
|
* @param {JavascriptParser} parser Parser
|
2020-03-02 19:16:36 +08:00
|
|
|
* @param {number} ecmaVersion EcmaScript version
|
2020-08-29 00:46:56 +08:00
|
|
|
* @param {boolean|null=} asiSafe asi safe
|
2018-05-20 15:32:59 +08:00
|
|
|
* @returns {string} code converted to string that evaluates
|
|
|
|
*/
|
2020-08-29 00:46:56 +08:00
|
|
|
const toCode = (code, parser, ecmaVersion, asiSafe) => {
|
2018-05-29 20:50:40 +08:00
|
|
|
if (code === null) {
|
|
|
|
return "null";
|
|
|
|
}
|
|
|
|
if (code === undefined) {
|
|
|
|
return "undefined";
|
|
|
|
}
|
2020-03-02 21:47:04 +08:00
|
|
|
if (Object.is(code, -0)) {
|
|
|
|
return "-0";
|
|
|
|
}
|
2018-06-28 17:31:59 +08:00
|
|
|
if (code instanceof RuntimeValue) {
|
2020-08-29 00:46:56 +08:00
|
|
|
return toCode(code.exec(parser), parser, ecmaVersion, asiSafe);
|
2018-06-28 17:31:59 +08:00
|
|
|
}
|
2018-05-29 20:50:40 +08:00
|
|
|
if (code instanceof RegExp && code.toString) {
|
|
|
|
return code.toString();
|
|
|
|
}
|
|
|
|
if (typeof code === "function" && code.toString) {
|
2018-02-25 09:00:20 +08:00
|
|
|
return "(" + code.toString() + ")";
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
|
|
|
if (typeof code === "object") {
|
2020-08-29 00:46:56 +08:00
|
|
|
return stringifyObj(code, parser, ecmaVersion, asiSafe);
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2020-02-29 01:09:59 +08:00
|
|
|
if (typeof code === "bigint") {
|
2020-03-09 17:59:31 +08:00
|
|
|
return ecmaVersion >= 11 ? `${code}n` : `BigInt("${code}")`;
|
2020-02-29 01:09:59 +08:00
|
|
|
}
|
2018-05-29 20:50:40 +08:00
|
|
|
return code + "";
|
2017-11-08 18:32:05 +08:00
|
|
|
};
|
|
|
|
|
2016-12-31 00:34:29 +08:00
|
|
|
class DefinePlugin {
|
2018-05-20 15:32:59 +08:00
|
|
|
/**
|
|
|
|
* Create a new define plugin
|
2018-07-13 09:06:21 +08:00
|
|
|
* @param {Record<string, CodeValue>} definitions A map of global object definitions
|
2018-05-20 15:32:59 +08:00
|
|
|
*/
|
2016-12-31 00:34:29 +08:00
|
|
|
constructor(definitions) {
|
|
|
|
this.definitions = definitions;
|
|
|
|
}
|
2015-07-13 06:20:09 +08:00
|
|
|
|
2018-03-19 11:20:44 +08:00
|
|
|
static runtimeValue(fn, fileDependencies) {
|
|
|
|
return new RuntimeValue(fn, fileDependencies);
|
|
|
|
}
|
|
|
|
|
2018-05-20 15:32:59 +08:00
|
|
|
/**
|
|
|
|
* Apply the plugin
|
2020-04-23 16:48:36 +08:00
|
|
|
* @param {Compiler} compiler the compiler instance
|
2018-06-10 10:37:09 +08:00
|
|
|
* @returns {void}
|
2018-05-20 15:32:59 +08:00
|
|
|
*/
|
2016-12-31 00:34:29 +08:00
|
|
|
apply(compiler) {
|
2017-02-05 07:22:33 +08:00
|
|
|
const definitions = this.definitions;
|
2018-02-25 09:00:20 +08:00
|
|
|
compiler.hooks.compilation.tap(
|
|
|
|
"DefinePlugin",
|
|
|
|
(compilation, { normalModuleFactory }) => {
|
|
|
|
compilation.dependencyTemplates.set(
|
|
|
|
ConstDependency,
|
|
|
|
new ConstDependency.Template()
|
|
|
|
);
|
2020-03-02 19:09:54 +08:00
|
|
|
const { ecmaVersion } = compilation.outputOptions;
|
2015-07-13 06:20:09 +08:00
|
|
|
|
2018-05-20 15:32:59 +08:00
|
|
|
/**
|
|
|
|
* Handler
|
2018-07-17 17:32:15 +08:00
|
|
|
* @param {JavascriptParser} parser Parser
|
2018-06-10 10:37:09 +08:00
|
|
|
* @returns {void}
|
2018-05-20 15:32:59 +08:00
|
|
|
*/
|
2018-02-25 09:00:20 +08:00
|
|
|
const handler = parser => {
|
2018-06-10 10:37:09 +08:00
|
|
|
/**
|
|
|
|
* Walk definitions
|
2018-06-23 05:17:55 +08:00
|
|
|
* @param {Object} definitions Definitions map
|
2018-06-10 10:37:09 +08:00
|
|
|
* @param {string} prefix Prefix string
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-02-25 09:00:20 +08:00
|
|
|
const walkDefinitions = (definitions, prefix) => {
|
|
|
|
Object.keys(definitions).forEach(key => {
|
|
|
|
const code = definitions[key];
|
|
|
|
if (
|
|
|
|
code &&
|
|
|
|
typeof code === "object" &&
|
2018-03-19 11:20:44 +08:00
|
|
|
!(code instanceof RuntimeValue) &&
|
2018-02-25 09:00:20 +08:00
|
|
|
!(code instanceof RegExp)
|
|
|
|
) {
|
|
|
|
walkDefinitions(code, prefix + key + ".");
|
|
|
|
applyObjectDefine(prefix + key, code);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
applyDefineKey(prefix, key);
|
|
|
|
applyDefine(prefix + key, code);
|
|
|
|
});
|
|
|
|
};
|
2015-07-13 06:20:09 +08:00
|
|
|
|
2018-05-20 15:32:59 +08:00
|
|
|
/**
|
|
|
|
* Apply define key
|
|
|
|
* @param {string} prefix Prefix
|
|
|
|
* @param {string} key Key
|
2018-06-10 10:37:09 +08:00
|
|
|
* @returns {void}
|
2018-05-20 15:32:59 +08:00
|
|
|
*/
|
2018-02-25 09:00:20 +08:00
|
|
|
const applyDefineKey = (prefix, key) => {
|
|
|
|
const splittedKey = key.split(".");
|
|
|
|
splittedKey.slice(1).forEach((_, i) => {
|
|
|
|
const fullKey = prefix + splittedKey.slice(0, i + 1).join(".");
|
2018-07-03 16:24:29 +08:00
|
|
|
parser.hooks.canRename.for(fullKey).tap("DefinePlugin", approve);
|
2018-02-25 09:00:20 +08:00
|
|
|
});
|
|
|
|
};
|
2016-12-31 00:34:29 +08:00
|
|
|
|
2018-05-20 15:32:59 +08:00
|
|
|
/**
|
|
|
|
* Apply Code
|
|
|
|
* @param {string} key Key
|
2018-06-23 05:17:55 +08:00
|
|
|
* @param {CodeValue} code Code
|
2018-06-10 10:37:09 +08:00
|
|
|
* @returns {void}
|
2018-05-20 15:32:59 +08:00
|
|
|
*/
|
2018-02-25 09:00:20 +08:00
|
|
|
const applyDefine = (key, code) => {
|
|
|
|
const isTypeof = /^typeof\s+/.test(key);
|
|
|
|
if (isTypeof) key = key.replace(/^typeof\s+/, "");
|
|
|
|
let recurse = false;
|
|
|
|
let recurseTypeof = false;
|
|
|
|
if (!isTypeof) {
|
2018-07-03 16:24:29 +08:00
|
|
|
parser.hooks.canRename.for(key).tap("DefinePlugin", approve);
|
2018-02-25 09:00:20 +08:00
|
|
|
parser.hooks.evaluateIdentifier
|
|
|
|
.for(key)
|
|
|
|
.tap("DefinePlugin", expr => {
|
|
|
|
/**
|
|
|
|
* this is needed in case there is a recursion in the DefinePlugin
|
|
|
|
* to prevent an endless recursion
|
|
|
|
* e.g.: new DefinePlugin({
|
|
|
|
* "a": "b",
|
|
|
|
* "b": "a"
|
|
|
|
* });
|
|
|
|
*/
|
|
|
|
if (recurse) return;
|
|
|
|
recurse = true;
|
2020-03-02 19:09:54 +08:00
|
|
|
const res = parser.evaluate(
|
2020-08-29 00:46:56 +08:00
|
|
|
toCode(code, parser, ecmaVersion, null)
|
2020-03-02 19:09:54 +08:00
|
|
|
);
|
2018-02-25 09:00:20 +08:00
|
|
|
recurse = false;
|
|
|
|
res.setRange(expr.range);
|
|
|
|
return res;
|
|
|
|
});
|
2018-03-19 11:20:44 +08:00
|
|
|
parser.hooks.expression.for(key).tap("DefinePlugin", expr => {
|
2020-08-29 00:46:56 +08:00
|
|
|
const strCode = toCode(
|
|
|
|
code,
|
|
|
|
parser,
|
|
|
|
ecmaVersion,
|
|
|
|
!parser.isAsiPosition(expr.range[0])
|
|
|
|
);
|
2019-09-30 16:08:08 +08:00
|
|
|
if (/__webpack_require__\s*(!?\.)/.test(strCode)) {
|
2018-11-06 02:03:12 +08:00
|
|
|
return toConstantDependency(parser, strCode, [
|
|
|
|
RuntimeGlobals.require
|
|
|
|
])(expr);
|
2019-09-30 16:08:08 +08:00
|
|
|
} else if (/__webpack_require__/.test(strCode)) {
|
|
|
|
return toConstantDependency(parser, strCode, [
|
|
|
|
RuntimeGlobals.requireScope
|
|
|
|
])(expr);
|
2018-03-19 11:20:44 +08:00
|
|
|
} else {
|
2018-07-05 04:08:31 +08:00
|
|
|
return toConstantDependency(parser, strCode)(expr);
|
2018-03-19 11:20:44 +08:00
|
|
|
}
|
|
|
|
});
|
2018-02-25 09:00:20 +08:00
|
|
|
}
|
|
|
|
parser.hooks.evaluateTypeof.for(key).tap("DefinePlugin", expr => {
|
2017-02-06 19:51:26 +08:00
|
|
|
/**
|
|
|
|
* this is needed in case there is a recursion in the DefinePlugin
|
|
|
|
* to prevent an endless recursion
|
|
|
|
* e.g.: new DefinePlugin({
|
2018-02-26 10:49:41 +08:00
|
|
|
* "typeof a": "typeof b",
|
2018-02-25 09:00:20 +08:00
|
|
|
* "typeof b": "typeof a"
|
2017-02-06 19:51:26 +08:00
|
|
|
* });
|
|
|
|
*/
|
2018-02-25 09:00:20 +08:00
|
|
|
if (recurseTypeof) return;
|
|
|
|
recurseTypeof = true;
|
2018-03-19 11:20:44 +08:00
|
|
|
const typeofCode = isTypeof
|
2020-08-29 00:46:56 +08:00
|
|
|
? toCode(code, parser, ecmaVersion, null)
|
|
|
|
: "typeof (" + toCode(code, parser, ecmaVersion, null) + ")";
|
2018-02-25 09:00:20 +08:00
|
|
|
const res = parser.evaluate(typeofCode);
|
|
|
|
recurseTypeof = false;
|
2016-12-31 00:34:29 +08:00
|
|
|
res.setRange(expr.range);
|
|
|
|
return res;
|
|
|
|
});
|
2018-02-25 09:00:20 +08:00
|
|
|
parser.hooks.typeof.for(key).tap("DefinePlugin", expr => {
|
2018-03-19 11:20:44 +08:00
|
|
|
const typeofCode = isTypeof
|
2020-08-29 00:46:56 +08:00
|
|
|
? toCode(code, parser, ecmaVersion, null)
|
|
|
|
: "typeof (" + toCode(code, parser, ecmaVersion, null) + ")";
|
2018-02-25 09:00:20 +08:00
|
|
|
const res = parser.evaluate(typeofCode);
|
|
|
|
if (!res.isString()) return;
|
2018-07-03 16:24:29 +08:00
|
|
|
return toConstantDependency(
|
2018-02-25 09:00:20 +08:00
|
|
|
parser,
|
|
|
|
JSON.stringify(res.string)
|
|
|
|
).bind(parser)(expr);
|
|
|
|
});
|
|
|
|
};
|
2016-09-14 18:04:42 +08:00
|
|
|
|
2018-05-20 15:32:59 +08:00
|
|
|
/**
|
|
|
|
* Apply Object
|
|
|
|
* @param {string} key Key
|
|
|
|
* @param {Object} obj Object
|
2018-06-10 10:37:09 +08:00
|
|
|
* @returns {void}
|
2018-05-20 15:32:59 +08:00
|
|
|
*/
|
2018-02-25 09:00:20 +08:00
|
|
|
const applyObjectDefine = (key, obj) => {
|
2018-07-03 16:24:29 +08:00
|
|
|
parser.hooks.canRename.for(key).tap("DefinePlugin", approve);
|
2018-02-25 09:00:20 +08:00
|
|
|
parser.hooks.evaluateIdentifier
|
|
|
|
.for(key)
|
|
|
|
.tap("DefinePlugin", expr =>
|
2020-07-21 18:57:32 +08:00
|
|
|
new BasicEvaluatedExpression()
|
|
|
|
.setTruthy()
|
|
|
|
.setSideEffects(false)
|
|
|
|
.setRange(expr.range)
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
|
|
|
parser.hooks.evaluateTypeof
|
|
|
|
.for(key)
|
2018-07-03 16:24:29 +08:00
|
|
|
.tap("DefinePlugin", evaluateToString("object"));
|
2018-03-19 11:20:44 +08:00
|
|
|
parser.hooks.expression.for(key).tap("DefinePlugin", expr => {
|
2020-08-29 00:46:56 +08:00
|
|
|
const strCode = stringifyObj(
|
|
|
|
obj,
|
|
|
|
parser,
|
|
|
|
ecmaVersion,
|
|
|
|
!parser.isAsiPosition(expr.range[0])
|
|
|
|
);
|
2018-03-19 11:20:44 +08:00
|
|
|
|
2019-09-30 16:08:08 +08:00
|
|
|
if (/__webpack_require__\s*(!?\.)/.test(strCode)) {
|
2018-11-06 02:03:12 +08:00
|
|
|
return toConstantDependency(parser, strCode, [
|
|
|
|
RuntimeGlobals.require
|
|
|
|
])(expr);
|
2019-09-30 16:08:08 +08:00
|
|
|
} else if (/__webpack_require__/.test(strCode)) {
|
|
|
|
return toConstantDependency(parser, strCode, [
|
|
|
|
RuntimeGlobals.requireScope
|
|
|
|
])(expr);
|
2018-07-05 04:08:31 +08:00
|
|
|
} else {
|
|
|
|
return toConstantDependency(parser, strCode)(expr);
|
2018-03-19 11:20:44 +08:00
|
|
|
}
|
|
|
|
});
|
2018-02-25 09:00:20 +08:00
|
|
|
parser.hooks.typeof
|
|
|
|
.for(key)
|
|
|
|
.tap(
|
|
|
|
"DefinePlugin",
|
2018-07-03 16:24:29 +08:00
|
|
|
toConstantDependency(parser, JSON.stringify("object"))
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
|
|
|
};
|
2017-11-08 18:32:05 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
walkDefinitions(definitions, "");
|
|
|
|
};
|
2017-12-14 17:22:27 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
normalModuleFactory.hooks.parser
|
|
|
|
.for("javascript/auto")
|
|
|
|
.tap("DefinePlugin", handler);
|
|
|
|
normalModuleFactory.hooks.parser
|
|
|
|
.for("javascript/dynamic")
|
|
|
|
.tap("DefinePlugin", handler);
|
|
|
|
normalModuleFactory.hooks.parser
|
|
|
|
.for("javascript/esm")
|
|
|
|
.tap("DefinePlugin", handler);
|
|
|
|
}
|
|
|
|
);
|
2016-12-31 00:34:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = DefinePlugin;
|