Repository navigation
fix(babel): do not change "use server" and "use cache" code - #77
Merged
Merged
Conversation
Next.js accepts only native async functions in a "use server" or "use cache" file or function, without this and arguments. The preset changed them, and Next.js stopped the build (issue #69). Each plugin of the preset after the first does nothing on server code: a program or a function with "use server", "use cache" or "use cache: <kind>". The guard does not skip the shared traversal, so the other presets of the configuration still change that code. The own traversals of the plugins skip the server functions. The Next.js fixture of the build tool test now starts a server action and a "use cache" function, and a new case builds the Turbopack rule of the guide. The guide makes that rule the main Next.js setup, and tells the pnpm settings, the alias and the costs. 0.3.2, PATCH: a bug fix before 1.0.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With webpack, next/babel compiles the browser code with @babel/preset-env,
and Next.js gives it no targets. preset-env then changes the async
functions of a server action, and Next.js stops the build ("Server Actions
must be async functions"), also without the preset of this library. The
fixture and the guide now give the default browsers of Next.js 16.
The test of withServerCodeSkipped no longer gives noScope to a plugin
visitor (Babel refuses it there), and counts each identifier.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e name
The case next-turbopack-rule and its fixture folder had the same name, so
the case folder was the fixture folder, and the copy failed ("src and dest
cannot be the same"). The fixture is now fixture/next-rule.
Also fix two STE findings in the CHANGELOG and the guide.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Fixes the first problem of #69, and documents the answers to the other questions.
The bug
Next.js accepts only native async functions in a
"use server"or"use cache"file or function, and it forbidsthisandargumentsthere. The preset changed these functions into plain functions that callcoroutinewiththisandarguments. Next.js then stopped the build. This happened in the setup of the guide (babel.config.js), and also with a Turbopack rule that gives only the browser files to the preset: in the browser bundle, Next.js compiles a"use server"file into references to the server, and it checks the functions first.The fix
src/babel/server-code.ts(new):isServerDirective("use server","use cache","use cache: <kind>", the same forms that Next.js reads) andwithServerCodeSkipped(). Each visitor function of a wrapped plugin does nothing on server code: a program with a server directive, or a path in a function with one.path.skip(), because all the plugins of a Babel pass share one traversal: a skip would also stopnext/babel,@babel/preset-typescriptand the other presets in that code. A test pins this.@babel/plugin-transform-async-generator-functionsthroughwithRuntimeSkipped) skip server functions in that traversal, where a skip stops only that plugin./use (?:server|cache)/) for each file keeps the ancestor walk off the files without these words.Tests
src/babel/server-code.test.ts: file directives (both quote styles,"use cache: remote"), inline server actions in a transformed page function, inline"use cache: private"next to a transformed function, nested generators, async generators,await usingand static class fields in a server function, server methods, a late string that is not a directive,"use client", another plugin in the same traversal,transformFromAstwithout the source text, and each visitor form ofwithServerCodeSkipped.test/bundlers(CI only):app/actions.js), which calls a module with an inline"use cache"function next to a plain async function (app/cached.js). All Next.js cases enablecacheComponents.next-turbopack-rule: no Babel config. Aturbopack.rulesrule gives the browser files with async code (acontentcondition) tobabel-loaderwith only the preset, also innode_modules, plus theresolveAliasof the pnpm section of the guide.next/babelcompiles the browser code with@babel/preset-env, and Next.js gives it no targets. preset-env then changes the async functions of a server action to ES5 (_asyncToGenerator, regenerator), and Next.js stops the build, also without this library. The fixturebabel.config.jsnow gives the default browsers of Next.js 16 (chrome 111, edge 111, firefox 111, safari 16.4) aspreset-envtargets, and the guide shows the same.Docs
"use cache"code is skipped.contentcondition and the costs.browserslistroute.Version
0.3.2. PATCH: a bug fix before 1.0.0 (the preset no longer changes server code that Next.js rejects). There is no new API. The merge starts
release.yml.🤖 Generated with Claude Code