parser
实例,在 compiler
中被发现,是用来解析由 webpack
处理过的每个模块。parser
也是扩展自 tapable
的 webpack 类
并且提供多种 tapable
钩子,
以下示例中,parser
位于 NormalModuleFactory 中,因此需要调用额外钩子
来进行获取:
compiler.hooks.normalModuleFactory.tap('MyPlugin', (factory) => {
factory.hooks.parser
.for('javascript/auto')
.tap('MyPlugin', (parser, options) => {
parser.hooks.someHook.tap(/* ... */);
});
});
和 compiler
用法相同,取决于不同的钩子类型,
也可以在某些钩子上访问 tapAsync
和 tapPromise
。
以下生命周期钩子函数,是由 parser
暴露,可以通过
如下方式访问:
SyncBailHook
Triggered when evaluating an expression consisting in a typeof
of a free variable
identifier
expression
parser.hooks.evaluateTypeof
.for('myIdentifier')
.tap('MyPlugin', (expression) => {
/* ... */
return expressionResult;
});
这会触发 evaluateTypeof
钩子的调用:
const a = typeof myIdentifier;
This won't trigger:
const myIdentifier = 0;
const b = typeof myIdentifier;
SyncBailHook
Called when evaluating an expression.
expressionType
expression
For example:
index.js
const a = new String();
MyPlugin.js
parser.hooks.evaluate.for('NewExpression').tap('MyPlugin', (expression) => {
/* ... */
return expressionResult;
});
Where the expressions types are:
'ArrowFunctionExpression'
'AssignmentExpression'
'AwaitExpression'
'BinaryExpression'
'CallExpression'
'ClassExpression'
'ConditionalExpression'
'FunctionExpression'
'Identifier'
'LogicalExpression'
'MemberExpression'
'NewExpression'
'ObjectExpression'
'SequenceExpression'
'SpreadElement'
'TaggedTemplateExpression'
'TemplateLiteral'
'ThisExpression'
'UnaryExpression'
'UpdateExpression'
SyncBailHook
Called when evaluating an identifier that is a free variable.
identifier
expression
SyncBailHook
Called when evaluating an identifier that is a defined variable.
identifier
expression
SyncBailHook
Called when evaluating a call to a member function of a successfully evaluated expression.
identifier
expression
param
This expression will trigger the hook:
index.js
const a = expression.myFunc();
MyPlugin.js
parser.hooks.evaluateCallExpressionMember
.for('myFunc')
.tap('MyPlugin', (expression, param) => {
/* ... */
return expressionResult;
});
SyncBailHook
General purpose hook that is called for every parsed statement in a code fragment.
statement
parser.hooks.statement.tap('MyPlugin', (statement) => {
/* ... */
});
Where the statement.type
could be:
'BlockStatement'
'VariableDeclaration'
'FunctionDeclaration'
'ReturnStatement'
'ClassDeclaration'
'ExpressionStatement'
'ImportDeclaration'
'ExportAllDeclaration'
'ExportDefaultDeclaration'
'ExportNamedDeclaration'
'IfStatement'
'SwitchStatement'
'ForInStatement'
'ForOfStatement'
'ForStatement'
'WhileStatement'
'DoWhileStatement'
'ThrowStatement'
'TryStatement'
'LabeledStatement'
'WithStatement'
SyncBailHook
Called when parsing an if statement. Same as the statement
hook, but triggered only when statement.type == 'IfStatement'
.
statement
SyncBailHook
Called when parsing statements with a label. Those statements have statement.type === 'LabeledStatement'
.
labelName
statement
SyncBailHook
Called for every import statement in a code fragment. The source
parameter contains the name of the imported file.
statement
source
The following import statement will trigger the hook once:
index.js
import _ from 'lodash';
MyPlugin.js
parser.hooks.import.tap('MyPlugin', (statement, source) => {
// source == 'lodash'
});
SyncBailHook
Called for every specifier of every import
statement.
statement
source
exportName
identifierName
The following import statement will trigger the hook twice:
index.js
import _, { has } from 'lodash';
MyPlugin.js
parser.hooks.importSpecifier.tap(
'MyPlugin',
(statement, source, exportName, identifierName) => {
/* First call
source == 'lodash'
exportName == 'default'
identifierName == '_'
*/
/* Second call
source == 'lodash'
exportName == 'has'
identifierName == 'has'
*/
}
);
SyncBailHook
Called for every export
statement in a code fragment.
statement
SyncBailHook
Called for every export
-import statement eg: export * from 'otherModule';
.
statement
source
SyncBailHook
Called for every export
statement exporting a declaration.
statement
declaration
Those exports will trigger this hook:
export const myVar = 'hello'; // also var, let
export function FunctionName() {}
export class ClassName {}
SyncBailHook
Called for every export
statement exporting an expression e.g.export default expression;
.
statement
declaration
SyncBailHook
Called for every specifier of every export
statement.
statement
identifierName
exportName
index
SyncBailHook
Called for every specifier of every export
-import statement.
statement
source
identifierName
exportName
index
SyncBailHook
Called when parsing a variable declaration.
declaration
SyncBailHook
Called when parsing a variable declaration defined using let
declaration
SyncBailHook
Called when parsing a variable declaration defined using const
declaration
SyncBailHook
Called when parsing a variable declaration defined using var
declaration
SyncBailHook
Triggered before renaming an identifier to determine if the renaming is allowed. This is usually used together with the rename
hook.
identifier
expression
var a = b;
parser.hooks.canRename.for('b').tap('MyPlugin', (expression) => {
// returning true allows renaming
return true;
});
SyncBailHook
Triggered when renaming to get the new identifier. This hook will be called only if canRename
returns true
.
identifier
expression
var a = b;
parser.hooks.rename.for('b').tap('MyPlugin', (expression) => {});
SyncBailHook
Called when parsing an AssignmentExpression
before parsing the assigned expression.
identifier
expression
a += b;
parser.hooks.assigned.for('a').tap('MyPlugin', (expression) => {
// this is called before parsing b
});
SyncBailHook
Called when parsing an AssignmentExpression
before parsing the assign expression.
identifier
expression
a += b;
parser.hooks.assigned.for('a').tap('MyPlugin', (expression) => {
// this is called before parsing a
});
SyncBailHook
Triggered when parsing the typeof
of an identifier
identifier
expression
SyncBailHook
Called when parsing a function call.
identifier
expression
eval(/* something */);
parser.hooks.call.for('eval').tap('MyPlugin', (expression) => {});
SyncBailHook
Triggered when parsing a call to a member function of an object.
objectIdentifier
expression, properties
myObj.anyFunc();
parser.hooks.callMemberChain
.for('myObj')
.tap('MyPlugin', (expression, properties) => {});
SyncBailHook
Invoked when parsing a new
expression.
identifier
expression
new MyClass();
parser.hooks.new.for('MyClass').tap('MyPlugin', (expression) => {});
SyncBailHook
Called when parsing an expression.
identifier
expression
const a = this;
parser.hooks.expression.for('this').tap('MyPlugin', (expression) => {});
SyncBailHook
Called when parsing a ConditionalExpression
e.g. condition ? a : b
expression
SyncBailHook
Get access to the abstract syntax tree (AST) of a code fragment
ast
comments