Created
September 18, 2019 15:37
-
-
Save yepstepz/87bf390dee7c46bb54f5bd17fb01346e to your computer and use it in GitHub Desktop.
Webpack config
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require("path"); | |
const CleanWebpackPlugin = require('clean-webpack-plugin'); | |
const MiniCssExtractPlugin = require("mini-css-extract-plugin"); | |
const HtmlWebpackPlugin = require("html-webpack-plugin"); | |
const merge = require("webpack-merge"); | |
const HtmlWebpackTagsPlugin = require('html-webpack-tags-plugin') | |
const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin') | |
const TerserJSPlugin = require('terser-webpack-plugin'); | |
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin'); | |
const loadImages = ({ include, exclude, options } = {}) => ({ | |
module: { | |
rules: [ | |
{ | |
test: /\.(png|jpg|webp|svg)$/, | |
include, | |
exclude, | |
use: { | |
loader: "url-loader", | |
options, | |
}, | |
}, | |
], | |
}, | |
}); | |
const commonConfig = merge([ | |
{ | |
// Where to start bundling | |
entry: { | |
app: "./src/index.js", | |
}, | |
// Where to output | |
output: { | |
// Output to the same directory | |
path: path.resolve(__dirname, 'dist'), | |
// Capture name from the entry using a pattern | |
chunkFilename: "[name].[chunkhash:4].js", | |
filename: "[name].[chunkhash:4].js", | |
}, | |
module: { | |
rules: [ | |
{ | |
test: /\.js$/, | |
use: "babel-loader", | |
exclude: /node_modules/, | |
}, | |
{ | |
test: /\.(ttf|eot|woff|otf|woff2)$/, | |
use: { | |
loader: "file-loader", | |
options: { | |
name: "fonts/[name].[ext]", | |
}, | |
}, | |
}, | |
] | |
}, | |
plugins: [ | |
new HtmlWebpackPlugin({ | |
inject: true, | |
inlineSource: '.css$', | |
template: path.resolve(__dirname, 'index.html'), | |
}), | |
new CleanWebpackPlugin(path.resolve(__dirname, 'dist'), {} ), | |
], | |
} | |
]); | |
const prodConfig = merge([ | |
{ | |
module: { | |
rules: [ | |
{ | |
test: /\.css$/, | |
use: ["style-loader", MiniCssExtractPlugin.loader, "css-loader"], | |
}, | |
{ | |
test: /\.s[ac]ss$/i, | |
use: [ | |
"style-loader", | |
{ | |
loader: MiniCssExtractPlugin.loader, | |
options: { | |
publicPath: './' | |
} | |
}, | |
"css-loader", | |
"sass-loader" | |
], | |
}, | |
{ | |
test: /\.(html)$/, | |
use: { | |
loader: 'html-loader', | |
options: { | |
attrs: ['img:src', ':srcset'] | |
} | |
} | |
}, | |
{ | |
test: /\.(jpg|png|webp|svg)$/, | |
use: { | |
loader: "file-loader", | |
options: { | |
name: "/assets/img/[name].[hash].[ext]", | |
}, | |
}, | |
}, | |
], | |
}, | |
plugins: [ | |
new MiniCssExtractPlugin({ | |
filename: '[name].[contenthash].css', | |
}), | |
new ScriptExtHtmlWebpackPlugin({ | |
defaultAttribute: 'defer' | |
}) | |
], | |
}, | |
{ | |
optimization: { | |
splitChunks: { | |
cacheGroups: { | |
commons: { | |
test: /[\\/]node_modules[\\/]/, | |
name: "vendor", | |
chunks: "initial", | |
}, | |
}, | |
}, | |
runtimeChunk: { | |
name: "manifest", | |
}, | |
minimizer: [new TerserJSPlugin({}), new OptimizeCSSAssetsPlugin({})], | |
}, | |
}, | |
{ | |
recordsPath: path.join(__dirname, "records.json") | |
} | |
// loadImages(), | |
]); | |
const devConfig = merge([ | |
{ | |
module: { | |
rules: [ | |
{ | |
test: /\.css$/, | |
use: ["style-loader", "css-loader"], | |
}, | |
{ | |
test: /\.s[ac]ss$/i, | |
use: ["style-loader", "css-loader", "sass-loader"], | |
} | |
], | |
}, | |
}, | |
loadImages(), | |
{ | |
plugins: [ | |
new HtmlWebpackTagsPlugin({ | |
append: true, links: '/src/styles/sprites.css' | |
}) | |
], | |
} | |
]) | |
module.exports = mode => { | |
if (mode === "production") { | |
return merge(commonConfig, prodConfig, { mode }); | |
} | |
return merge(commonConfig, devConfig, { mode }); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment