Answer the question
In order to leave comments, you need to log in
How to minify non-modular JS with webpack?
Scripts of third-party services were loaded on the page. Some of them are cut by adblockers, although they do not apply to advertising, so I want to include a local copy of those scripts in the assembly. Scripts are not modular and define global variables and functions.
How to configure webpack to just compile and minify multiple JS from a specific folder?
For example, the folder structure is:
myproject
|- package.json
|- webpack.config.js
|- /dist
|- index.html
|- /src
|- main.js
|- /local_copies
|- xd_connection.js
|- rbadman-html5.min.js
|- adman_init.js
|- preroll.js
/local_copies
and Uglify into a separate bundle, or include it in the general one. (function(w){ w.fastXDM = {...}})(window)
or directly assigning a global object:if (!window.VK) window.VK = {};
VK._Rpc = null;
VK._v = false;
VK._callbacks = {};
// ...
https://vk.com/js/api/xd_connection.js?2
https://ad.mail.ru/static/admanhtml/rbadman-html5.min.js
https://vk.com/js/api/adman_init.js
https://js.appscentrum.com/scr/preroll.js
https://an.yandex.ru/system/context.js
https://mc.yandex.ru/metrika/watch.js
Answer the question
In order to leave comments, you need to log in
Solution: need special loader: script-loader
Install:
Use:import exec from 'script-loader!./script.js';
Everything is the same as with the rest of the files - just because the code is "non-modular" does not mean that you can not import. Just write something like:
import './local_copies/xd_connection.js';
import './local_copies/rbadman-html5.min.js';
// ну и т.д.
const context = require.context('./local_copies', false, /\.js$/);
context.keys().forEach(key => context(key));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question