fix(prettier-plugin-liquid): route standalone externals to prettier/standalone#1192
Open
SinhSinhAn wants to merge 1 commit intoShopify:mainfrom
Open
fix(prettier-plugin-liquid): route standalone externals to prettier/standalone#1192SinhSinhAn wants to merge 1 commit intoShopify:mainfrom
SinhSinhAn wants to merge 1 commit intoShopify:mainfrom
Conversation
…tandalone
The webpack config for the browser `standalone.js` bundle declared
the `prettier` external with `commonjs: 'prettier'` and
`commonjs2: 'prettier'`. The resulting UMD wrapper emitted
`require('prettier')` in both the `module.exports` and `exports[...]`
branches, so downstream bundlers (webpack, rollup, vite) processing
`standalone.js` for a browser target resolved prettier's Node-flavored
main entry. That entry imports `module`, `url`, and `path` and crashes
browser builds with "Module not found" errors.
Route the `commonjs` and `commonjs2` keys to `prettier/standalone`,
matching the `amd` branch, so downstream bundlers land on prettier's
browser-safe entry. Keep `root: 'prettier'` so <script>-tag consumers
still bind against the `window.prettier` global populated by
`prettier/standalone.js` on the page.
Closes Shopify#1098
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 is this PR?
Fixes #1098. The standalone browser bundle of
@shopify/prettier-plugin-liquidhas been emittingrequire("prettier")in its UMD wrapper, which breaks browser consumers because prettier's main entry imports Node builtins (module,url,path).What was the issue?
The reporter's screenshot on the issue shows three webpack errors when consuming
standalone.jsfrom a browser build:The root cause is the webpack
externalsdeclaration atpackages/prettier-plugin-liquid/webpack.config.js:The generated UMD wrapper checks
commonjs/commonjs2first (becausemodule.exportsis defined), so downstream bundlers target those branches and getrequire("prettier"). That resolves toprettier/index.mjs, which is the full Node-flavored entry that importsmodule,url, andpath. Those are Node builtins and cannot be resolved for browser targets.The intended browser entry,
prettier/standalone, is browser-safe and already correctly wired for the AMD branch.How this PR fixes it
One-file change in
webpack.config.js: route thecommonjsandcommonjs2keys toprettier/standalone, matching theamdbranch. Theroot: 'prettier'key stays as-is because a<script>tag forprettier/standalone.jsstill populateswindow.prettier, so the global name is unchanged.Verification
After the fix, the top of the freshly built
standalone.jsnow shows:grep 'require("prettier")' standalone.jsreturns zero matches. All references to prettier inside the bundle are now routed through the browser-safe entry.Tests
All 141 existing
prettier-plugin-liquidtests still pass. No existing test covered the build output ofstandalone.js(the test suite is formatter-input/output scenarios run through Node), and the fix is entirely a build-configuration change.Closes #1098