diff --git a/types/webpack/index.d.ts b/types/webpack/index.d.ts index 38728ee46a..e8ef101feb 100644 --- a/types/webpack/index.d.ts +++ b/types/webpack/index.d.ts @@ -19,6 +19,7 @@ // Rubens Pinheiro Gonçalves Cavalcante // Anders Kaseorg // Felix Haus +// Daniel Chin // 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; 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 { diff --git a/types/webpack/test/index.ts b/types/webpack/test/index.ts index ef75f2976b..682c4d734d 100644 --- a/types/webpack/test/index.ts +++ b/types/webpack/test/index.ts @@ -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: [