Created
December 26, 2018 11:32
-
-
Save jez500/4f4295778e386ddbc96c2377e3415fc3 to your computer and use it in GitHub Desktop.
Simple Webpack 4 Compile SCSS to CSS config with autoprefix and minify
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
{ | |
"name": "my-project", | |
"version": "1.0.0", | |
"description": "Simple SCSS complie with autoprefix and minify", | |
"scripts": { | |
"build": "webpack" | |
}, | |
"devDependencies": { | |
"autoprefixer": "^9.0.0", | |
"breakpoint-sass": "^2.7.1", | |
"css-loader": "^2.0.0", | |
"cssnano": "^4.0.5", | |
"node-sass": "^4.9.0", | |
"mini-css-extract-plugin": "^0.5.0", | |
"postcss-loader": "^3.0.0", | |
"sass-loader": "^7.0.1", | |
"style-loader": "^0.23.1", | |
"webpack": "^4.6.0", | |
"webpack-cli": "^3.0.0" | |
} | |
} |
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 autoprefixer = require('autoprefixer'); | |
const cssnano = require('cssnano'); | |
const MiniCssExtractPlugin = require("mini-css-extract-plugin"); | |
// Hard code this to production but can be adapted to accept args to change env. | |
const mode = 'production'; | |
module.exports = { | |
mode, | |
output: { | |
// Webpack will create js files even though they are not used | |
filename: '[name].bundle.js', | |
chunkFilename: '[name].[chunkhash].chunk.js', | |
// Where the CSS is saved to | |
path: path.resolve(__dirname, 'css'), | |
publicPath: "/css" | |
}, | |
resolve: { | |
extensions: ['.css', '.scss'], | |
alias: { | |
// Provides ability to include node_modules with ~ | |
'~': path.resolve(process.cwd(), 'src'), | |
}, | |
}, | |
entry: { | |
// Will create "styles.css" in "css" dir. | |
"styles": './scss/my-styles.scss', | |
}, | |
module: { | |
rules: [ | |
{ | |
test: /\.scss$/, | |
use: [ | |
// Extract and save the final CSS. | |
MiniCssExtractPlugin.loader, | |
// Load the CSS, set url = false to prevent following urls to fonts and images. | |
{ loader: "css-loader", options: { url: false, importLoaders: 1 } }, | |
// Add browser prefixes and minify CSS. | |
{ loader: 'postcss-loader', options: { plugins: [autoprefixer(), cssnano()] }}, | |
// Load the SCSS/SASS | |
{ loader: 'sass-loader' }, | |
], | |
}, | |
], | |
}, | |
plugins: [ | |
// Define the filename pattern for CSS. | |
new MiniCssExtractPlugin({ | |
filename: '[name].css', | |
chunkFilename: '[id].css', | |
}) | |
] | |
} |
๐
Great.
Thanks, this helped me! it just works!
If someone runs into an issue with the postcss-loader in a new version of webpack - this is the fix:
Replace Line 44 with:
options: { postcssOptions: { plugins: [autoprefixer(), cssnano()], }, },
Thanks! ATM works fine ๐
This has saved me thank you! However, it appears to generate a javascript file for each scss file you process, any way to stop that?
Tnx a lot sir <3
Thanks, this helps a lot :)
Nice. Thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run above with
npm run build