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.
Webpack v4+ will minify your code by default in production mode .
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.
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.
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.
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') ] }) ] };
try this plugin, aims to concat and minify js without webpack:
https://github.com/hxlniada/webpack-concat-plugin
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With