GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(javascript): let program taps read the program through the path … · webpack/webpack@a57d78b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • .changeset
    • 046-javascript-program-path-taps.md
    • 046-javascript-program-tree-hook.md
  • docs
    • syntax.md
  • lib
    • dependencies/esm
      • ESMDetectionParserPlugin.js
    • javascript
      • JavascriptMetaInfoPlugin.js
      • JavascriptParser.js
      • JavascriptParserHelpers.js
      • UseStrictPlugin.js
      • syntax-parser.js
  • test
    • configCases/parsing/program-path-taps
      • custom.js
      • esm.js
      • index.js
      • strict-cjs.js
      • test.config.js
      • using.js
      • webpack.config.js
    • unitCases/javascript
      • parser.unittest.js
  • types.d.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎.changeset/046-javascript-program-path-taps.md‎

Copy file name to clipboard
+5Lines changed: 5 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": minor
3+
---
4+
5+
Let a `program` parser tap read the program through the path with `path: true`.
Collapse file

‎.changeset/046-javascript-program-tree-hook.md‎

Copy file name to clipboardExpand all lines: .changeset/046-javascript-program-tree-hook.md
-5Lines changed: 0 additions & 5 deletions
This file was deleted.
Collapse file

‎docs/syntax.md‎

Copy file name to clipboardExpand all lines: docs/syntax.md
+1-1Lines changed: 1 addition & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ Conventions every language keeps, and a new member follows:
126126

127127
## JavaScript
128128

129-
- `syntax.js` reaches `parser` and `printer` through getters: parsing never loads the printer, and the printer loads the parser to read an asset into a `SyntaxTree`. It is the only parser: a source it refuses is refused, and terser's parse options for what is not a program (`parse.expression`, `parse.strict`, `parse.experimental_typescript`) are refused too. `toEstreeObject(node, fields?)` builds the ESTree object of a node of the last parse (`SyntaxTree#estree` of any tree), each call returning the same object: every field, or only those listed and the rest on a later call; a whole program asked for first is built in one pass in id order. It bridges hooks and plugins while the parser moves to that struct-of-arrays tree. A reader the next parse must not disturb, as `JavascriptParser` is for its `programTree` hook, takes the tree with `takeParsedTree()` and releases it once read. The tree already reads as [the node path](#the-node-path) does, a node passed last: `type`, `start`, `end`, `range`, `name`, `value`, `field`, `fieldCount`, `flag` and `textOf` with the `NodeType`, `Field`, `Flag` and `Part` that `syntax.parser` exports. `SourceProcessor` walks the tree a parse writes as the CSS and HTML ones walk theirs, each visitor handed that path and the children taken in source order (a template's quasis and expressions in turn). Asked for `mode: "minify"`, the same walk prints: `syntax-printer.js`'s `printer` returns each node's text from its children's, as `minify` prints a source it neither compresses nor mangles, comments left out and terser's format options read from `format`. A minify walks the tree its own parse wrote (`walkSyntaxTree`) with the comments it keeps, a name printed inside its parent's text where none of its own is read: each is taken as terser's printer reaches it, by the node holding its token, and printed once the text before it is, which a text opening with it leaves to the node printing that text. What terser's stream reads off the text before a node — `has_parens` before a function or object starting a statement, `!--`, `</script` — it reads off the tree instead, the one `parsedTree()` returns while the walk runs, and through the one member only the JavaScript path has, `ancestor(level)`, a node enclosing the current one; and each child's text is read once (`PrintContext#consume`), so a long one is made flat and its pieces let go as it is printed.
129+
- `syntax.js` reaches `parser` and `printer` through getters: parsing never loads the printer, and the printer loads the parser to read an asset into a `SyntaxTree`. It is the only parser: a source it refuses is refused, and terser's parse options for what is not a program (`parse.expression`, `parse.strict`, `parse.experimental_typescript`) are refused too. `toEstreeObject(node, fields?)` builds the ESTree object of a node of the last parse (`SyntaxTree#estree` of any tree), each call returning the same object: every field, or only those listed and the rest on a later call; a whole program asked for first is built in one pass in id order. It bridges hooks and plugins while the parser moves to that struct-of-arrays tree. A reader the next parse must not disturb takes the tree with `takeParsedTree()` and releases it once read. The tree already reads as [the node path](#the-node-path) does, a node passed last: `type`, `start`, `end`, `range`, `name`, `value`, `field`, `fieldCount`, `flag` and `textOf` with the `NodeType`, `Field`, `Flag` and `Part` that `syntax.parser` exports. `SourceProcessor` walks the tree a parse writes as the CSS and HTML ones walk theirs, each visitor handed that path and the children taken in source order (a template's quasis and expressions in turn). Asked for `mode: "minify"`, the same walk prints: `syntax-printer.js`'s `printer` returns each node's text from its children's, as `minify` prints a source it neither compresses nor mangles, comments left out and terser's format options read from `format`. A minify walks the tree its own parse wrote (`walkSyntaxTree`) with the comments it keeps, a name printed inside its parent's text where none of its own is read: each is taken as terser's printer reaches it, by the node holding its token, and printed once the text before it is, which a text opening with it leaves to the node printing that text. What terser's stream reads off the text before a node — `has_parens` before a function or object starting a statement, `!--`, `</script` — it reads off the tree instead, the one `parsedTree()` returns while the walk runs, and through the one member only the JavaScript path has, `ancestor(level)`, a node enclosing the current one; and each child's text is read once (`PrintContext#consume`), so a long one is made flat and its pieces let go as it is printed.
130130
- `syntax-parser.js` — tokenizer, acorn-derived core and every production in one file (any parse reaches the productions). `parseProgram(input, pos, options)` is the one entry, a build's included: it writes a new `SyntaxTree`, read through `A`, and returns the program's node, unless `estree: true` asks for ESTree objects, the program returned, as `JavascriptParser` and `WebManifestParser` do (rebuilding a whole program with `toEstreeObject` costs 30–55% more). Beside `sourceType` (and `auto`, a module turning into a script at a top-level `return`), `ecmaVersion` and `parseError`, it takes acorn's grammar and token options, `comments` (an array to collect into) and `importPhases`. `JavascriptParser`, the concatenation, the minifier, the `SourceProcessor` and the tooling all parse through it; only a custom `parse` function replaces it. The tree path writes each node into the tree, or the one a caller passes as `syntaxTree`, as it finishes, children before their parent, an id in the tree's columns rather than an object; the few the parser reads back by name (a key, a module name, an exported declaration, a function before its body ends) stay objects until their parent is written, and an expression read as a pattern after all is retyped in place. Every edition takes a tree, each owned production checking the edition where acorn does; parser plugins do not, since acorn's own productions read their nodes back. A parse into ESTree objects keeps acorn's productions for an older edition, whose objects lack the newer fields. `regexp.js` (pattern validator) is the one on-demand piece, reached only for a pattern the host engine rejected — never `require` it from a build path.
131131
- **`sourceType` names which production a source is**, for `parseProgram`, `process` and the printer's `minify` alike: `"module"` (the default), `"script"`, or a `FunctionBody` read as acorn's `commonjs` goal reads one (`return` and `new.target` at its top, no `import`, `export` or `import.meta`): `"commonjs"`, a CommonJS module in the function Node wraps it in, or `"event-handler"`, an event handler attribute's value in the one HTML compiles it into, which a `#!` line cannot open. HTML offers only `"event-handler"` for an attribute, never `"module"`. `minify` compresses a function's body inside a function built around its tree, so every rule a function's body reads by applies and a name it reads undeclared stays global (for a handler, the element's, form's or document's); it prints the body without that function, mapped where it was read, its `#!` line and comments kept, and a handler without the last semicolon the closing brace would leave off. `parseProgram` and `process` read import phases (`import defer`, `import source`) too.
132132
- `syntax-printer.js` — webpack's JavaScript minifier, printing, mangling and compressing in one pass; `jsMinify.js` is the `minify` the default minimizer dispatches JS to under `experiments.futureDefaults`, and goes through it alone; without that flag the minimizer dispatches to `terserMinify` as `minimizer-webpack-plugin` publishes it, running the user's own terser since webpack lists it only as a devDependency. Neither needs an option to say which, and the printer itself never loads terser. `createModules` builds what the one pass reads: the parser's tables (`createParseTables`), the mangler's names (`createFrequency`), the output stream (`MinifiedOutput`, which the node printer's stream extends), the parse into a `SyntaxTree` (`parse.parseTree`), its mangling (`mangleTree`, `mangleTreeProperties`, `mangleManglingScopes`) and the `improvements` switches the node printer reads. `prepareMinify` reads and mangles the one source a `SourceProcessor` run minifies, and `createNodePrinter`'s printer compresses it as it prints. `test/specCases/minify-corpora.spectest.js` minifies each source of each corpus in its `CORPORA` list (add a new one there) under the option sets in `OPTION_SETS` with the one `SourceProcessor` pass `jsMinify` runs and with terser, and runs each output whose test states what its input prints (terser's `expect_stdout`, swc's `expected.stdout` and `exec.rs`) in terser's sandbox: it must print that, unless `INHERITED` lists it as misprinting by an option's own contract. webpack must refuse a source where terser does and only there: where the reference refuses a source the spec allows, the parse error it answers with is listed in `REFUSED_BY_REFERENCE` with what makes the source legal, and where webpack refuses a source the reference reads, the source is listed in `REFUSED_BY_WEBPACK` with the rule it breaks (a test262 test expecting a parse error needs no entry). Summed over each corpus, webpack's outputs must gzip no bigger than terser's. An entry that stops matching fails until retired. Each output is also sized against swc's recorded one, a fixture's `output.js` or, for one of terser's cases, what swc's port of it records (gzip decides, raw breaks a tie): one bigger fails unless `test/helpers/swcSmaller.js` lists it under what swc does that webpack does not yet, and a listed one no longer bigger fails until retired. Under the default minimizer's options each source of every corpus but test262 is also minified by `oxc-minify`, held the same way to `test/helpers/oxcSmaller.js`; `test/helpers/rivalSmallerByDesign.js` adds to both lists the sources no minifier change closes (deflate's header, or swc running a transform the case's own options turn off); an oxc output that misprints is no lead. `JS_MINIFY_REPORT=<file>` tallies where webpack's is worse, the same and better. terser's corpus submodule is pinned to the `terser` devDependency — bump both together, and regenerate `data.js`.
Collapse file

‎lib/dependencies/esm/ESMDetectionParserPlugin.js‎

Copy file name to clipboardExpand all lines: lib/dependencies/esm/ESMDetectionParserPlugin.js
+13-40Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
"use strict";
77

88
const {
9-
hasTopLevelUsingDeclaration,
10-
hasTopLevelUsingDeclarationInTree
9+
hasTopLevelUsingDeclaration
1110
} = require("../../javascript/JavascriptParserHelpers");
1211
const {
1312
JAVASCRIPT_MODULE_TYPE_ESM
@@ -81,44 +80,18 @@ module.exports = class ESMDetectionParserPlugin {
8180
enableHarmony(isStrictHarmony);
8281
}
8382
};
84-
if (parser.options.walkIds) {
85-
const { Field, NodeType } = require("../../javascript/syntax-parser");
86-
87-
parser.hooks.programTree.tap(PLUGIN_NAME, (tree, program) => {
88-
topLevelUsingDeclaration = hasTopLevelUsingDeclarationInTree(
89-
tree,
90-
program
91-
);
92-
detectHarmony(() => {
93-
const count = tree.fieldCount(Field.body, program);
94-
for (let i = 0; i < count; i++) {
95-
const type = tree.type(tree.field(i, Field.body, program));
96-
if (
97-
type === NodeType.ImportDeclaration ||
98-
type === NodeType.ExportDefaultDeclaration ||
99-
type === NodeType.ExportNamedDeclaration ||
100-
type === NodeType.ExportAllDeclaration
101-
) {
102-
return true;
103-
}
104-
}
105-
return false;
106-
});
107-
});
108-
} else {
109-
parser.hooks.program.tap(PLUGIN_NAME, (ast) => {
110-
topLevelUsingDeclaration = hasTopLevelUsingDeclaration(ast);
111-
detectHarmony(() =>
112-
ast.body.some(
113-
(statement) =>
114-
statement.type === "ImportDeclaration" ||
115-
statement.type === "ExportDefaultDeclaration" ||
116-
statement.type === "ExportNamedDeclaration" ||
117-
statement.type === "ExportAllDeclaration"
118-
)
119-
);
120-
});
121-
}
83+
parser.hooks.program.tap(PLUGIN_NAME, (ast) => {
84+
topLevelUsingDeclaration = hasTopLevelUsingDeclaration(ast);
85+
detectHarmony(() =>
86+
ast.body.some(
87+
(statement) =>
88+
statement.type === "ImportDeclaration" ||
89+
statement.type === "ExportDefaultDeclaration" ||
90+
statement.type === "ExportNamedDeclaration" ||
91+
statement.type === "ExportAllDeclaration"
92+
)
93+
);
94+
});
12295

12396
parser.hooks.topLevelAwait.tap(PLUGIN_NAME, (node) => {
12497
const module = parser.state.module;
Collapse file

‎lib/javascript/JavascriptMetaInfoPlugin.js‎

Copy file name to clipboardExpand all lines: lib/javascript/JavascriptMetaInfoPlugin.js
+6-17Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ const {
1313
const { getInnerGraphUtils } = require("../optimize/InnerGraph");
1414
const { recordOptimizationBailout } = require("../util/buildDiagnostics");
1515

16-
const {
17-
hasTopLevelUsingDeclaration,
18-
hasTopLevelUsingDeclarationInTree
19-
} = require("./JavascriptParserHelpers");
16+
const { hasTopLevelUsingDeclaration } = require("./JavascriptParserHelpers");
2017

2118
/** @import Compiler from "../Compiler" */
2219
/** @import JavascriptParser from "../javascript/JavascriptParser" */
@@ -50,19 +47,11 @@ class JavascriptMetaInfoPlugin {
5047
parser.state.module.buildInfo.moduleConcatenationBailout =
5148
"a top-level using declaration";
5249
};
53-
if (parser.options.walkIds) {
54-
parser.hooks.programTree.tap(PLUGIN_NAME, (tree, program) => {
55-
if (hasTopLevelUsingDeclarationInTree(tree, program)) {
56-
recordTopLevelUsing();
57-
}
58-
});
59-
} else {
60-
parser.hooks.program.tap(PLUGIN_NAME, (ast) => {
61-
if (hasTopLevelUsingDeclaration(ast)) {
62-
recordTopLevelUsing();
63-
}
64-
});
65-
}
50+
parser.hooks.program.tap(PLUGIN_NAME, (ast) => {
51+
if (hasTopLevelUsingDeclaration(ast)) {
52+
recordTopLevelUsing();
53+
}
54+
});
6655
parser.hooks.call.for("eval").tap(PLUGIN_NAME, () => {
6756
const buildInfo = parser.state.module.buildInfo;
6857
buildInfo.moduleConcatenationBailout = "eval()";

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.