CSS Preprocessor
SASS
sass/sass-simple
├── index.html
├── js
│ └── app.js
└── scss
└── main.scss
sass/sass-simple/scss/main.scss:
body {
background-color: green;
p {
color: red;
font-size: 40px;
display: flex;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="dist/css/bundle.css">
</head>
<body>
<script src="dist/js/bundle.js"></script>
</body>
</html>
sass/sass-simple/package.json:
$ npm init -y
$ npm i webpack webpack-cli @webpack-cli/init -D
{
"name": "sass-simple",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build-dev": "npx webpack --config webpack.dev.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@webpack-cli/init": "^0.1.0",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-preset-env": "^1.7.0",
"css-loader": "^1.0.0",
"extract-loader": "^2.0.1",
"file-loader": "^1.1.11",
"node-sass": "^4.9.2",
"sass-loader": "^7.0.3",
"uglifyjs-webpack-plugin": "^1.2.7",
"webpack": "^4.16.2",
"webpack-cli": "^3.1.0"
}
}
sass/sass-simple/webpack.dev.js:
$ npx webpack init
? Will your application have multiple bundles? No
? Which module will be the first to enter the application? [default: ./src/index] ./js/app.js
? Which folder will your generated bundles be in? [default: dist]:
? Will you be using ES2015? Yes
? Will you use one of the below CSS solutions? SASS
...
Congratulations! Your new webpack configuration file has been created!
$ npm i extract-loader file-loader -D
$ npm r style-loader -D
const webpack = require('webpack')
const path = require('path')
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
module.exports = {
module: {
rules: [{
test: /\.js$/,
include: [path.resolve(__dirname, 'js')],
loader: 'babel-loader',
options: {
plugins: ['syntax-dynamic-import'],
presets: [['env', {
'modules': false
}]]
}
}, {
test: /\.(scss|css)$/,
use: [{
loader: 'file-loader',
options: {
name: 'bundle.css',
outputPath: 'css/'
}
}, {
loader: 'extract-loader'
}, {
loader: 'css-loader'
}, {
loader: 'sass-loader'
}]
}]
},
entry: ['./js/app.js', './scss/main.scss'],
output: {
filename: 'js/bundle.js',
path: path.resolve(__dirname, 'dist')
},
mode: 'development',
optimization: {
splitChunks: {
cacheGroups: {
vendors: {
priority: -10,
test: /[\\/]node_modules[\\/]/
}
},
chunks: 'async',
minChunks: 1,
minSize: 30000,
name: true
}
}
}
$ npx webpack --config webpack.dev.js
$ npm run build-dev
PostCSS
postcss/sass-simple
├── index.html
├── js
│ └── app.js
└── scss
└── main.scss
postcss/postcss-simple/scss/main.scss:
body {
background-color: green;
p {
color: red;
font-size: 40px;
display: flex;
}
}
postcss/postcss-simple/index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="dist/css/bundle.css">
</head>
<body>
<script src="dist/js/bundle.js"></script>
</body>
</html>
postcss/postcss-simple/package.json:
$ npm init -y
$ npm i webpack webpack-cli @webpack-cli/init -D
{
"name": "postcss-simple",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build-dev": "npx webpack --config webpack.dev.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@webpack-cli/init": "^0.1.0",
"autoprefixer": "^9.0.1",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-preset-env": "^1.7.0",
"css-loader": "^1.0.0",
"cssnano": "^4.0.3",
"extract-loader": "^2.0.1",
"file-loader": "^1.1.11",
"node-sass": "^4.9.2",
"postcss-loader": "^2.1.6",
"sass-loader": "^7.0.3",
"uglifyjs-webpack-plugin": "^1.2.7",
"webpack": "^4.16.2",
"webpack-cli": "^3.1.0"
}
}
postcss/postcss-simple/webpack.dev.js:
$ npx webpack init
? Will your application have multiple bundles? No
? Which module will be the first to enter the application? [default: ./src/index] ./js/app.js
? Which folder will your generated bundles be in? [default: dist]:
? Will you be using ES2015? Yes
? Will you use one of the below CSS solutions? SASS
...
Congratulations! Your new webpack configuration file has been created!
$ npm i extract-loader file-loader postcss-loader cssnano autoprefixer -D
$ npm r style-loader -D
const webpack = require('webpack')
const path = require('path')
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
module.exports = {
module: {
rules: [{
test: /\.js$/,
include: [path.resolve(__dirname, 'js')],
loader: 'babel-loader',
options: {
plugins: ['syntax-dynamic-import'],
presets: [['env', {
'modules': false
}]]
}
}, {
test: /\.(scss|css)$/,
use: [{
loader: 'file-loader',
options: {
name: 'bundle.css',
outputPath: 'css/'
}
}, {
loader: 'extract-loader'
}, {
loader: 'css-loader'
}, {
loader: 'postcss-loader'
}, {
loader: 'sass-loader'
}]
}]
},
entry: ['./js/app.js', './scss/main.scss'],
output: {
filename: 'js/bundle.js',
path: path.resolve(__dirname, 'dist')
},
mode: 'development',
optimization: {
splitChunks: {
cacheGroups: {
vendors: {
priority: -10,
test: /[\\/]node_modules[\\/]/
}
},
chunks: 'async',
minChunks: 1,
minSize: 30000,
name: true
}
},
plugins: [
new UglifyJSPlugin()
]
}
postcss/postcss-simple/postcss.config.js:
module.exports = {
plugins: [
require('autoprefixer')({ browsers: ['last 3 versions', 'android >= 4.4', 'ie>7'] }),
require('cssnano')
]
}
$ npx webpack --config webpack.dev.js
$ npm run build-dev
postcss/postcss-simple/dist/css/bundle.css:
body{background-color:green}body p{color:red;display:-webkit-box;display:-ms-flexbox;display:flex;font-size:40px}