Skip to content

Commit 22fe698

Browse files
committed
Webpack optimizations
1 parent a273ac1 commit 22fe698

14 files changed

+1098
-29
lines changed

package-lock.json

+926
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"build": "webpack --mode production --env production",
7+
"build": "NODE_ENV=production webpack --mode=production",
88
"clean": "rm -rf dist",
9-
"start": "webpack-dev-server --open --mode development --env development"
9+
"start": "NODE_ENV=development webpack-dev-server --open --mode=development"
1010
},
1111
"keywords": [],
1212
"author": "",
@@ -21,15 +21,20 @@
2121
"@babel/core": "^7.4.0",
2222
"@babel/preset-env": "^7.4.2",
2323
"@babel/preset-react": "^7.0.0",
24+
"@babel/register": "^7.4.0",
2425
"babel-loader": "^8.0.5",
26+
"compression-webpack-plugin": "^2.0.0",
2527
"css-loader": "^2.1.1",
2628
"html-webpack-inline-source-plugin": "0.0.10",
2729
"html-webpack-plugin": "^3.2.0",
30+
"mini-css-extract-plugin": "^0.5.0",
2831
"node-sass": "^4.11.0",
32+
"optimize-css-assets-webpack-plugin": "^5.0.1",
2933
"postcss-flexbugs-fixes": "^4.1.0",
3034
"postcss-import": "^12.0.1",
3135
"postcss-loader": "^3.0.0",
3236
"postcss-preset-env": "^6.6.0",
37+
"postcss-safe-parser": "^4.0.1",
3338
"sass-loader": "^7.1.0",
3439
"style-loader": "^0.23.1",
3540
"webpack": "^4.29.6",

webpack.config.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
module.exports = (env) => {
2-
return require(`./webpack/${env}.js`)
3-
}
1+
// Imports: Dependencies
2+
const path = require("path")
3+
require("@babel/register")
4+
5+
module.exports = (env, argv) => {
6+
return require(`./webpack/${argv.mode}.js`)
7+
}

webpack/development.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
const merge = require("webpack-merge")
2-
const environment = require("./environment.js")
1+
import merge from "webpack-merge"
2+
import environment from "./environment"
3+
import { developmentPlugins } from "./plugins"
34

4-
module.exports = merge(environment, {})
5+
module.exports = merge(environment, {
6+
devtool: "source-map",
7+
plugins: developmentPlugins
8+
})

webpack/environment.js

+10-15
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
const HtmlWebPackPlugin = require("html-webpack-plugin")
2-
const WebpackModules = require("webpack-modules")
1+
import { environmentPlugins, optimizationPlugins } from "./plugins"
2+
import MiniCssExtractPlugin from "mini-css-extract-plugin"
3+
const devMode = process.env.NODE_ENV !== "production"
34

45
module.exports = {
56
module: {
67
rules: [
78
{
8-
test: /\.(scss)$/,
9+
test: /\.(sa|sc|c)ss$/,
910
use: [
10-
{ loader: "style-loader" },
11-
{ loader: "css-loader" },
12-
{ loader: "postcss-loader" },
13-
{ loader: "sass-loader" }
11+
{ loader: devMode ? "style-loader" : MiniCssExtractPlugin.loader, options: { sourceMap: devMode } },
12+
{ loader: "css-loader", options: { sourceMap: devMode } },
13+
{ loader: "postcss-loader", options: { sourceMap: devMode } },
14+
{ loader: "sass-loader", options: { sourceMap: devMode } }
1415
]
1516
},
1617
{
@@ -22,12 +23,6 @@ module.exports = {
2223
}
2324
]
2425
},
25-
plugins: [
26-
new WebpackModules(),
27-
new HtmlWebPackPlugin({
28-
inlineSource: ".(js|css)$",
29-
template: "./src/index.html",
30-
filename: "./index.html"
31-
})
32-
]
26+
optimization: optimizationPlugins,
27+
plugins: environmentPlugins
3328
}

webpack/plugins/compression-plugin.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Prepare compressed versions of assets to serve them with Content-Encoding
3+
* Docs: https://github.com/webpack-contrib/compression-webpack-plugin
4+
*/
5+
6+
const CompressionPlugin = require("compression-webpack-plugin")
7+
8+
const config = {
9+
filename: "[path].gz[query]",
10+
algorithm: "gzip",
11+
cache: true,
12+
test: /\.(html)$/// /\.(js|css|html|json|ico|svg|eot|otf|ttf|map)$/
13+
}
14+
15+
export default () => new CompressionPlugin(config)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Embed javascript and css source inline when using the webpack dev server or middleware
3+
* Docs: https://github.com/DustinJackson/html-webpack-inline-source-plugin
4+
*/
5+
6+
const HtmlWebpackInlineSourcePlugin = require("html-webpack-inline-source-plugin")
7+
8+
export default () => new HtmlWebpackInlineSourcePlugin()
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Plugin that simplifies creation of HTML files to serve your bundles
3+
* Docs: https://github.com/jantimon/html-webpack-plugin
4+
*/
5+
6+
import HtmlWebpackPlugin from "html-webpack-plugin"
7+
8+
const devMode = process.env.NODE_ENV !== "production"
9+
10+
const config = {
11+
inlineSource: ".(js|css)$",
12+
minify: true,
13+
inject: true,
14+
hash: devMode ? true : false
15+
}
16+
17+
export default () => new HtmlWebpackPlugin(config)

webpack/plugins/index.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import compressionPlugin from "./compression-plugin"
2+
import htmlWebpackInlineSourcePlugin from "./html-webpack-inline-source-plugin"
3+
import htmlWebpackPlugin from "./html-webpack-plugin"
4+
import miniCssExtractPlugin from "./mini-css-extract-plugin"
5+
import optimizeCSSAssetsPlugin from "./optimize-css-assets-webpack-plugin"
6+
import terserPlugin from "./terser-plugin"
7+
import webpackModulesPlugin from "./webpack-modules-plugin"
8+
9+
export const optimizationPlugins = {
10+
minimizer: [
11+
terserPlugin(),
12+
optimizeCSSAssetsPlugin()
13+
]
14+
}
15+
16+
export const environmentPlugins = [
17+
miniCssExtractPlugin(),
18+
webpackModulesPlugin(),
19+
htmlWebpackPlugin()
20+
]
21+
22+
export const devPlugins = []
23+
24+
export const productionPlugins = [
25+
htmlWebpackInlineSourcePlugin(),
26+
compressionPlugin()
27+
]
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* This plugin extracts CSS into separate files.
3+
* It creates a CSS file per JS file which contains CSS.
4+
* It supports On-Demand-Loading of CSS and SourceMaps.
5+
*
6+
* Docs: https://github.com/webpack-contrib/mini-css-extract-plugin
7+
*/
8+
9+
import MiniCssExtractPlugin from "mini-css-extract-plugin"
10+
const devMode = process.env.NODE_ENV !== "production"
11+
12+
export default () => new MiniCssExtractPlugin({})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* A Webpack plugin to optimize \ minimize CSS assets.
3+
* Docs: https://github.com/NMFR/optimize-css-assets-webpack-plugin
4+
*/
5+
6+
import OptimizeCSSAssetsPlugin from "optimize-css-assets-webpack-plugin"
7+
const safePostCssParser = require("postcss-safe-parser")
8+
9+
export default () =>
10+
new OptimizeCSSAssetsPlugin({
11+
parser: safePostCssParser,
12+
map: {
13+
inline: false,
14+
annotation: true
15+
}
16+
})

webpack/plugins/terser-plugin.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* This plugin is used to minify your JavaScript.
3+
* Docs: https://github.com/webpack-contrib/terser-webpack-plugin
4+
*/
5+
6+
import TerserPlugin from "terser-webpack-plugin"
7+
8+
const config = {
9+
parallel: true,
10+
cache: false,
11+
sourceMap: false,
12+
terserOptions: {
13+
parse: {
14+
// Let terser parse ecma 8 code but always output
15+
// ES5 compliant code for older browsers
16+
ecma: 8
17+
},
18+
compress: {
19+
ecma: 5,
20+
warnings: false,
21+
comparisons: false
22+
},
23+
mangle: {
24+
safari10: true
25+
},
26+
output: {
27+
ecma: 5,
28+
comments: false,
29+
ascii_only: true
30+
}
31+
}
32+
}
33+
34+
export default () => new TerserPlugin(config)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* This plugin handles `.mjs` files correctly within webpack
3+
* Docs: https://github.com/lukeed/webpack-modules
4+
*/
5+
6+
import WebpackModules from "webpack-modules"
7+
8+
export default () => new WebpackModules()

webpack/production.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
const HtmlWebpackInlineSourcePlugin = require("html-webpack-inline-source-plugin")
2-
const merge = require("webpack-merge")
3-
const environment = require("./environment.js")
1+
import merge from "webpack-merge"
2+
import environment from "./environment"
3+
import { productionPlugins } from "./plugins"
44

55
module.exports = merge(environment, {
6-
plugins: [
7-
new HtmlWebpackInlineSourcePlugin()
8-
]
6+
plugins: productionPlugins
97
})

0 commit comments

Comments
 (0)