fix: error unexpected token in fetch JS compatibility issue with Webpack 4#10864
Merged
Conversation
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.
Summary
Restore Webpack 4 / ES2018 compatibility for
lib/adapters/fetch.js. The fetch adapter was using the nullish coalescing operator (??), which is ES2020 syntax and breaks consumers bundling axios source with Webpack 4 (e.g. Vue 2.7 projects). axios'slib/source has always been intended to stay ES2018-compatible; the lint config already enforcesecmaVersion: 2018, but the rule was never wired into CI, so the regression slipped through.Linked issue
Closes #10851
Changes
lib/adapters/fetch.js: replaceutils.global ?? globalThiswith an explicit!== undefined && !== nullternary so the source parses under ES2018..github/workflows/run-ci.yml: runnpm run lintin thebuild-and-run-vitestjob, so the existingecmaVersion: 2018rule is actually enforced on PRs.tests/unit/syntaxCompat.test.js: new vitest unit test that parses everylib/**/*.jsfile with acorn atecmaVersion: 2018defence in depth so any future ES2019+ syntax inlib/fails a named test, not just a generic lint error.eslint.config.js: addglobalThis: 'readonly'to the lib globals blocks (the bareglobalThisreference was previously masked by the??parse error; once parsing succeeds,no-undefflags it).package.json/package-lock.json: addacorn ^8.16.0as adevDependencyfor the new test.Checklist
tests/unit/syntaxCompat.test.jscovers the regression classindex.d.tsandindex.d.cts) — N/A, no public API changeSummary by cubic
Restores ES2018 compatibility in the fetch adapter to fix “Unexpected token ??” build errors when bundling
axioswithwebpack4 /vue2.7. CI now enforces ES2018 syntax and adds a parsing test to prevent regressions. Addresses #10851.Description
Summary of changes
??inlib/adapters/fetch.jswith an ES2018-safe ternary.npm run lintto CI to enforceecmaVersion: 2018.tests/unit/syntaxCompat.test.jsparses alllib/**/*.jswithacornat ES2018.globalThis: 'readonly'ineslint.config.jsfor lib targets.acornas a devDependency.Reasoning
lib/is intended to stay ES2018.??is ES2020 and breakswebpack4 builds.Additional context
Docs
/docs/aboutlib/targeting ES2018.webpack4 andvue2.7 when bundlingaxiossource.Testing
tests/unit/syntaxCompat.test.js(acorn ES2018 parse over alllib/**/*.js).npm run lint.Semantic version impact
Written for commit ab8ecfb. Summary will update on new commits.