Skip to content

Instantly share code, notes, and snippets.

@jez500
Created December 26, 2018 11:32
Show Gist options
  • Save jez500/4f4295778e386ddbc96c2377e3415fc3 to your computer and use it in GitHub Desktop.
Save jez500/4f4295778e386ddbc96c2377e3415fc3 to your computer and use it in GitHub Desktop.
Simple Webpack 4 Compile SCSS to CSS config with autoprefix and minify
{
"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"
}
}
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',
})
]
}
@gaborszita
Copy link

Thanks, this helps a lot :)

@everaldomatias
Copy link

Nice. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment