[webpack] Add RuntimeValue for DefinePlugin (#42036)

* Add RuntimeValue in DefinePlugin

* Add tests

* Tweak

* Fix

* More specific type and tests

* Fix test error

* Add parser type

* Fix tests error
This commit is contained in:
Yenru Chin
2020-02-02 19:20:15 +00:00
committed by GitHub
parent d8d74cffbd
commit 33fa63bc7f
2 changed files with 92 additions and 3 deletions
+60 -2
View File
@@ -19,6 +19,7 @@
// Rubens Pinheiro Gonçalves Cavalcante <https://github.com/rubenspgcavalcante>
// Anders Kaseorg <https://github.com/andersk>
// Felix Haus <https://github.com/ofhouse>
// Daniel Chin <https://github.com/danielthank>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
@@ -943,7 +944,7 @@ declare namespace webpack {
createModule: SyncBailHook;
module: SyncWaterfallHook;
createParser: HookMap;
parser: HookMap;
parser: HookMap<normalModuleFactory.Parser>;
createGenerator: HookMap;
generator: HookMap;
}
@@ -952,6 +953,48 @@ declare namespace webpack {
hooks: NormalModuleFactoryHooks;
}
namespace normalModuleFactory {
interface ParserHooks {
evaluateTypeof: HookMap;
evaluate: HookMap;
evaluateIdentifier: HookMap;
evaluateDefinedIdentifier: HookMap;
evaluateCallExpressionMember: HookMap;
statement: SyncBailHook;
statementIf: SyncBailHook;
label: HookMap;
import: SyncBailHook;
importSpecifier: SyncBailHook;
export: SyncBailHook;
exportImport: SyncBailHook;
exportDeclaration: SyncBailHook;
exportExpression: SyncBailHook;
exportSpecifier: SyncBailHook;
exportImportSpecifier: SyncBailHook;
varDeclaration: SyncBailHook;
varDeclarationLet: HookMap;
varDeclarationConst: HookMap;
varDeclarationVar: HookMap;
canRename: HookMap;
rename: HookMap;
assigned: HookMap;
typeof: HookMap;
importCall: SyncBailHook;
call: HookMap;
callAnyMember: HookMap;
new: HookMap;
expression: HookMap;
expressionAnyMember: HookMap;
expressionConditionalOperator: SyncBailHook;
expressionLogicalOperator: SyncBailHook;
program: SyncBailHook;
}
class Parser extends Tapable {
hooks: ParserHooks;
}
}
interface ContextModuleFactoryHooks {
beforeResolve: AsyncSeriesWaterfallHook;
afterResolve: AsyncSeriesWaterfallHook;
@@ -1622,7 +1665,22 @@ declare namespace webpack {
}
class DefinePlugin extends Plugin {
constructor(definitions: { [key: string]: any });
constructor(definitions: { [key: string]: DefinePlugin.CodeValuePrimitive});
static runtimeValue(
fn: ({ module }: { module: compilation.Module }) => DefinePlugin.CodeValuePrimitive,
fileDependencies?: string[]
): DefinePlugin.RuntimeValue;
}
namespace DefinePlugin {
class RuntimeValue {
constructor(
fn: ({ module }: { module: compilation.Module }) => CodeValuePrimitive,
fileDependencies?: string[]
);
exec(parser: compilation.normalModuleFactory.Parser): CodeValuePrimitive;
}
type CodeValuePrimitive = string | number | boolean | RegExp | RuntimeValue | null | undefined;
}
class DllPlugin extends Plugin {
+32 -1
View File
@@ -445,7 +445,21 @@ plugin = new webpack.DefinePlugin({
VERSION: JSON.stringify("5fa3b9"),
BROWSER_SUPPORTS_HTML5: true,
TWO: "1+1",
"typeof window": JSON.stringify("object")
"typeof window": JSON.stringify("object"),
RUNTIME: webpack.DefinePlugin.runtimeValue(
() => JSON.stringify("TEST_VALUE")
)
});
plugin = new webpack.DefinePlugin({
TEST_RUNTIME: webpack.DefinePlugin.runtimeValue(
({ module }) => JSON.stringify(module.context)
)
});
plugin = new webpack.DefinePlugin({
TEST_RUNTIME: webpack.DefinePlugin.runtimeValue(
() => JSON.stringify("TEST_VALUE"),
["value.txt"]
)
});
plugin = new webpack.ProvidePlugin(definitions);
plugin = new webpack.ProvidePlugin({
@@ -881,6 +895,23 @@ class BannerPlugin extends webpack.Plugin {
}
}
class DefinePlugin extends webpack.Plugin {
apply(compiler: webpack.Compiler) {
compiler.hooks.compilation.tap(
"DefinePlugin",
(compilation, { normalModuleFactory }) => {
normalModuleFactory.hooks.parser
.for("javascript/auto")
.tap("DefinePlugin", (parser) => {
parser.hooks.evaluateIdentifier
.for("TEST")
.tap("DefinePlugin", () => {});
});
}
);
}
}
configuration = {
module: {
rules: [