Skip to content

Commit 7941097

Browse files
committed
docs(api): rewrite the parser documentation
Improve lead-in to give a little more context on what the parser does. Add the latest hooks and use the same layout as the other pages in the section.
1 parent 1555e7c commit 7941097

File tree

1 file changed

+262
-39
lines changed

1 file changed

+262
-39
lines changed

src/content/api/parser.md

Lines changed: 262 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,92 +4,315 @@ group: Plugins
44
sort: 4
55
---
66

7-
The parser instance takes a String and callback and will return an expression when there's a match.
7+
The `parser` instance, found in the `compiler`, is used to parse each module
8+
being processed by webpack. The `parser` is yet another webpack class that
9+
extends `tapable` and provides a variety of `tapable` hooks that can be used by
10+
plugin authors to customize the parsing process.
11+
12+
The `parser` is found within [module factories]() and therefore takes little
13+
more work to access:
814

915
``` js
10-
compiler.parser.plugin("var rewire", function (expr) {
11-
//if you original module has 'var rewire'
12-
//you now have a handle on the expresssion object
13-
return true;
14-
});
16+
compiler.hooks.normalModuleFactory.tap(factory => {
17+
factory.hooks.parser.tap((parser, options) => {
18+
parser.hooks.someHook.tap(...)
19+
})
20+
})
1521
```
1622

23+
As with the `compiler`, `tapAsync` and `tapPromise` may also be available
24+
depending on the type of hook.
25+
1726

18-
## `program(ast)` bailing
27+
## Hooks
1928

20-
General purpose plugin interface for the AST of a code fragment.
29+
The following lifecycle hooks are exposed by the `parser` and can be accessed
30+
as such:
2131

2232

23-
## `statement(statement: Statement)` bailing
33+
### evaluateTypeof
2434

25-
General purpose plugin interface for the statements of the code fragment.
35+
`SyncBailHook`
2636

37+
Evaluate the type of an identifier.
2738

28-
## `call <identifier>(expr: Expression)` bailing
39+
Parameters: `expression`
2940

30-
`abc(1)` => `call abc`
3141

32-
`a.b.c(1)` => `call a.b.c`
42+
### evaluate
3343

44+
`SyncBailHook`
3445

35-
## `expression <identifier>(expr: Expression)` bailing
46+
Evaluate an expression.
3647

37-
`abc` => `expression abc`
48+
Parameters: `expression`
3849

39-
`a.b.c` => `expression a.b.c`
4050

51+
### evaluateIdentifier
4152

42-
## `expression ?:(expr: Expression)` bailing
53+
`SyncBailHook`
4354

44-
`(abc ? 1 : 2)` => `expression ?!`
55+
Evaluate an identifier that is a free variable.
4556

46-
Return a boolean value to omit parsing of the wrong path.
57+
Parameters: `expression`
4758

4859

49-
## `typeof <identifier>(expr: Expression)` bailing
60+
### evaluateDefinedIdentifier
5061

51-
`typeof a.b.c` => `typeof a.b.c`
62+
`SyncBailHook`
5263

64+
Evaluate an identifier that is a defined variable.
5365

54-
## `statement if(statement: Statement)` bailing
66+
Parameters: `expression`
5567

56-
`if(abc) {}` => `statement if`
5768

58-
Return a boolean value to omit parsing of the wrong path.
69+
### evaluateCallExpressionMember
5970

71+
`SyncBailHook`
6072

61-
## `label <labelname>(statement: Statement)` bailing
73+
Evaluate a call to a member function of a successfully evaluated expression.
6274

63-
`xyz: abc` => `label xyz`
75+
Parameters: `expression` `param`
6476

6577

66-
## `var <name>(statement: Statement)` bailing
78+
### statement
6779

68-
`var abc, def` => `var abc` + `var def`
80+
`SyncBailHook`
6981

70-
Return `false` to not add the variable to the known definitions.
82+
General purpose hook that is called when parsing statements in a code fragment.
7183

84+
Parameters: `statement`
7285

73-
## `evaluate <expression type>(expr: Expression)` bailing
7486

75-
Evaluate an expression.
87+
### statementIf
7688

89+
`SyncBailHook`
7790

78-
## `evaluate typeof <identifier>(expr: Expression)` bailing
91+
...
7992

80-
Evaluate the type of an identifier.
93+
Parameters: `statement`
8194

8295

83-
## `evaluate Identifier <identifier>(expr: Expression)` bailing
96+
### label
8497

85-
Evaluate a identifier that is a free var.
98+
`SyncBailHook`
8699

100+
...
87101

88-
## `evaluate defined Identifier <identifier>(expr: Expression)` bailing
102+
Parameters: `statement`
89103

90-
Evaluate a identifier that is a defined var.
91104

105+
### import
92106

93-
## `evaluate CallExpression .<property>(expr: Expression)` bailing
107+
`SyncBailHook`
94108

95-
Evaluate a call to a member function of a successfully evaluated expression.
109+
...
110+
111+
Parameters: `statement` `source`
112+
113+
114+
### importSpecifier
115+
116+
`SyncBailHook`
117+
118+
...
119+
120+
Parameters: `statement` `source` `exportName` `identifierName`
121+
122+
123+
### export
124+
125+
`SyncBailHook`
126+
127+
...
128+
129+
Parameters: `statement`
130+
131+
132+
### exportImport
133+
134+
`SyncBailHook`
135+
136+
...
137+
138+
Parameters: `statement` `source`
139+
140+
141+
### exportDeclaration
142+
143+
`SyncBailHook`
144+
145+
...
146+
147+
Parameters: `statement` `declaration`
148+
149+
150+
### exportExpression
151+
152+
`SyncBailHook`
153+
154+
...
155+
156+
Parameters: `statement` `declaration`
157+
158+
159+
### exportSpecifier
160+
161+
`SyncBailHook`
162+
163+
...
164+
165+
Parameters: `statement` `identifierName` `exportName` `index`
166+
167+
168+
### exportImportSpecifier
169+
170+
`SyncBailHook`
171+
172+
...
173+
174+
Parameters: `statement` `source` `identifierName` `exportName` `index`
175+
176+
177+
### varDeclaration
178+
179+
`SyncBailHook`
180+
181+
...
182+
183+
Parameters: `declaration`
184+
185+
186+
### varDeclarationLet
187+
188+
`SyncBailHook`
189+
190+
...
191+
192+
Parameters: `declaration`
193+
194+
195+
### varDeclarationConst
196+
197+
`SyncBailHook`
198+
199+
...
200+
201+
Parameters: `declaration`
202+
203+
204+
### varDeclarationVar
205+
206+
`SyncBailHook`
207+
208+
...
209+
210+
Parameters: `declaration`
211+
212+
213+
### canRename
214+
215+
`SyncBailHook`
216+
217+
...
218+
219+
Parameters: `initExpression`
220+
221+
222+
### rename
223+
224+
`SyncBailHook`
225+
226+
...
227+
228+
Parameters: `initExpression`
229+
230+
231+
### assigned
232+
233+
`SyncBailHook`
234+
235+
...
236+
237+
Parameters: `expression`
238+
239+
240+
### assign
241+
242+
`SyncBailHook`
243+
244+
...
245+
246+
Parameters: `expression`
247+
248+
249+
### typeof
250+
251+
`SyncBailHook`
252+
253+
...
254+
255+
Parameters: `expression`
256+
257+
258+
### call
259+
260+
`SyncBailHook`
261+
262+
...
263+
264+
Parameters: `expression`
265+
266+
267+
### callAnyMember
268+
269+
`SyncBailHook`
270+
271+
...
272+
273+
Parameters: `expression`
274+
275+
276+
### new
277+
278+
`SyncBailHook`
279+
280+
...
281+
282+
Parameters: `expression`
283+
284+
285+
### expression
286+
287+
`SyncBailHook`
288+
289+
...
290+
291+
Parameters: `expression`
292+
293+
294+
### expressionAnyMember
295+
296+
`SyncBailHook`
297+
298+
...
299+
300+
Parameters: `expression`
301+
302+
303+
### expressionConditionalOperator
304+
305+
`SyncBailHook`
306+
307+
...
308+
309+
Parameters: `expression`
310+
311+
312+
### program
313+
314+
`SyncBailHook`
315+
316+
Get access to the abstract syntax tree (AST) of a code fragment
317+
318+
Parameters: `ast` `comments`

0 commit comments

Comments
 (0)