├── .editorconfig
├── .eslintrc
├── .gitignore
├── README.md
├── config
├── dev.js
├── index.js
└── prod.js
├── package.json
├── project.config.json
├── src
├── app.js
├── app.scss
├── assets
│ └── img
│ │ └── xcx.jpg
├── index.html
├── pages
│ ├── index
│ │ ├── index.js
│ │ └── index.scss
│ └── page2
│ │ ├── page2.js
│ │ └── page2.scss
├── redux
│ ├── actions
│ │ └── index.js
│ ├── reducers
│ │ ├── common
│ │ │ ├── common.js
│ │ │ └── initState.js
│ │ └── index.js
│ └── store.js
├── servers
│ ├── baseUrl.js
│ ├── config.js
│ ├── http.js
│ ├── interceptors.js
│ └── servers.js
└── utils
│ └── common.js
├── yarn-error.log
└── yarn.lock
/.editorconfig:
--------------------------------------------------------------------------------
1 | # http://editorconfig.org
2 | root = true
3 |
4 | [*]
5 | indent_style = space
6 | indent_size = 2
7 | charset = utf-8
8 | trim_trailing_whitespace = true
9 | insert_final_newline = true
10 |
11 | [*.md]
12 | trim_trailing_whitespace = false
13 |
--------------------------------------------------------------------------------
/.eslintrc:
--------------------------------------------------------------------------------
1 | {
2 | "extends": ["taro"],
3 | "rules": {
4 | "no-unused-vars": ["error", { "varsIgnorePattern": "Taro" }],
5 | "react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx", ".tsx"] }]
6 | },
7 | "parser": "babel-eslint"
8 | }
9 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | dist/
2 | .temp/
3 | .rn_temp/
4 | node_modules/
5 | .DS_Store
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## 前言
2 |
3 | [Taro](https://taro.aotu.io/) 是一套遵循 [React](https://reactjs.org/) 语法规范的 **多端开发** 解决方案。现如今市面上端的形态多种多样,Web、React-Native、微信小程序等各种端大行其道,当业务要求同时在不同的端都要求有所表现的时候,针对不同的端去编写多套代码的成本显然非常高,这时候只编写一套代码就能够适配到多端的能力就显得极为需要。
4 |
5 | 使用 [Taro](https://taro.aotu.io/),我们可以只书写一套代码,再通过 [Taro](https://taro.aotu.io/) 的编译工具,将源代码分别编译出可以在不同端(微信/百度/支付宝/字节跳动小程序、H5、React-Native 等)运行的代码。
6 |
7 | 该项目基于[Taro](https://taro.aotu.io/),构建了一个demo。
8 |
9 | ## 项目运行
10 |
11 | ```JS
12 |
13 | git clone git@github.com:TigerHee/taro-init.git
14 |
15 | cd taro-init
16 |
17 | // 全局安装taro脚手架
18 | npm install -g @tarojs/cli
19 | 或
20 | yarn global add @tarojs/cli
21 |
22 | // 安装项目依赖
23 | npm install
24 | 或
25 | yarn
26 |
27 |
28 | // 微信小程序
29 | taro build --type weapp --watch
30 |
31 | // 支付宝小程序
32 | taro build --type alipay --watch
33 |
34 | // 百度小程序
35 | taro build --type swan --watch
36 |
37 | // 字节跳动小程序
38 | taro build --type tt --watch
39 |
40 | // H5
41 | taro build --type h5 --watch
42 |
43 | // React Native
44 | taro build --type rn --watch
45 |
46 | // (去掉 --watch 将不会监听文件修改,并会对代码进行压缩打包)
47 |
48 | ```
49 | ## 项目版本升级
50 |
51 | ```js
52 | // 1. 更新 Taro CLI 工具:
53 |
54 | npm i -g @tarojs/cli@latest
55 |
56 | // 2. 更新项目中 Taro 相关的依赖:
57 |
58 | taro update project
59 |
60 | // 3. 删除原来的node_modules后重新安装依赖(注意):
61 |
62 | cnpm install
63 |
64 | ```
65 |
66 | ## 目录结构
67 |
68 |
69 | ├── config // Taro配置目录
70 | ├── dist // 小程序编译结果目录
71 | │ ├── dev.js // 开发时配置
72 | │ ├── index.js // 默认配置
73 | │ └── prod.js // 打包时配置
74 | ├── src // 源码目录
75 | │ ├── components // 组件
76 | │ ├── pages // 页面文件目录
77 | │ │ ├── index
78 | │ │ └── page2
79 | │ ├── redux // redux
80 | │ │ ├── actions
81 | │ │ ├── reducers
82 | │ │ └── store
83 | │ ├── servers // 接口请求相关
84 | │ │ ├── api // 请求方法封装改造
85 | │ │ ├── config // BASE_URL获取
86 | │ │ ├── interceptors // 请求拦截器
87 | │ │ └── servers // 请求方法汇总
88 | │ ├── utils // 常用工具类
89 | │ ├── app.js // 入口文件
90 | │ └── index.html
91 | ├── README.md
92 | └── package.json
93 |
94 | ## 线上作品:
95 |
96 | 
97 |
--------------------------------------------------------------------------------
/config/dev.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | env: {
3 | NODE_ENV: '"development"'
4 | },
5 | defineConstants: {
6 | },
7 | weapp: {},
8 | h5: {}
9 | }
10 |
--------------------------------------------------------------------------------
/config/index.js:
--------------------------------------------------------------------------------
1 | // eslint-disable-next-line import/no-commonjs
2 | const path = require("path");
3 |
4 | const config = {
5 | projectName: 'tigerHee',
6 | date: '2019-4-1',
7 | designWidth: 750,
8 | deviceRatio: {
9 | '640': 2.34 / 2,
10 | '750': 1,
11 | '828': 1.81 / 2
12 | },
13 | sourceRoot: 'src',
14 | outputRoot: 'dist',
15 | plugins: {
16 | babel: {
17 | sourceMap: true,
18 | presets: [
19 | ['env', {
20 | modules: false
21 | }]
22 | ],
23 | plugins: [
24 | 'transform-decorators-legacy',
25 | 'transform-class-properties',
26 | 'transform-object-rest-spread'
27 | ]
28 | }
29 | },
30 | defineConstants: {
31 | },
32 | copy: {
33 | patterns: [
34 | ],
35 | options: {
36 | }
37 | },
38 | weapp: {
39 | module: {
40 | postcss: {
41 | autoprefixer: {
42 | enable: true,
43 | config: {
44 | browsers: [
45 | 'last 3 versions',
46 | 'Android >= 4.1',
47 | 'ios >= 8'
48 | ]
49 | }
50 | },
51 | pxtransform: {
52 | enable: true,
53 | config: {
54 |
55 | }
56 | },
57 | url: {
58 | enable: true,
59 | config: {
60 | limit: 10240 // 设定转换尺寸上限
61 | }
62 | },
63 | cssModules: {
64 | enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
65 | config: {
66 | namingPattern: 'module', // 转换模式,取值为 global/module
67 | generateScopedName: '[name]__[local]___[hash:base64:5]'
68 | }
69 | }
70 | }
71 | }
72 | },
73 | h5: {
74 | publicPath: '/',
75 | staticDirectory: 'static',
76 | module: {
77 | postcss: {
78 | autoprefixer: {
79 | enable: true,
80 | config: {
81 | browsers: [
82 | 'last 3 versions',
83 | 'Android >= 4.1',
84 | 'ios >= 8'
85 | ]
86 | }
87 | },
88 | cssModules: {
89 | enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
90 | config: {
91 | namingPattern: 'module', // 转换模式,取值为 global/module
92 | generateScopedName: '[name]__[local]___[hash:base64:5]'
93 | }
94 | }
95 | }
96 | }
97 | },
98 | alias: {
99 | '@src': path.resolve(__dirname, '..', 'src')
100 | }
101 | }
102 |
103 | module.exports = function (merge) {
104 | if (process.env.NODE_ENV === 'development') {
105 | return merge({}, config, require('./dev'))
106 | }
107 | return merge({}, config, require('./prod'))
108 | }
109 |
--------------------------------------------------------------------------------
/config/prod.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | env: {
3 | NODE_ENV: '"production"'
4 | },
5 | defineConstants: {
6 | },
7 | weapp: {},
8 | h5: {
9 | /**
10 | * 如果h5端编译后体积过大,可以使用webpack-bundle-analyzer插件对打包体积进行分析。
11 | * 参考代码如下:
12 | * webpackChain (chain) {
13 | * chain.plugin('analyzer')
14 | * .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin, [])
15 | * }
16 | */
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "tigerHee",
3 | "version": "1.0.0",
4 | "private": true,
5 | "description": "小程序研究学习",
6 | "scripts": {
7 | "build:weapp": "taro build --type weapp",
8 | "build:swan": "taro build --type swan",
9 | "build:alipay": "taro build --type alipay",
10 | "build:tt": "taro build --type tt",
11 | "build:h5": "taro build --type h5",
12 | "build:rn": "taro build --type rn",
13 | "dev:weapp": "npm run build:weapp -- --watch",
14 | "dev:swan": "npm run build:swan -- --watch",
15 | "dev:alipay": "npm run build:alipay -- --watch",
16 | "dev:tt": "npm run build:tt -- --watch",
17 | "dev:h5": "npm run build:h5 -- --watch",
18 | "dev:rn": "npm run build:rn -- --watch"
19 | },
20 | "author": "",
21 | "license": "MIT",
22 | "dependencies": {
23 | "@tarojs/async-await": "1.2.22",
24 | "@tarojs/components": "1.2.22",
25 | "@tarojs/redux": "1.2.22",
26 | "@tarojs/redux-h5": "1.2.22",
27 | "@tarojs/router": "1.2.22",
28 | "@tarojs/taro": "1.2.22",
29 | "@tarojs/taro-alipay": "1.2.22",
30 | "@tarojs/taro-h5": "1.2.22",
31 | "@tarojs/taro-swan": "1.2.22",
32 | "@tarojs/taro-tt": "1.2.22",
33 | "@tarojs/taro-weapp": "1.2.22",
34 | "nervjs": "^1.3.9",
35 | "nerv-devtools": "^1.3.9",
36 | "redux": "^4.0.0",
37 | "redux-logger": "^3.0.6",
38 | "redux-thunk": "^2.3.0",
39 | "taro-ui": "^2.0.0"
40 | },
41 | "devDependencies": {
42 | "@types/react": "^16.4.8",
43 | "@types/webpack-env": "^1.13.6",
44 | "@tarojs/plugin-babel": "1.2.22",
45 | "@tarojs/plugin-csso": "1.2.22",
46 | "@tarojs/plugin-sass": "1.2.22",
47 | "@tarojs/plugin-uglifyjs": "1.2.22",
48 | "@tarojs/webpack-runner": "1.2.22",
49 | "babel-plugin-transform-class-properties": "^6.24.1",
50 | "babel-plugin-transform-decorators-legacy": "^1.3.4",
51 | "babel-plugin-transform-jsx-stylesheet": "^0.6.5",
52 | "babel-plugin-transform-object-rest-spread": "^6.26.0",
53 | "babel-preset-env": "^1.6.1",
54 | "babel-eslint": "^8.2.3",
55 | "eslint": "^4.19.1",
56 | "eslint-config-taro": "1.2.22",
57 | "eslint-plugin-react": "^7.8.2",
58 | "eslint-plugin-import": "^2.12.0",
59 | "eslint-plugin-taro": "1.2.22"
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/project.config.json:
--------------------------------------------------------------------------------
1 | {
2 | "miniprogramRoot": "dist/",
3 | "projectname": "tigerHee",
4 | "description": "小程序研究学习",
5 | "appid": "appid",
6 | "setting": {
7 | "urlCheck": true,
8 | "es6": false,
9 | "postcss": false,
10 | "minified": false,
11 | "newFeature": true
12 | },
13 | "compileType": "miniprogram",
14 | "condition": {}
15 | }
--------------------------------------------------------------------------------
/src/app.js:
--------------------------------------------------------------------------------
1 | import '@tarojs/async-await'
2 | import Taro, { Component } from '@tarojs/taro'
3 | import { Provider } from '@tarojs/redux'
4 | import configStore from '@src/redux/store'
5 |
6 | import Index from '@src/pages/index'
7 | import 'taro-ui/dist/style/index.scss'
8 | import './app.scss'
9 |
10 | // 如果需要在 h5 环境中开启 React Devtools
11 | // 取消以下注释:
12 | // if (process.env.NODE_ENV !== 'production' && process.env.TARO_ENV === 'h5') {
13 | // require('nerv-devtools')
14 | // }
15 | const store = configStore()
16 |
17 | class App extends Component {
18 |
19 | config = {
20 | pages: [
21 | 'pages/index/index',
22 | 'pages/page2/page2',
23 | ],
24 | window: {
25 | backgroundTextStyle: 'light',
26 | navigationBarBackgroundColor: '#ff6100',
27 | navigationBarTitleText: '前端之最',
28 | navigationBarTextStyle: 'white',
29 | navigationStyle: 'default',
30 | backgroundColor: '#ffffff',
31 | }
32 | }
33 |
34 | componentDidMount() { }
35 |
36 | componentDidShow() { }
37 |
38 | componentDidHide() { }
39 |
40 | componentDidCatchError() { }
41 |
42 | // 在 App 类中的 render() 函数没有实际作用
43 | // 请勿修改此函数
44 | render() {
45 | return (
46 |