From 06a053fb108746bfdd5bd3600929621a70ab3ee9 Mon Sep 17 00:00:00 2001 From: Tristan Maat Date: Sun, 8 Apr 2018 21:35:54 +0000 Subject: [PATCH] Extract CSS from webpack js --- package.json | 2 ++ webpack.common.js | 6 ++++-- webpack.prod.js | 12 ++++++++++++ yarn.lock | 23 ++++++++++++++++++++++- 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 89afbd8..e67a57d 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,9 @@ "html-loader": "^0.5.5", "html-webpack-plugin": "^3.1.0", "jstransformer-markdown-it": "^2.0.0", + "mini-css-extract-plugin": "^0.4.0", "node-sass": "^4.8.3", + "optimize-css-assets-webpack-plugin": "^4.0.0", "postcss-loader": "^2.1.3", "precss": "^3.1.2", "pug": "^2.0.3", diff --git a/webpack.common.js b/webpack.common.js index c986c58..1888614 100644 --- a/webpack.common.js +++ b/webpack.common.js @@ -3,6 +3,7 @@ const path = require("path"); const webpack = require("webpack"); const HtmlWebpackPlugin = require("html-webpack-plugin"); const CleanWebpackPlugin = require("clean-webpack-plugin"); +const MiniCssExtractPlugin = require("mini-css-extract-plugin"); const PAGE_DIR = path.resolve(__dirname, "src", "pages"); @@ -28,7 +29,8 @@ module.exports = { new webpack.ProvidePlugin({ $: "jquery", jQuery: "jquery" - }) + }), + new MiniCssExtractPlugin() ].concat(pages.map(page => new HtmlWebpackPlugin(page))), module: { rules: [ @@ -45,7 +47,7 @@ module.exports = { { test: /.scss$/, use: [{ - loader: "style-loader" + loader: MiniCssExtractPlugin.loader }, { loader: "css-loader" }, { diff --git a/webpack.prod.js b/webpack.prod.js index 3e1b320..bc8cf76 100644 --- a/webpack.prod.js +++ b/webpack.prod.js @@ -1,7 +1,19 @@ const merge = require("webpack-merge"); const common = require("./webpack.common.js"); +const UglifyJsPlugin = require("uglifyjs-webpack-plugin"); +const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin"); module.exports = merge(common, { + optimization: { + minimizer: [ + new UglifyJsPlugin({ + cache: true, + parallel: true, + sourceMap: true + }), + new OptimizeCSSAssetsPlugin({}) + ] + }, devtool: "source-map", mode: "production" }); diff --git a/yarn.lock b/yarn.lock index feedcc2..a8772b3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1761,7 +1761,7 @@ cssesc@^0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/cssesc/-/cssesc-0.1.0.tgz#c814903e45623371a0477b40109aaafbeeaddbb4" -cssnano@^3.10.0: +cssnano@^3.10.0, cssnano@^3.4.0: version "3.10.0" resolved "https://registry.yarnpkg.com/cssnano/-/cssnano-3.10.0.tgz#4f38f6cea2b9b17fa01490f23f1dc68ea65c1c38" dependencies: @@ -3499,6 +3499,13 @@ kind-of@^6.0.0, kind-of@^6.0.2: version "6.0.2" resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-6.0.2.tgz#01146b36a6218e64e58f3a8d66de5d7fc6f6d051" +last-call-webpack-plugin@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/last-call-webpack-plugin/-/last-call-webpack-plugin-3.0.0.tgz#9742df0e10e3cf46e5c0381c2de90d3a7a2d7555" + dependencies: + lodash "^4.17.5" + webpack-sources "^1.1.0" + lazy-cache@^1.0.3: version "1.0.4" resolved "https://registry.yarnpkg.com/lazy-cache/-/lazy-cache-1.0.4.tgz#a1d78fc3a50474cb80845d3b3b6e1da49a446e8e" @@ -3875,6 +3882,13 @@ mimic-response@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/mimic-response/-/mimic-response-1.0.0.tgz#df3d3652a73fded6b9b0b24146e6fd052353458e" +mini-css-extract-plugin@^0.4.0: + version "0.4.0" + resolved "https://registry.yarnpkg.com/mini-css-extract-plugin/-/mini-css-extract-plugin-0.4.0.tgz#ff3bf08bee96e618e177c16ca6131bfecef707f9" + dependencies: + loader-utils "^1.1.0" + webpack-sources "^1.1.0" + minimalistic-assert@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/minimalistic-assert/-/minimalistic-assert-1.0.0.tgz#702be2dda6b37f4836bcb3f5db56641b64a1d3d3" @@ -4236,6 +4250,13 @@ onetime@^2.0.0: dependencies: mimic-fn "^1.0.0" +optimize-css-assets-webpack-plugin@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/optimize-css-assets-webpack-plugin/-/optimize-css-assets-webpack-plugin-4.0.0.tgz#d5f80041fb1391b358a1f35273c3b53de814e8fe" + dependencies: + cssnano "^3.4.0" + last-call-webpack-plugin "^3.0.0" + ora@^0.2.3: version "0.2.3" resolved "https://registry.yarnpkg.com/ora/-/ora-0.2.3.tgz#37527d220adcd53c39b73571d754156d5db657a4"