Skip to content

Commit a9d844b

Browse files
committed
add decorator example
1 parent f966fce commit a9d844b

File tree

8 files changed

+64
-2
lines changed

8 files changed

+64
-2
lines changed

Diff for: README.md

+5
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,10 @@
1313
- add `./webpack-helpers/pluginsPatch.js` and use it in `webpack.monkey.js`
1414
- build project: `yarn build`
1515
- check `build/stats.html` file
16+
- add decorator support
17+
- add package `yarn add babel-plugin-transform-decorators-legacy`
18+
- and `webpack-helpers/utils.js` (add `findRule` function)
19+
- add `webpack-helpers/babelPatch.js` and use it in `webpack.monkey.js`
20+
- add and use sample decorator
1621

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

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6+
"babel-plugin-transform-decorators-legacy": "^1.3.4",
67
"monkey-react-scripts": "^0.1.0",
78
"react": "^16.1.1",
89
"react-dom": "^16.1.1",

Diff for: src/App.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { Component } from 'react';
22
import logo from './logo.svg';
33
import './App.css';
4+
import {Sample} from "./SampleDecorator";
45

56
class App extends Component {
67
render() {
@@ -13,6 +14,7 @@ class App extends Component {
1314
<p className="App-intro">
1415
To get started, edit <code>src/App.js</code> and save to reload.
1516
</p>
17+
<Sample/>
1618
</div>
1719
);
1820
}

Diff for: src/SampleDecorator.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React , {Component} from 'react'
2+
3+
function withRedBorder(Cmp) {
4+
class NewCmp extends Component {
5+
render () {
6+
return (
7+
<div style={{border: '1px solid red', padding: 8}}>
8+
<Cmp {...this.props}/>
9+
</div>
10+
)
11+
}
12+
}
13+
14+
return NewCmp
15+
}
16+
17+
@withRedBorder
18+
export class Sample extends Component {
19+
render () {
20+
return (<div>Sample With Decorator</div>)
21+
}
22+
}
23+
24+
Sample.propTypes = {}

Diff for: webpack-helpers/babelPatch.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const {findRule} = require('./utils');
2+
3+
module.exports.babelPatch = function babelPatch(webpackConfig, isDevelopment) {
4+
// find babel rule
5+
const babelRule = findRule(webpackConfig, (rule) => {
6+
return ('' + rule.test === '' + /\.(js|jsx|mjs)$/)
7+
});
8+
const plugins = babelRule.options.plugins || [];
9+
babelRule.options.plugins = [...plugins, 'transform-decorators-legacy']
10+
};

Diff for: webpack-helpers/utils.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports.findRule = function findRule(webpackConfig, callback) {
2+
const rules = webpackConfig.module.rules[1].oneOf;
3+
const index = rules.findIndex(callback);
4+
if (index === -1) throw Error('Loader not found');
5+
return rules[index]
6+
};

Diff for: webpack.monkey.js

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

34
module.exports = function (webpackConfig, isDevelopment) {
45
pluginsPatch(webpackConfig, isDevelopment)
6+
babelPatch(webpackConfig, isDevelopment)
57
};

Diff for: yarn.lock

+14-2
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,10 @@ babel-plugin-syntax-class-properties@^6.8.0:
557557
version "6.13.0"
558558
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-class-properties/-/babel-plugin-syntax-class-properties-6.13.0.tgz#d7eb23b79a317f8543962c505b827c7d6cac27de"
559559

560+
babel-plugin-syntax-decorators@^6.1.18:
561+
version "6.13.0"
562+
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-decorators/-/babel-plugin-syntax-decorators-6.13.0.tgz#312563b4dbde3cc806cee3e416cceeaddd11ac0b"
563+
560564
[email protected], babel-plugin-syntax-dynamic-import@^6.18.0:
561565
version "6.18.0"
562566
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-dynamic-import/-/babel-plugin-syntax-dynamic-import-6.18.0.tgz#8d6a26229c83745a9982a441051572caa179b1da"
@@ -598,6 +602,14 @@ [email protected]:
598602
babel-runtime "^6.22.0"
599603
babel-template "^6.24.1"
600604

605+
babel-plugin-transform-decorators-legacy@^1.3.4:
606+
version "1.3.4"
607+
resolved "https://registry.yarnpkg.com/babel-plugin-transform-decorators-legacy/-/babel-plugin-transform-decorators-legacy-1.3.4.tgz#741b58f6c5bce9e6027e0882d9c994f04f366925"
608+
dependencies:
609+
babel-plugin-syntax-decorators "^6.1.18"
610+
babel-runtime "^6.2.0"
611+
babel-template "^6.3.0"
612+
601613
babel-plugin-transform-es2015-arrow-functions@^6.22.0:
602614
version "6.22.0"
603615
resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-arrow-functions/-/babel-plugin-transform-es2015-arrow-functions-6.22.0.tgz#452692cb711d5f79dc7f85e440ce41b9f244d221"
@@ -928,14 +940,14 @@ babel-register@^6.26.0:
928940
mkdirp "^0.5.1"
929941
source-map-support "^0.4.15"
930942

931-
[email protected], babel-runtime@^6.18.0, babel-runtime@^6.22.0, babel-runtime@^6.26.0:
943+
[email protected], babel-runtime@^6.18.0, babel-runtime@^6.2.0, babel-runtime@^6.22.0, babel-runtime@^6.26.0:
932944
version "6.26.0"
933945
resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.26.0.tgz#965c7058668e82b55d7bfe04ff2337bc8b5647fe"
934946
dependencies:
935947
core-js "^2.4.0"
936948
regenerator-runtime "^0.11.0"
937949

938-
babel-template@^6.16.0, babel-template@^6.24.1, babel-template@^6.26.0:
950+
babel-template@^6.16.0, babel-template@^6.24.1, babel-template@^6.26.0, babel-template@^6.3.0:
939951
version "6.26.0"
940952
resolved "https://registry.yarnpkg.com/babel-template/-/babel-template-6.26.0.tgz#de03e2d16396b069f46dd9fff8521fb1a0e35e02"
941953
dependencies:

0 commit comments

Comments
 (0)