Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to merge all js files into one bundle webpack

How to merge all js files into one? With minify in webpack 1.* ?

Here is my part of webpack config

entry: {
    bundle: "./src/index.tsx",
    styles: './static/scss/main.scss',

    main : [
        './static/js/jquery.js',
        './static/js/bowser.js',
        './static/js/bootstrap.js',
        './static/js/jquery.resizeend.js',
        './static/js/slick.js',
        './static/js/jquery.waypoints.js',
        './static/js/jquery.backgroundpos.js',
        './static/js/jquery.fancybox.js',
        './static/js/select2.full.js',
        './static/js/jquery.mousewheel.js',
        './static/js/clipboard.js',
        './static/js/circle-progress.js',
        './static/js/common.js',
        './static/js/main.js'
    ]
},
output: {
    filename: "[name].js",
    path: "../root/js/react",
    library: '[name]'
},

after build all js files are minified but i have problems with these dependencies :

bootstrap can't find jquery

Uncaught Error: Bootstrap's JavaScript requires jQuery
    at Object.1213 (main.js:3)
    at t (main.js:1)
    at Object.0 (main.js:1)
    at t (main.js:1)
    at main.0 (main.js:1)
    at main.js:1

Note: Jquery comes before bootstrap so it should be ok

like image 326
Андрей Гузюк Avatar asked Mar 01 '17 09:03

Андрей Гузюк


People also ask

How do I bundle all JavaScript files?

You can bundle your JavaScript using the CLI command by providing an entry file and output path. Webpack will automatically resolve all dependencies from import and require and bundle them into a single output together with your app's script.

What is JavaScript bundling?

JavaScript bundling is an optimization technique you can use to reduce the number of server requests for JavaScript files. Bundling accomplishes this by merging multiple JavaScript files together into one file to reduce the number of page requests.

Does webpack include dependencies in bundle?

Webpack builds a dependency graph used internally Now all modules that are used in your app are included in the dependency graph. Your project have many installed dependencies in the node_modules folder that should not be included in your client-side JavaScript production bundle.


1 Answers

Jquery comes before bootstrap so it should be ok

No that is not the case, because webpack still keeps them as modules, but bootstrap expects jQuery to be present globally. You can fix this by using the ProvidePlugin, so webpack automatically imports jQuery whenever it sees it being used. Add it to your plugins section in your webpack config:

plugins: [
  new webpack.ProvidePlugin({
    $: 'jquery',
    jQuery: 'jquery'
  })
]

Note: I linked the docs for webpack 2, but this works the exact same for webpack 1.

like image 138
Michael Jungo Avatar answered Sep 20 '22 11:09

Michael Jungo