It's main purpose is to put your .js, .css, .html, .sass files into however many bundles you want
(function () {
// your code here
})();
So do all build steps
with zero configuration
Example webpack config
{
entry: './src/index.js',
plugins: [
new CleanWebpackPlugin(['dist']),
new HtmlWebpackPlugin({
title: 'YouTube client by DimaDK24',
})
],
output: {
filename: '[name].[contenthash].js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
'css-loader'
],
},
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader'
},
{
test: /\.(png|jpg|gif|svg|eot|woff|woff2|ttf)$/,
use: 'file-loader'
}
]
}
}
// loader.js
module.exports = function(content) {
return "module.exports = " + JSON.stringify(content);
}
entry: {
index: './src/index.js',
another: './src/another-module.js'
}
But there's an issue with such configuration
import('lodash')
.then({ default: _ } => {
console.log(_.join(['Hello', 'webpack'], ' ')
})
.catch(error => {
console.log(`An error occurred while loading: ${error}`)
});
import(/* webpackPrefetch: true */'lodash')
.then({ default: _ } => {
console.log(_.join(['Hello', 'webpack'], ' ')
})
.catch(error => {
console.log(`An error occurred while loading: ${error}`)
});
The best solution for live reloading of dev website when code changes. You just install and run webpack-dev-server and it will do it.
To start we can just add
output: {
filename: '[name].[contenthash].js',
}
But in real webpack config caching is a bit complicated
Read more:
nice, but a bit old article on Medium
webpack team promises more simple caching config out of the box in webpack 5
Without source maps
!function(t){var n={};function e(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};
With source maps
dev-tool: source-map
For production it's better not to expose original content to end users
So we need another config for production build
Generate source maps, but do not link them with original files
dev-tool: hidden-source-map
You could upload them to your error reporting service