Answer the question
In order to leave comments, you need to log in
How to specify an alternative source (fallback) for external CDN modules in webpack?
I am migrating a project from RequireJS to webpack. In the old config, some of the libraries are loaded from external sites: Yandex.Metrica, VK scripts, and others. For a third of visitors, advertising is cut and these scripts will not work. In this case, empty path fallbacks with called methods are substituted. For example, for Yandex.Metrica, the following config:
requirejs.config({
paths:
"yametrika": [ "//mc.yandex.ru/metrika/watch", "lib/yamertika_dummy"],
Metrika.params()
, Metrika.reachGoal()
etc. are called. In case AdBlock or similar didn't miss Metrica scripts, empty methods in the stub are called. try{..} catch()
. But I would like to substitute my alternative. Answer the question
In order to leave comments, you need to log in
Haven't tried it on a working project yet, just going with my thoughts.
Looks like you just need to load the gags as polyfills. See the Loading Polyfills documentation .
entry: {
polyfills: './src/polyfills.js',
index: './src/index.js'
},
output: {
filename: '[name].bundle.js',
// создаст polyfills.bundle.js и index.bundle.js
<script>
<script src="https://mc.yandex.ru/metrika/watch.js"></script>
<script>
if( !(Ya in window)) {
var scriptElement = document.createElement('script');
scriptElement.async = false;
scriptElement.src = '/polyfills.bundle.js';
document.head.appendChild(scriptElement);
}
</script>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question