Get ready for your next interview with our comprehensive question library
Webpack has four core concepts:
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
rules: [{ test: /\.js$/, use: 'babel-loader' }]
},
plugins: [new HtmlWebpackPlugin()]
};
A basic webpack.config.js structure:
const path = require('path');
module.exports = {
mode: 'development', // or 'production'
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
rules: [
// Loader configurations
]
},
plugins: [
// Plugin instances
],
resolve: {
// Module resolution options
},
devServer: {
// Dev server configuration
}
};
Loaders transform files from different languages or formats into modules that Webpack can understand. They run during the build process and transform files on a per-file basis.
module.exports = {
module: {
rules: [
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
]
}
};
Loaders are processed right-to-left (or bottom-to-top) in the use array.
css-loader:
@import and url() like import/require()style-loader:
<style> tags// This processes CSS files and injects them into the DOM
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
}
For production, you might use MiniCssExtractPlugin.loader instead of style-loader to extract CSS into separate files.
Plugins:
Loaders:
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html'
})
]
};
HtmlWebpackPlugin automatically generates HTML files that include your Webpack bundles. It's useful because:
new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html',
chunks: ['app'], // Only include specific chunks
minify: process.env.NODE_ENV === 'production'
})
module.exports = {
devServer: {
static: './dist',
port: 3000,
open: true,
hot: true,
compress: true,
historyApiFallback: true, // For SPA routing
proxy: {
'/api': {
target: 'http://localhost:8080',
changeOrigin: true
}
}
}
};
Key options:
static: Serve static files from directoryhot: Enable HMRproxy: Proxy API requests to another serverhistoryApiFallback: Handle client-side routingUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumAccess all premium content - interview questions, and other learning resources
We regularly update our features and content, to ensure you get the most relevant and updated premium content.
1000 monthly credits
Cancel anytime