-
-
Save hcivelek/5542172d5102541a5b1189521dfd3b98 to your computer and use it in GitHub Desktop.
Simple Webpack 4 Compile SCSS to CSS config with autoprefix and minify
This file contains 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 and JS SCSS complie with autoprefix and minify", | |
"scripts": { | |
"build": "webpack", | |
"watch": "webpack --watch" | |
}, | |
"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" | |
}, | |
"dependencies": { | |
} | |
} |
This file contains 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 = 'development'; | |
module.exports = { | |
mode, | |
watch:true, | |
output: { | |
// Webpack will create js files even though they are not used | |
filename: '../js/app.js', | |
chunkFilename: '[name].[chunkhash].chunk.js', | |
// Where the CSS is saved to | |
path: path.resolve(__dirname, '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. | |
'./src/index.js', | |
'./src/app.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: 'app.css', | |
chunkFilename: '[id].css', | |
}) | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment