Skip to content

Commit cbdf2d5

Browse files
committed
add sass support
1 parent a9d844b commit cbdf2d5

File tree

8 files changed

+353
-36
lines changed

8 files changed

+353
-36
lines changed

Diff for: README.md

+5
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,10 @@
1818
- and `webpack-helpers/utils.js` (add `findRule` function)
1919
- add `webpack-helpers/babelPatch.js` and use it in `webpack.monkey.js`
2020
- add and use sample decorator
21+
- add sass support
22+
- add package `yarn add node-sass sass-loader`
23+
- edit `webpack-helpers/utils.js` (add `addRule` function)
24+
- add `webpack-helpers/sassPatch.js` and use it in `webpack.monkey.js`
25+
- add and use sample sass file
2126

2227
[monkey-react-scripts]: https://github.com/monkey-patches/monkey-react-scripts

Diff for: package.json

+2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
"dependencies": {
66
"babel-plugin-transform-decorators-legacy": "^1.3.4",
77
"monkey-react-scripts": "^0.1.0",
8+
"node-sass": "^4.7.2",
89
"react": "^16.1.1",
910
"react-dom": "^16.1.1",
1011
"react-scripts": "1.0.17",
12+
"sass-loader": "^6.0.6",
1113
"webpack-visualizer-plugin": "^0.1.11"
1214
},
1315
"scripts": {

Diff for: src/SampleDecorator.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import React , {Component} from 'react'
2+
import './sample.scss'
23

34
function withRedBorder(Cmp) {
45
class NewCmp extends Component {
56
render () {
67
return (
7-
<div style={{border: '1px solid red', padding: 8}}>
8+
<div style={{border: '1px solid red', padding: 8}} className="red-bg">
89
<Cmp {...this.props}/>
910
</div>
1011
)

Diff for: src/sample.scss

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$color: lighten(red, 30%);
2+
3+
.red-bg {
4+
background-color: $color;
5+
}

Diff for: webpack-helpers/sassPatch.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const {addRule, findRule} = require('./utils')
2+
3+
module.exports.sassPatch = function sassPatch (webpackConfig, isDevelopment) {
4+
// find css rule
5+
const cssRule = findRule(webpackConfig, (rule) => {
6+
return ('' + rule.test === '' + /\.css$/)
7+
})
8+
const cssLoaders = isDevelopment ? cssRule.use : cssRule.loader
9+
10+
const postCssLoader = cssLoaders[cssLoaders.length - 1]
11+
postCssLoader.options.sourceMap = true // add source option to postcss
12+
13+
// add sass support
14+
const sassLoader = {loader: require.resolve('sass-loader'), options: {sourceMap: true}}
15+
const sassLoaders = cssLoaders.concat(sassLoader)
16+
17+
const sassRule = {
18+
test: /\.scss$/,
19+
[isDevelopment ? 'use' : 'loader']: sassLoaders,
20+
}
21+
22+
addRule(webpackConfig, sassRule)
23+
24+
}

Diff for: webpack-helpers/utils.js

+5
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@ module.exports.findRule = function findRule(webpackConfig, callback) {
44
if (index === -1) throw Error('Loader not found');
55
return rules[index]
66
};
7+
8+
module.exports.addRule = function addRule (webpackConfig, rule) {
9+
const rules = webpackConfig.module.rules[1].oneOf
10+
rules.splice(rules.length - 1, 0, rule) // add before exclude rule
11+
}

Diff for: webpack.monkey.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const {pluginsPatch} = require('./webpack-helpers/pluginsPatch');
22
const {babelPatch} = require('./webpack-helpers/babelPatch');
3+
const {sassPatch} = require('./webpack-helpers/sassPatch');
34

45
module.exports = function (webpackConfig, isDevelopment) {
56
pluginsPatch(webpackConfig, isDevelopment)
67
babelPatch(webpackConfig, isDevelopment)
8+
sassPatch(webpackConfig, isDevelopment)
79
};

0 commit comments

Comments
 (0)