在 compiler
中找到的 parser
實例用於解析 webpack 處理的每個模組。parser
是另一個 webpack 類別,它延伸了 tapable
,並提供各種 tapable
hook,外掛作者可以使用這些 hook 來自訂解析程序。
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
也可能可用,具體取決於 hook 的類型。
parser
公開以下生命週期 hook,並且可以這樣存取
SyncBailHook
在評估一個包含自由變數的 typeof
的表達式時觸發
identifier
expression
parser.hooks.evaluateTypeof
.for('myIdentifier')
.tap('MyPlugin', (expression) => {
/* ... */
return expressionResult;
});
這將觸發 evaluateTypeof
hook
const a = typeof myIdentifier;
這不會觸發
const myIdentifier = 0;
const b = typeof myIdentifier;
SyncBailHook
在評估表達式時呼叫。
expressionType
expression
例如
index.js
const a = new String();
MyPlugin.js
parser.hooks.evaluate.for('NewExpression').tap('MyPlugin', (expression) => {
/* ... */
return expressionResult;
});
其中表達式類型為
'ArrowFunctionExpression'
'AssignmentExpression'
'AwaitExpression'
'BinaryExpression'
'CallExpression'
'ClassExpression'
'ConditionalExpression'
'FunctionExpression'
'Identifier'
'LogicalExpression'
'MemberExpression'
'NewExpression'
'ObjectExpression'
'SequenceExpression'
'SpreadElement'
'TaggedTemplateExpression'
'TemplateLiteral'
'ThisExpression'
'UnaryExpression'
'UpdateExpression'
SyncBailHook
在評估一個是自由變數的識別碼時呼叫。
identifier
expression
SyncBailHook
在評估一個是已定義變數的識別碼時呼叫。
identifier
expression
SyncBailHook
在評估成功評估的表達式的成員函數呼叫時呼叫。
identifier
expression
param
此表達式會觸發掛勾
index.js
const a = expression.myFunc();
MyPlugin.js
parser.hooks.evaluateCallExpressionMember
.for('myFunc')
.tap('MyPlugin', (expression, param) => {
/* ... */
return expressionResult;
});
SyncBailHook
一般用途掛勾,在程式區段中每個已剖析的陳述呼叫。
statement
parser.hooks.statement.tap('MyPlugin', (statement) => {
/* ... */
});
其中 statement.type
可能為
'BlockStatement'
'VariableDeclaration'
'FunctionDeclaration'
'ReturnStatement'
'ClassDeclaration'
'ExpressionStatement'
'ImportDeclaration'
'ExportAllDeclaration'
'ExportDefaultDeclaration'
'ExportNamedDeclaration'
'IfStatement'
'SwitchStatement'
'ForInStatement'
'ForOfStatement'
'ForStatement'
'WhileStatement'
'DoWhileStatement'
'ThrowStatement'
'TryStatement'
'LabeledStatement'
'WithStatement'
SyncBailHook
在剖析 if 陳述時呼叫。與 statement
掛勾相同,但僅在 statement.type == 'IfStatement'
時觸發。
statement
SyncBailHook
在剖析具有 標籤 的陳述時呼叫。這些陳述具有 statement.type === 'LabeledStatement'
。
labelName
statement
SyncBailHook
在程式區段中每個匯入陳述呼叫。source
參數包含匯入檔案的名稱。
statement
source
下列匯入陳述會觸發一次掛勾
index.js
import _ from 'lodash';
MyPlugin.js
parser.hooks.import.tap('MyPlugin', (statement, source) => {
// source == 'lodash'
});
SyncBailHook
針對每個 import
陳述式的每個指定元呼叫。
statement
source
exportName
identifierName
下列 import 陳述式將觸發此掛勾兩次
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
針對程式碼片段中的每個 export
陳述式呼叫。
statement
SyncBailHook
針對每個 export
-import 陳述式呼叫,例如:export * from 'otherModule';
。
statement
source
SyncBailHook
針對每個匯出宣告的 export
陳述式呼叫。
statement
declaration
這些匯出將觸發此掛勾
export const myVar = 'hello'; // also var, let
export function FunctionName() {}
export class ClassName {}
SyncBailHook
針對每個匯出表達式的 export
陳述式呼叫,例如 export default expression;
。
statement
declaration
SyncBailHook
針對每個 export
陳述式的每個指定元呼叫。
statement
identifierName
exportName
index
SyncBailHook
針對每個 export
-import 陳述式的每個指定元呼叫。
statement
source
identifierName
exportName
index
SyncBailHook
在解析變數宣告時呼叫。
declaration
SyncBailHook
在使用 let
解析變數宣告時呼叫
declaration
SyncBailHook
在使用 const
解析變數宣告時呼叫
declaration
SyncBailHook
在使用 var
解析變數宣告時呼叫
declaration
SyncBailHook
在重新命名識別碼之前觸發,以判斷是否允許重新命名。這通常與 rename
勾子一起使用。
identifier
expression
var a = b;
parser.hooks.canRename.for('b').tap('MyPlugin', (expression) => {
// returning true allows renaming
return true;
});
SyncBailHook
在重新命名以取得新識別碼時觸發。只有當 canRename
回傳 true
時,才會呼叫此勾子。
identifier
expression
var a = b;
parser.hooks.rename.for('b').tap('MyPlugin', (expression) => {});
SyncBailHook
在解析 AssignmentExpression
之前,在解析已指派運算式時呼叫。
identifier
expression
a += b;
parser.hooks.assigned.for('a').tap('MyPlugin', (expression) => {
// this is called before parsing b
});
SyncBailHook
在解析 AssignmentExpression
之前,在解析指派運算式時呼叫。
identifier
expression
a += b;
parser.hooks.assigned.for('a').tap('MyPlugin', (expression) => {
// this is called before parsing a
});
SyncBailHook
在解析識別碼的 typeof
時觸發
identifier
expression
SyncBailHook
在解析函式呼叫時呼叫。
identifier
expression
eval(/* something */);
parser.hooks.call.for('eval').tap('MyPlugin', (expression) => {});
SyncBailHook
在解析對物件成員函式的呼叫時觸發。
objectIdentifier
expression, properties
myObj.anyFunc();
parser.hooks.callMemberChain
.for('myObj')
.tap('MyPlugin', (expression, properties) => {});
SyncBailHook
解析 new
表達式時呼叫。
identifier
expression
new MyClass();
parser.hooks.new.for('MyClass').tap('MyPlugin', (expression) => {});
SyncBailHook
解析表達式時呼叫。
identifier
expression
const a = this;
parser.hooks.expression.for('this').tap('MyPlugin', (expression) => {});
SyncBailHook
解析 ConditionalExpression
時呼叫,例如 condition ? a : b
expression
SyncBailHook
取得程式碼片段的抽象語法樹 (AST)
ast
comments