fix(nextjs-integration): make turbopack ENV inlining string/comment-safe#860
Merged
Conversation
…afe via shared AST replacer
Contributor
|
The changes in this PR will be included in the next version bump.
|
commit: |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.


What
The turbopack loader's static replacement of non-sensitive
ENV.KEYreferences was a plain regexString.replaceover the whole source, so matches inside string literals, comments, and template literal text got spliced with the quoted value — e.g.console.log("ENV.PUBLIC_VAR is set")became invalid JS.Replacement is now AST-based, reusing the vite integration's replacement machinery:
createReplacerTransformFnmoved from@varlock/vite-integrationinto@env-spec/utils/ast-replacer(generalized to take any estree-compliantparsefn instead of rollup's plugin context); the vite integration now imports it from there — no behavior change on the vite side.@babel/parser(plugins picked per file extension) and replaces only realENV.KEYmember expressions. String literals, comments, template literal quasis, JSX text, nested members likefoo.ENV.KEY, and longer identifiers likeENV.KEY_LONGERare left untouched;${ENV.KEY}interpolations still inline.Webpack is untouched (it uses DefinePlugin, which was already safe), and the recent turbopack gating is preserved: dev-mode server files still skip inlining in favor of runtime proxy reads, while client components, edge files, and all build-mode files still inline.
Testing
packages/integrations/nextjs/test/loader.test.ts) covering strings, comments, template literals, JSX text, member expressions, longer identifiers, sensitive skipping, the parse-failure fallback, and the dev/build/client/edge inlining gate.ENV.PUBLIC_VARinside a string literal, template-literal text, and JSX text, and the build scenarios assert the text survives verbatim into the prerendered HTML (guards both the turbopack AST path and webpack's DefinePlugin path end-to-end).