demos for webpack4
# clone the repository
git clone [email protected]:8788/webpack-demos.git
# install dependencies
cd basic/
npm install
# build for production
npm run build
- zero-config
- basic
- loader
- plugins
- dev-server
- extract-css
- image-base64
- es6+
- lazy-loading
- writing-a-loader
- writing-a-plugin
// package.json
{
// ...
"scripts": {
"build": "webpack --mode production"
},
// ...
}
npm run build
// webpack.config.js
const path = require('path')
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, './dist')
}
}
// webpack.config.js
const path = require('path')
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, './dist')
},
module: {
rules: [
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
}
]
}
}
// webpack.config.js
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, './dist')
},
module: {
rules: [
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html'
})
]
}
// webpack.config.js
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, './dist')
},
module: {
rules: [
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html'
}),
new webpack.HotModuleReplacementPlugin()
],
devServer: {
host: '127.0.0.1',
port: 8081,
contentBase: path.join(__dirname, 'dist'),
open: true,
hot: true,
disableHostCheck: true,
proxy: {
// https://127.0.0.1:81/v2/5c0e62192e00005400043e63
'/v2': {
target: 'https://www.mocky.io/',
changeOrigin: true
}
},
before (app) {
app.get('/api', function(req, res) {
res.json({ code: 200, msg: 'mock test' })
})
}
}
}
// webpack.config.js
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, './dist')
},
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader
},
'css-loader'
]
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html'
}),
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[id].css'
}),
new webpack.HotModuleReplacementPlugin()
],
devServer: {
host: '127.0.0.1',
port: 8081,
contentBase: path.join(__dirname, 'dist'),
open: true,
hot: true,
disableHostCheck: true
}
}
// webpack.config.js
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, './dist')
},
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader
},
'css-loader'
]
},
{
test: /\.(png|jpg|gif)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 4 * 1024, // inline files smaller than 4k
name: '[name].[hash:6].[ext]'
}
}
]
},
{
test: /\.svg$/,
use: [
{
loader: 'svg-url-loader',
options: {
limit: 4 * 1024, // inline files smaller than 4k
name: '[name].[hash:6].[ext]'
}
}
]
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html'
}),
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[id].css'
}),
new OptimizeCSSAssetsPlugin(),
new webpack.HotModuleReplacementPlugin()
],
devServer: {
host: '127.0.0.1',
port: 8081,
contentBase: path.join(__dirname, 'dist'),
open: true,
hot: true,
disableHostCheck: true
}
}
// webpack.config.js
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, './dist')
},
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader
},
'css-loader'
]
},
{
test: /\.js$/,
use: 'babel-loader'
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html'
}),
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[id].css'
}),
new webpack.HotModuleReplacementPlugin()
],
devServer: {
host: '127.0.0.1',
port: 8081,
contentBase: path.join(__dirname, 'dist'),
open: true,
hot: true,
disableHostCheck: true
}
}
// webpack.config.js
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
chunkFilename: '[name].bundle.js',
path: path.resolve(__dirname, './dist')
},
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader
},
'css-loader'
]
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html'
}),
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[id].css'
}),
new webpack.HotModuleReplacementPlugin()
],
devServer: {
host: '127.0.0.1',
port: 8081,
contentBase: path.join(__dirname, 'dist'),
open: true,
hot: true,
disableHostCheck: true
}
}
// markdown-loader.js
const marked = require('marked')
const loaderUtils = require('loader-utils')
module.exports = function (src) {
const options = loaderUtils.getOptions(this)
this.cacheable()
marked.setOptions(options)
return marked(src)
}
// webpack.config.js
const webpack = require('webpack')
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.md$/,
use: [
'html-loader',
{
loader: path.resolve('./loader/markdown-loader.js')
}
]
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html'
}),
new webpack.HotModuleReplacementPlugin()
],
devServer: {
host: '127.0.0.1',
port: 8081,
contentBase: path.join(__dirname, 'dist'),
open: true,
hot: true
}
}
// time-plugin.js
const colors = require('colors')
class TimePlugin {
apply (compiler) {
compiler.hooks.beforeRun.tap('TimePlugin', () => this.startTime = Date.now())
compiler.hooks.done.tapAsync('TimePlugin', (compilation, callback) => {
this.endTime = Date.now()
callback()
console.log(`\nCompile done in: ${this.endTime - this.startTime}ms`.green)
})
}
}
module.exports = TimePlugin
// webpack.config.js
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const TimePlugin = require('./plugin/time-plugin.js')
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, './dist')
},
module: {
rules: [
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html'
}),
new webpack.HotModuleReplacementPlugin(),
new TimePlugin()
],
devServer: {
host: '127.0.0.1',
port: 8081,
contentBase: path.join(__dirname, 'dist'),
open: true,
hot: true
}
}