Examples and Analysis of Low version Browser Compatibility with Vite #10734
gookyn
started this conversation in
Show and tell
Replies: 1 comment
-
Configuration Analysis of Low version Browser Compatibility with Vite |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Compatible with legacy browsers and modern browsers
Target
Compatible browsers and versions
Test the JS API
globalThis
Array.includes
Test cases
Compatible with legacy browser configurations only
1. Configuration options
2. Build log
Due to the
renderLegacyChunks: true
andmodernPolyfills: false
settings, only the polyfills required by legacy browsers are loaded when building; and the polyfills that are actually used (global-this, array.includes) will be loaded.3. Build output file
-legacy
is the file that the legacy browser needs to loadpolyfills-legacy
is the polyfills file that legacy browsers need to load4. index.html
In index.html, there will be two parts of logic for loading JS scripts, and different logics are executed according to whether the browser supports ESM:
<script>
: load js files via<script type="module">
<script>
: additionally load the js file corresponding to-legacy
through<script nomodule>
In addition, you can note: Since ESM is not supported, in the
<script nomodule>
script,System
is used to load the module.5. Visit the page
Chrome 106
The output is correct:
Load the file as high version js:
Chrome 64
Output error:
Reason: No polyfills block generated for low-version modern browsers!
Load the file as high version js:
IE11
The output is correct:
Load file:
Additional loaded
-legacy
files, including polyfills.6. Conclusion
With the default configuration, it is compatible with legacy browsers, but not modern browsers with lower versions.
Increase compatibility with low-version modern browser configuration
1. Configuration options
2. Build log
Set
modernPolyfills: true
to automatically detect polyfills required by modern browsers in the current target range3. Build output file
Generated one more polyfills file for modern browsers
4. index.html
Added load polyfills-modern script
5. Visit the page
Chrome 106
The output is correct:
Load file:
Chrome 64
The output is correct (goal achieved!):
Load file:
IE11
The output is correct:
Load file:
6. Conclusion
Through
renderLegacyChunks: true
compatible with legacy browsers,modernPolyfills: true
compatible with lower version modern browsers, and finally compatible with all versions of browsers, achieve the goal!Insufficient: The introduction of unnecessary files in high-version modern browsers and legacy browsers will increase traffic consumption, but it has little effect.
Other configuration way
In the previous configuration, the required polyfills are automatically detected, and it can also be achieved by manually adding explicit polyfills:
Benefit: Doesn't introduce unnecessary polyfills, as shown in the following figure
Disadvantages: Manual addition is troublesome, easy to miss, and the problem may be discovered after the deployment of production!
Compatible with modern browsers only
Target
Compatible browsers and versions
Configure
Because we don't need to be compatible with legacy browsers, we can set
renderLegacyChunks: false
, and we only need to detect polyfillsmodernPolyfills: true
required by modern browsers.Build result
1. Log
Only detect modern polyfills.
2. Output file
Only extra polyfills-modern files will be output.
3. index.html
Visit the page
Chrome 64, Chrome 106 is correct, and the loading file is the same (correct!)
Conclusion
If legacy browsers are completely ignored, generation of legacy chunks can be disabled:
renderLegacyChunks: false
, in which case thepolyfills
option will also have no effect.At the same time, if you want to be compatible with lower versions of modern browsers, you can set the compatible target range and automatically detect the polyfills needed in the target range through
modernPolyfills: true
, or you can manually add only the polyfills you need.Beta Was this translation helpful? Give feedback.
All reactions