Repository navigation
wp-build: Keep translator comments next to their calls in built JavaScript - #87189
Conversation
…cript Rolldown's printer only keeps legal, annotation and JSDoc comments, and keeps JSDoc comments only in some positions: before a statement, a call argument, an object property value or a template literal expression. So `/* translators: */` and `// translators:` comments were dropped from the transpiled `build` and `build-module` files, which the script and module bundles are built from, and from the route and widget bundles. `wp i18n make-pot` then extracted the strings from the built files without them. A new Rolldown plugin finds each translator comment that `make-pot` would give to a `__()`, `_x()`, `_n()` or `_nx()` call, using the parser so text in strings, template literals and regular expressions is left alone. When the comment already sits where the printer keeps it, it becomes a JSDoc comment in place. Otherwise, as before a conditional branch, an array element, a `return` argument or in an empty JSX expression container, it moves into the call, before its first argument, which the printer always keeps and `make-pot` reads. Only comments change, so the minified output stays byte-for-byte the same, and the edits go through a magic string so the source maps stay correct. The plugin runs in the transpile step and in the unminified route and widget builds.
🤖 PR meta 🤖🎉 PropsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the |
Trunk dropped the Unreleased Bug Fixes entry for WordPress#75396 and its heading, which the merge had brought back. Remove that entry again and keep this pull request's entry in its own Bug Fixes section.
…e types Replace the loose Node typedef, which allowed any property, with the ESTree types that rolldown/utils exports, and type the gettext calls as call expressions. This fixes the jsdoc/reject-any-type lint warning.
jsnajdr
left a comment
There was a problem hiding this comment.
This is an unpleasant fix for something that should be fixed in Rolldown in the first place, but I'm afraid at the moment we can't do anything else.
The new Rolldown plugins converts all translators comments to JSDoc and also moves them to a place in the AST where Rolldown will preserve them (next to an expression).
Babel and webpack used to preserve all comments. The problem is that comments are not really a regular part of an AST, the parser needs to implement something on top of the regular JS parser to keep track of comments correctly. Babel did that work, and e.g. Prettier does that work, too. But the newer ESBuild and Rolldown tools probably don't bother implementing this; it makes the parser faster.
webpack method of bundling is also very friendly to preserving comments and other stuff in the sources. It keeps the original source intact and only "patches" the places where import and export statements are. It doesn't do a parse+print cycle on every source file.
Even ESBuild was already dropping some translators comments, although just a small percentage. Comments before statements or before JSX attributes were already lost:
// comment before a plain assignment
missingText = __( 'Select post' );
// comment before a return
return __( 'Custom' );
// comment before a JSX attribute
label={ __( 'More' ) }Now Rolldown makes this much more serious, losing almost all comments.
This should be backported to WP 7.2, as I'm afraid we'll be losing translator comments there, too, on the shipped scripts.
|
There was a conflict while trying to cherry-pick the commit to the wp/7.2 branch. Please resolve the conflict manually and create a PR to the wp/7.2 branch. PRs to wp/7.2 are similar to PRs to trunk, but you should base your PR on the wp/7.2 branch instead of trunk. |
|
Based on my checks, the |
Thanks, that's good news 🙂 |
|
Thanks @simison, fair point. I started this separately because the remaining cases (ternary branches, array elements, empty JSX containers) needed the comment moved into the call rather than converted in place, which was a different approach from #87055. In hindsight, suggesting it on #87055 first would have been better. Thanks @obenland for identifying the problem and the JSDoc approach, and @jsnajdr for the review. |
I filed an OXC issue that would help solve our problem upstream: oxc-project/oxc#30338 |
Resolve the conflict in packages/wp-build/CHANGELOG.md by keeping trunk's Unreleased section and listing the WordPress#86766 entry under its Bug Fixes heading, next to WordPress#87189. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EisotksEYkwb8knGHGSXxP
…cript (WordPress#87189) * wp-build: Keep translator comments next to their calls in built JavaScript Rolldown's printer only keeps legal, annotation and JSDoc comments, and keeps JSDoc comments only in some positions: before a statement, a call argument, an object property value or a template literal expression. So `/* translators: */` and `// translators:` comments were dropped from the transpiled `build` and `build-module` files, which the script and module bundles are built from, and from the route and widget bundles. `wp i18n make-pot` then extracted the strings from the built files without them. A new Rolldown plugin finds each translator comment that `make-pot` would give to a `__()`, `_x()`, `_n()` or `_nx()` call, using the parser so text in strings, template literals and regular expressions is left alone. When the comment already sits where the printer keeps it, it becomes a JSDoc comment in place. Otherwise, as before a conditional branch, an array element, a `return` argument or in an empty JSX expression container, it moves into the call, before its first argument, which the printer always keeps and `make-pot` reads. Only comments change, so the minified output stays byte-for-byte the same, and the edits go through a magic string so the source maps stay correct. The plugin runs in the transpile step and in the unminified route and widget builds. * Changelog: Add wp-build entry for translator comments * Changelog: Move wp-build translator comments entry under Bug Fixes Trunk dropped the Unreleased Bug Fixes entry for WordPress#75396 and its heading, which the merge had brought back. Remove that entry again and keep this pull request's entry in its own Bug Fixes section. * wp-build: Type translator comments plugin nodes with Rolldown's ESTree types Replace the loose Node typedef, which allowed any property, with the ESTree types that rolldown/utils exports, and type the gettext calls as call expressions. This fixes the jsdoc/reject-any-type lint warning. Co-authored-by: yyppsk <pranjalpratapsingh@git.wordpress.org> Co-authored-by: simison <simison@git.wordpress.org> Co-authored-by: kraftbj <kraftbj@git.wordpress.org>
What?
Closes #87161
Keeps
translators:comments next to the gettext call they describe in the JavaScript that@wordpress/buildproduces, sowp i18n make-potextracts them from the built files.Why?
Rolldown's printer only keeps legal (
/*!,@license), annotation (@__PURE__) and JSDoc (/** */) comments, and keeps even JSDoc comments only in some positions. So every/* translators: … */and// translators: …comment is lost in two places:build-moduleandbuild, which the script and module bundles are built from (so the.min.js.mapfiles don't have them either);On
trunk,make-potfinds 0 translator comments for the 581 strings inbuild/routes, and 2 for the 3,519 strings in the unminifiedbuild/scriptsfiles.#87055 and #87183 also address this. #87055 rewrites translator comments to
/** translators: … */where they stand, and #87183 emits translation stubs for--no-script-debugbuilds. This PR is an alternative to #87055 for the regular build. The difference is that it also handles comments in positions where Rolldown drops even a JSDoc comment: before a conditional branch, before an array element, in an empty JSX expression container followed by the call, and before areturnargument, a logical operand or a variable initializer. It doesn't overlap with #87183, which covers builds without unminified files.How?
A new Rolldown plugin,
translatorCommentsPlugininpackages/wp-build/lib/translator-comments-plugin.mjs:parseSyncfromrolldown/utils, so comment-like text in strings, template literals and regular expressions is never touched. The hook filter skips modules that don't containtranslators, and a module with no translator comment returns before anything else is done.make-potwould give it to: the first__(),_x(),_n()or_nx()call starting on the line the comment ends on or the next one, or the call the comment is in, before the first argument. A comment with no such call is left alone.// translators:comment becomes/** translators: … */.__( /** translators: … */ 'Draft' ). The printer always keeps a comment before a call argument and prints it on its own line, andmake-potreads the leading comments of a gettext call's arguments. A moved multi-line comment leaves a line break behind, so automatic semicolon insertion is unchanged.Only comments are changed, so every
*.min.jsfile stays byte-for-byte the same. The edits go throughRolldownMagicString, so the source maps stay correct.The plugin runs in the transpile step and in the unminified route (
route.js,content.js) and widget (render.js,widget.js) builds. The script and module bundles are built frombuild-module, which now has the comments in positions the printer keeps, so they need no plugin of their own. The plugin only knowsmake-pot's rules, not anything Gutenberg-specific, so it works the same for any plugin built with@wordpress/build.The worker bundles (
wpWorkers) are built without the plugin, on purpose. A worker bundle is inlined into the package'sworker-codemodule as a JSON string and runs from a Blob URL in its own global scope, somake-potcan't extract strings from it and no script translations are loaded for it: a gettext call in a worker can't be translated either way. Neither worker in Gutenberg (@wordpress/vipsand@wordpress/video-conversion) depends on@wordpress/i18nor has a gettext call or translator comment in its sources or its built bundle.Testing Instructions
trunk, runnpm run build. Copybuild/routesto.pot-test/before/routes, and the*.jsfiles ofbuild/scriptswithout the*.min.jsfiles to.pot-test/before/scripts. Save the checksums of the minified files:find build -name '*.min.js' -print0 | sort -z | xargs -0 sha256sum > min-before.sha.npm run buildand do the same into.pot-test/afterandmin-after.sha.diff min-before.sha min-after.shaprints nothing: all 162*.min.jsfiles are identical.npm run wp-env-test startrunning, for each ofbeforeandafter, and each ofroutesandscripts, runnpm run wp-env-test -- run --env-cwd=wp-content/plugins/gutenberg/.pot-test/<before|after> cli php -d memory_limit=-1 /usr/local/bin/wp i18n make-pot <routes|scripts> <routes|scripts>.pot --ignore-domain(in Git Bash on Windows, prefix it withMSYS_NO_PATHCONV=1 MSYS2_ARG_CONV_EXCL='*').#. translators:lines in each POT file withgrep -c '^#\. translators:'. Compare with the table below..pot-test/after, check that these strings have their comments:%s settings updated.(routes/experiments-home/stage.tsx) inroutes.potSave as pendingandPublishing…(editor),"%s" successfully created.(edit-post) inscripts.potInvalidandDraft(navigation-link/shared/invalid-draft-display.jsx, comments before conditional branches),Block(editor/src/components/sidebar/header.jsx, a comment in an empty JSX expression container) andn/j/Y(date-format-picker, a comment before an array element) inscripts.potbuild/scripts/edit-post/index.min.js.map, the embedded source ofpackages/edit-post/build-module/components/layout/index.mjsnow has the/** translators: %s: Title of the created post or template, e.g: "Hello world". */comment on the line before the"%s" successfully created.call.npm run test:unit -- packages/wp-buildTesting Instructions for Keyboard
Not applicable: build tooling only.
Screenshots or screencast
wp i18n make-pot --ignore-domainon the unminified build output:translators:comments beforetranslators:comments afterbuild/routesbuild/scripts(*.jswithout*.min.js)The number of strings is the same before and after. As a cross-check, I ran
make-poton the.jsand.jsxsources ofpackages/*/srcandroutes(make-potdoesn't scan.tsor.tsxfiles). Of the strings in both POT files, 278 have a translator comment in the source. All 278 have it in the build after this change, against 2 before. 5 more strings get a comment in the build thatmake-potdoesn't pick up in the source, because the comment is in an empty JSX expression container, such asBlockabove.Use of AI Tools
Assisted with Claude Opus 5.5 for testing and verifying accuracy.