Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I concatenate and minify files using webpack

I want to be able to minify and concatenate files to 1 single file without using grunt How to concatenate and minify multiple CSS and JavaScript files with Grunt.js (0.3.x) can I achieve this with just webpack? I tried many different combinations but the issue is some of the libraries I use assuming that it's AMD or CommonJS format so I keep on getting errors.

like image 568
Binh Phan Avatar asked Feb 18 '16 02:02

Binh Phan


People also ask

Can you minify a Webpack?

Webpack v4+ will minify your code by default in production mode .

How do I bundle a JavaScript file in Webpack?

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.

How does Webpack bundling work?

Webpack is a module bundler. It takes disparate dependencies, creates modules for them and bundles the entire network up into manageable output files. This is especially useful for Single Page Applications (SPAs), which is the defacto standard for Web Applications today.

What are the benefits of using Webpack?

Webpack gives you control over how to treat different assets it encounters. For example, you can decide to inline assets to your JavaScript bundles to avoid requests. Webpack also allows you to use techniques like CSS Modules to couple styling with components.


2 Answers

Merge multiple CSS into single file can done using extract-text-webpack-plugin or webpack-merge-and-include-globally.

https://code.luasoftware.com/tutorials/webpack/merge-multiple-css-into-single-file/

To merge multiple JavaScripts into single file without AMD or CommonJS wrapper can be done using webpack-merge-and-include-globally. Alternatively, you can expose those wrapped modules as global scope using expose-loader.

https://code.luasoftware.com/tutorials/webpack/merge-multiple-javascript-into-single-file-for-global-scope/

Example using webpack-merge-and-include-globally.

const path = require('path'); const MergeIntoSingleFilePlugin = require('webpack-merge-and-include-globally');  module.exports = {   entry: './src/index.js',   output: {     filename: '[name]',     path: path.resolve(__dirname, 'dist'),   },   plugins: [     new MergeIntoSingleFilePlugin({       "bundle.js": [         path.resolve(__dirname, 'src/util.js'),         path.resolve(__dirname, 'src/index.js')       ],       "bundle.css": [         path.resolve(__dirname, 'src/css/main.css'),         path.resolve(__dirname, 'src/css/local.css')       ]     })   ] }; 
like image 148
Desmond Lua Avatar answered Oct 04 '22 03:10

Desmond Lua


try this plugin, aims to concat and minify js without webpack:

https://github.com/hxlniada/webpack-concat-plugin

like image 29
xlhuang Avatar answered Oct 04 '22 02:10

xlhuang