-
Notifications
You must be signed in to change notification settings - Fork 261
fix: normalize PURE annotations for Vite 8 and Rolldown #2766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Hector Hernandez (hectorhdzg)
merged 4 commits into
microsoft:main
from
hectorhdzg:hectorhdzg-fix-vite8-pure-annotations
Sep 8, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,132 @@ | ||
| /** | ||
| * pureAnnotations.mjs - Shared helpers for canonicalizing `/*#__PURE__*\/` | ||
| * (and `/*#@__PURE__*\/`) tree-shaking annotations. | ||
| * pureAnnotations.mjs - Shared helpers for normalizing `/*#__PURE__*\/` | ||
| * (and `/*@__PURE__*\/`) tree-shaking annotations. | ||
| * | ||
| * TypeScript emits parenthesized PURE annotations with whitespace after the | ||
| * opening parenthesis, e.g. `( /*#__PURE__*\/"http.")`. The wrapping | ||
| * parentheses are required so that older versions of Rollup / Webpack / Terser | ||
| * still tree-shake the constants, so they must NOT be removed. However, newer | ||
| * bundlers such as Rolldown (Vite 8) are stricter and reject the spaced form, | ||
| * emitting `[INVALID_ANNOTATION]` warnings. Canonicalizing to the flush form | ||
| * `(/*#__PURE__*\/...)` is accepted by every bundler while preserving the | ||
| * tree-shaking behaviour. | ||
| * PURE annotations are only meaningful on call and new expressions. The | ||
| * source retains legacy parenthesized annotations, while generated output is | ||
| * parsed so invalid annotations can be removed without touching annotation | ||
| * text inside strings or unrelated comments. | ||
| * | ||
| * This single source of truth is shared by: | ||
| * - rollup.base.config.js `fixPureAnnotations()` (rollup-bundled dist/es5), | ||
| * which imports it directly (rollup inlines it when bundling the config). | ||
| * - tools/grunt-tasks/fixPureAnnotations.js `fix-pure` (tsc dist-es5), which | ||
| * loads it via dynamic import() from the CommonJS grunt task. | ||
| * | ||
| * It is authored as an ES module (.mjs) so the rollup config bundler (which | ||
| * does not run the commonjs plugin) can consume the named exports. | ||
| */ | ||
|
|
||
| // Matches an opening parenthesis followed by a (possibly whitespace padded) | ||
| // PURE / @__PURE__ annotation, capturing the leading marker char (# or @). | ||
| export var PURE_COMMENT_CANONICALIZE = /\(\s*\/\*\s*([#@])__PURE__\s*\*\/\s*/g; | ||
| import { parse } from "acorn"; | ||
|
|
||
| var PURE_COMMENT = /^\s*([#@])__PURE__\s*$/; | ||
|
|
||
| /** | ||
| * Rewrites any spaced PURE annotation forms in the supplied code to the | ||
| * canonical flush-against-the-paren form. Returns the (possibly unchanged) | ||
| * code string. | ||
| * Removes PURE annotations that do not apply to a call or new expression and | ||
| * canonicalizes valid annotations. Returns the (possibly unchanged) code. | ||
| * @param {string} code | ||
| * @returns {string} | ||
| */ | ||
| export function canonicalizePureAnnotations(code) { | ||
| return code.replace(PURE_COMMENT_CANONICALIZE, "(/*$1__PURE__*/"); | ||
| if (code.indexOf("__PURE__") === -1) { | ||
| return code; | ||
| } | ||
|
|
||
| var comments = []; | ||
| var tokens = []; | ||
| var validExpressionStarts = {}; | ||
|
|
||
| var ast = parse(code, { | ||
| allowHashBang: true, | ||
| ecmaVersion: "latest", | ||
| onComment: comments, | ||
| onToken: tokens, | ||
| sourceType: "module" | ||
| }); | ||
|
|
||
| _collectValidExpressionStarts(ast, validExpressionStarts); | ||
|
|
||
| var edits = []; | ||
| comments.forEach(function (comment) { | ||
| if (comment.type !== "Block") { | ||
| return; | ||
| } | ||
|
|
||
| var pureMatch = PURE_COMMENT.exec(comment.value); | ||
| if (!pureMatch) { | ||
| return; | ||
| } | ||
|
|
||
| var tokenIndex = _findNextToken(tokens, comment.end); | ||
| var immediateToken = tokens[tokenIndex]; | ||
| var isValid = !!(immediateToken && validExpressionStarts[immediateToken.start]); | ||
| var start = comment.start; | ||
| var previous = start - 1; | ||
| while (previous >= 0 && /\s/.test(code.charAt(previous))) { | ||
| previous--; | ||
| } | ||
|
|
||
| if (code.charAt(previous) === "(") { | ||
| start = previous + 1; | ||
| } | ||
|
|
||
| var end = comment.end; | ||
| if (immediateToken && /^\s*$/.test(code.substring(end, immediateToken.start))) { | ||
| end = immediateToken.start; | ||
| } | ||
|
|
||
| edits.push({ | ||
| end: end, | ||
| replacement: isValid ? "/*" + pureMatch[1] + "__PURE__*/" : "", | ||
| start: start | ||
| }); | ||
| }); | ||
|
|
||
| for (var lp = edits.length - 1; lp >= 0; lp--) { | ||
| var edit = edits[lp]; | ||
| code = code.substring(0, edit.start) + edit.replacement + code.substring(edit.end); | ||
| } | ||
|
|
||
| return code; | ||
| } | ||
|
|
||
| function _collectValidExpressionStarts(node, validExpressionStarts) { | ||
| var pending = [node]; | ||
|
|
||
| while (pending.length > 0) { | ||
| var current = pending.pop(); | ||
| if (!current || typeof current !== "object") { | ||
| continue; | ||
| } | ||
|
|
||
| if (current.type === "CallExpression" || current.type === "NewExpression") { | ||
| validExpressionStarts[current.start] = true; | ||
| } | ||
|
|
||
| Object.keys(current).forEach(function (key) { | ||
| var value = current[key]; | ||
| if (Array.isArray(value)) { | ||
| value.forEach(function (child) { | ||
| if (child && typeof child === "object") { | ||
| pending.push(child); | ||
| } | ||
| }); | ||
| } else if (value && typeof value === "object" && typeof value.type === "string") { | ||
| pending.push(value); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| function _findNextToken(tokens, position) { | ||
| var low = 0; | ||
| var high = tokens.length; | ||
|
|
||
| while (low < high) { | ||
| var middle = (low + high) >> 1; | ||
| if (tokens[middle].start < position) { | ||
| low = middle + 1; | ||
| } else { | ||
| high = middle; | ||
| } | ||
| } | ||
|
|
||
| return low; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.