Skip to content

Commit

Permalink
migration
Browse files Browse the repository at this point in the history
  • Loading branch information
rickyleung committed Aug 23, 2019
1 parent 5f3dcbd commit 027521a
Show file tree
Hide file tree
Showing 60 changed files with 1,033 additions and 0 deletions.
Binary file added mindmap/实战篇.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions sourcecode/project-migration/stage-0/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"presets": [
"@babel/env",
"@babel/preset-react"
],
"plugins": [
"@babel/proposal-class-properties",
"@babel/proposal-object-rest-spread"
]
}
2 changes: 2 additions & 0 deletions sourcecode/project-migration/stage-0/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
4 changes: 4 additions & 0 deletions sourcecode/project-migration/stage-0/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
实战篇 渐进式迁移策略
===============

JavaScript 原始工程
34 changes: 34 additions & 0 deletions sourcecode/project-migration/stage-0/build/webpack.base.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
entry: {
'app': './src/index.jsx'
},
output: {
filename: '[name].[chunkhash:8].js'
},
resolve: {
extensions: ['.js', '.jsx']
},
module: {
rules: [
{
test: /\.jsx?$/,
use: [{
loader: 'babel-loader'
}],
exclude: /node_modules/
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/tpl/index.html'
})
],
optimization: {
splitChunks: {
chunks: 'all'
}
}
}
9 changes: 9 additions & 0 deletions sourcecode/project-migration/stage-0/build/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const merge = require('webpack-merge')
const baseConfig = require('./webpack.base.config')
const devConfig = require('./webpack.dev.config')
const proConfig = require('./webpack.pro.config')

module.exports = (env, argv) => {
let config = argv.mode === 'development' ? devConfig : proConfig;
return merge(baseConfig, config);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
devtool: 'cheap-module-eval-source-map',
devServer: {
port: 8080
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { CleanWebpackPlugin } = require('clean-webpack-plugin')

module.exports = {
plugins: [
new CleanWebpackPlugin()
]
}
33 changes: 33 additions & 0 deletions sourcecode/project-migration/stage-0/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "project-migration",
"version": "1.0.0",
"description": "",
"main": "./src/index.jsx",
"scripts": {
"start": "webpack-dev-server --mode=development --config ./build/webpack.config.js",
"build": "webpack --mode=production --config ./build/webpack.config.js"
},
"keywords": [
"TypeScript"
],
"author": "liangxiao",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.5.5",
"@babel/plugin-proposal-class-properties": "^7.5.5",
"@babel/plugin-proposal-object-rest-spread": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.6",
"clean-webpack-plugin": "^3.0.0",
"html-webpack-plugin": "^3.2.0",
"webpack": "^4.39.2",
"webpack-cli": "^3.3.2",
"webpack-dev-server": "^3.5.1",
"webpack-merge": "^4.2.1"
},
"dependencies": {
"react": "^16.9.0",
"react-dom": "^16.9.0"
}
}
29 changes: 29 additions & 0 deletions sourcecode/project-migration/stage-0/src/components/Hello.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';

class Hello extends Component {
state = {
count: 0
}
static defaultProps = {
firstName: '',
lastName: ''
}
render() {
return (
<>
<p>你点击了 {this.state.count}</p>
<button onClick={() => {this.setState({count: this.state.count + 1})}}>
Hello {this.props.name}
</button>
</>
)
}
}
Hello.propTypes = {
name: PropTypes.string.isRequired,
firstName: PropTypes.string,
lastName: PropTypes.string
}

export default Hello;
15 changes: 15 additions & 0 deletions sourcecode/project-migration/stage-0/src/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';
import ReactDOM from 'react-dom';

import Hello from './components/Hello';

import { add } from './utils/a';

add(1, 1)

ReactDOM.render(
<div>
<Hello name="World" />
</div>,
document.querySelectorAll('.app')[0]
);
12 changes: 12 additions & 0 deletions sourcecode/project-migration/stage-0/src/tpl/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!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>Project Migration</title>
</head>
<body>
<div class="app"></div>
</body>
</html>
3 changes: 3 additions & 0 deletions sourcecode/project-migration/stage-0/src/utils/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function add(x, y) {
return x + y
}
11 changes: 11 additions & 0 deletions sourcecode/project-migration/stage-1/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"presets": [
"@babel/env",
"@babel/preset-react",
"@babel/preset-typescript"
],
"plugins": [
"@babel/proposal-class-properties",
"@babel/proposal-object-rest-spread"
]
}
2 changes: 2 additions & 0 deletions sourcecode/project-migration/stage-1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
4 changes: 4 additions & 0 deletions sourcecode/project-migration/stage-1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
实战篇 渐进式迁移策略
===============

JavaScript + TypeScript 共存策略
34 changes: 34 additions & 0 deletions sourcecode/project-migration/stage-1/build/webpack.base.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
entry: {
'app': './src/index.jsx'
},
output: {
filename: '[name].[chunkhash:8].js'
},
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx']
},
module: {
rules: [
{
test: /\.(j|t)sx?$/,
use: [{
loader: 'babel-loader'
}],
exclude: /node_modules/
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/tpl/index.html'
})
],
optimization: {
splitChunks: {
chunks: 'all'
}
}
}
9 changes: 9 additions & 0 deletions sourcecode/project-migration/stage-1/build/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const merge = require('webpack-merge')
const baseConfig = require('./webpack.base.config')
const devConfig = require('./webpack.dev.config')
const proConfig = require('./webpack.pro.config')

module.exports = (env, argv) => {
let config = argv.mode === 'development' ? devConfig : proConfig;
return merge(baseConfig, config);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
devtool: 'cheap-module-eval-source-map',
devServer: {
port: 8080
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { CleanWebpackPlugin } = require('clean-webpack-plugin')

module.exports = {
plugins: [
new CleanWebpackPlugin()
]
}
39 changes: 39 additions & 0 deletions sourcecode/project-migration/stage-1/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "project-migration",
"version": "1.0.0",
"description": "",
"main": "./src/index.jsx",
"scripts": {
"start": "webpack-dev-server --mode=development --config ./build/webpack.config.js",
"build": "webpack --mode=production --config ./build/webpack.config.js",
"type-check": "tsc --watch"
},
"keywords": [
"TypeScript"
],
"author": "liangxiao",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.5.5",
"@babel/plugin-proposal-class-properties": "^7.5.5",
"@babel/plugin-proposal-object-rest-spread": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@babel/preset-react": "^7.0.0",
"@babel/preset-typescript": "^7.3.3",
"@types/react": "^16.9.2",
"@types/react-dom": "^16.8.5",
"babel-loader": "^8.0.6",
"clean-webpack-plugin": "^3.0.0",
"html-webpack-plugin": "^3.2.0",
"ts-loader": "^6.0.4",
"typescript": "^3.5.3",
"webpack": "^4.39.2",
"webpack-cli": "^3.3.2",
"webpack-dev-server": "^3.5.1",
"webpack-merge": "^4.2.1"
},
"dependencies": {
"react": "^16.9.0",
"react-dom": "^16.9.0"
}
}
29 changes: 29 additions & 0 deletions sourcecode/project-migration/stage-1/src/components/Hello.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';

class Hello extends Component {
state = {
count: 0
}
static defaultProps = {
firstName: '',
lastName: ''
}
render() {
return (
<>
<p>你点击了 {this.state.count}</p>
<button onClick={() => {this.setState({count: this.state.count + 1})}}>
Hello {this.props.name}
</button>
</>
)
}
}
Hello.propTypes = {
name: PropTypes.string.isRequired,
firstName: PropTypes.string,
lastName: PropTypes.string
}

export default Hello;
32 changes: 32 additions & 0 deletions sourcecode/project-migration/stage-1/src/components/Hi.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React, { Component } from 'react';

interface Props {
name: string;
firstName?: string;
lastName?: string;
}
interface State {
count: number
}

class Hi extends Component<Props, State> {
state = {
count: 0
}
static defaultProps = {
firstName: '',
lastName: ''
}
render() {
return (
<>
<p>你点击了 {this.state.count}</p>
<button onClick={() => {this.setState({count: this.state.count + 1})}}>
Hi {this.props.name}
</button>
</>
)
}
}

export default Hi;
19 changes: 19 additions & 0 deletions sourcecode/project-migration/stage-1/src/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';
import ReactDOM from 'react-dom';

import Hello from './components/Hello';
import Hi from './components/Hi';

import { add } from './utils/a';
import { sub } from './utils/b';

add(1, 1)
sub(1, 1)

ReactDOM.render(
<div>
<Hello name="World" />
<Hi name="World" />
</div>,
document.querySelectorAll('.app')[0]
);
Loading

0 comments on commit 027521a

Please sign in to comment.