`webpackAST` returned in `extraInfo` from custom loader appears ignored internally
See original GitHub issueBug report
I think this is some kind of bug, but it’s a little confusing since this optimisation seems under-advertised. The docs do mention:
It can be useful to pass an abstract syntax tree (AST), like ESTree, as the fourth argument (meta) to speed up the build time if you want to share common ASTs between loaders.
but this omits the specific variable name of webpackAST (it implies the fourth argument should just be the AST itself), so I’m basing this report on the source code and checked-in testcases instead.
If I’m doing something wrong here, please let me know!
What is the current behavior?
When defining a custom loader which returns an extraInfo object containing an acorn webpackAST property, webpack appears to ignore the supplied ast.
If the current behavior is a bug, please provide the steps to reproduce.
- Create a custom loader which looks like the ast-loader test case: https://github.com/webpack/webpack/blob/9fcaa243573005d6fdece9a3f8d89a0e8b399613/test/cases/parsing/precreated-ast/ast-loader.js#L27
- Add a console.log to
NormalModule.jshere to log the value ofthis._astandextraInfo: https://github.com/webpack/webpack/blob/293e677b355da0d5bc4ddfc97d2afec114912711/lib/NormalModule.js#L794-L799 - Notice that the logs show
extraInfoasnullalways:
{ extraInfo: null, ast: null }
According to a log earlier on, at this spot, result always appears to be a single-item array containing only the code, despite the loader using this.callback(null, code, map, { webpackAST });.
What is the expected behavior?
Webpack and NormalModule should use the supplied extraInfo + webpackAST to avoid re-parsing files.
Other relevant information:
webpack version: 5.73.0
Node.js version: 16.13.2
Operating System: Ubuntu 18.04.5 LTS
Additional tools: n/a
Issue Analytics
- State:
- Created a year ago
- Comments:8 (8 by maintainers)

Top Related StackOverflow Question
@jdb8
PR welcome
Anyway, there is some trick(pseudo code, just idea):
So you can run any loader inside your loader and do another fancy things
@alexander-akait I think that was the issue: my loader above had another custom loader in front of it that was not passing in or returning the
metaargument:Removing that loader, or updating it to pass through the
mapandmetaarguments resolved the issue (I’m now seeing the console.log inNormalModulelog what I expect:I’m curious though, are there any examples in the wild of
webpackASTbeing used for this kind of performance optimisation? I’m working on a caching layer which can pre-crunch transpiled sources, and the plan was to also pre-generateacornASTs compatible with webpack to skip its internal parsing work.I haven’t benchmarked things properly yet, but so far I’m not seeing that much of a speedup. I’m wondering if the lack of advertising for this optimisation means that it might not be worth aiming for?
If it is worth doing, would you be open to a PR updating some of the documentation around this? I think adding a warning that all loaders in the chain must support
metamight help someone else avoid making the mistake I did. Would also look to more explicitly document the shape of themetaarg beyond what’s currently listed in the current docs.I’ll close this issue since there doesn’t seem to be a bug here, though - thanks for taking a look!