├── .babelrc ├── .commitlintrc.js ├── .eslintrc ├── .github ├── FUNDING.yml ├── ISSUE_TEMPLATE.md ├── auto_assign.yml ├── dependabot.yml ├── stale.yml └── workflows │ ├── greetings.yml │ └── traffic2badge.yml ├── .gitignore ├── .npmrc ├── .prettierignore ├── .prettierrc ├── .travis.yml ├── LICENSE ├── README.md ├── config ├── env.js ├── paths.js ├── webpack.config.dev.js ├── webpack.config.prod.js └── webpackDevServer.config.js ├── package.json ├── public ├── favicon.ico ├── index.html └── mock │ └── music.json ├── scripts ├── build.js └── start.js ├── src ├── client │ ├── devTools.js │ ├── index.js │ ├── index.less │ ├── middleware │ │ └── logger.js │ └── store │ │ ├── configureStore.dev.js │ │ ├── configureStore.js │ │ └── configureStore.prod.js └── common │ ├── actions │ ├── music.js │ └── todoList.js │ ├── api │ ├── index.js │ └── music.js │ ├── components │ ├── modalForm │ │ ├── form.js │ │ ├── index.js │ │ ├── index.less │ │ └── modal.js │ ├── searchbar │ │ ├── index.js │ │ └── index.less │ └── table │ │ ├── index.js │ │ └── index.less │ ├── container │ ├── bottom.js │ ├── bottom.less │ ├── content.js │ ├── content.less │ ├── header.js │ ├── header.less │ ├── index.js │ └── index.less │ ├── images │ ├── 360_logo.png │ ├── baidu_logo.png │ ├── chutian.jpg │ ├── face.png │ ├── minren.jpg │ ├── sougou_logo.png │ ├── xiaoying.jpg │ └── zuozu.jpg │ ├── pages │ ├── follow │ │ ├── index.js │ │ └── index.less │ ├── gallary │ │ ├── album │ │ │ ├── index.js │ │ │ └── index.less │ │ └── waterfall │ │ │ ├── imgUrlList.js │ │ │ ├── index.js │ │ │ ├── index.less │ │ │ └── waterfall.js │ ├── home │ │ ├── accessNum │ │ │ ├── EchartsViews.js │ │ │ └── index.js │ │ ├── buildSiteLog │ │ │ └── index.js │ │ ├── index.js │ │ ├── index.less │ │ ├── leftTop │ │ │ └── index.js │ │ ├── msgBanner │ │ │ └── index.js │ │ └── proFinish │ │ │ ├── EchartsProjects.js │ │ │ └── index.js │ ├── login │ │ ├── index.js │ │ └── index.less │ ├── mock │ │ ├── reform │ │ │ └── index.js │ │ ├── todo │ │ │ ├── index.js │ │ │ └── index.less │ │ └── useMock │ │ │ ├── index.js │ │ │ ├── index.less │ │ │ └── redux │ │ │ └── index.js │ ├── music │ │ ├── index.js │ │ └── index.less │ ├── search │ │ ├── index.js │ │ ├── index.less │ │ └── logo-select.js │ └── tools │ │ ├── editor │ │ ├── index.js │ │ └── index.less │ │ ├── smallTools │ │ ├── components │ │ │ ├── age.js │ │ │ ├── bmi.js │ │ │ ├── decorator.js │ │ │ ├── house.js │ │ │ └── salary.js │ │ ├── index.js │ │ └── index.less │ │ └── todoList │ │ ├── filterLink.js │ │ ├── index.js │ │ └── index.less │ ├── reducers │ ├── index.js │ ├── music.js │ └── todoList.js │ ├── routes.js │ └── utils │ ├── ajax.js │ ├── config.js │ ├── index.js │ └── menu.js ├── tsconfig.json └── yarn.lock /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "env", 4 | "react", 5 | "stage-2" 6 | ], 7 | "plugins": [ 8 | ["on-demand-loading", {"library": "diana"}], 9 | ["react-hot-loader/babel"] 10 | ] 11 | } -------------------------------------------------------------------------------- /.commitlintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parserPreset: { 3 | parserOpts: { 4 | headerPattern: /^(\w*)(?:\((.*)\))?:[ ]?(.*)$/, 5 | headerCorrespondence: ['type', 'scope', 'subject'] 6 | } 7 | }, 8 | rules: { 9 | 'type-empty': [2, 'never'], 10 | 'type-case': [2, 'always', 'lower-case'], 11 | 'subject-empty': [2, 'never'], 12 | 'type-enum': [2, 'always', ['feat', 'fix', 'docs', 'style', 'refactor', 'test', 'chore']] 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": true, 4 | "commonjs": true, 5 | "es6": true, 6 | "node": true 7 | }, 8 | "parser": "babel-eslint", 9 | "extends": ["airbnb", "eslint-config-prettier"], 10 | "parserOptions": { 11 | "sourceType": "module", 12 | "ecmaFeatures": { 13 | "experimentalObjectRestSpread": true, 14 | "jsx": true, 15 | "modules": true 16 | } 17 | }, 18 | "rules": { 19 | "indent": 0, 20 | "linebreak-style": ["error", "unix"], 21 | "semi": ["error", "never"], 22 | "react/prop-types": ["ignore"], 23 | "react/display-name": ["ignore"], 24 | "react/jsx-filename-extension": 0, 25 | "arrow-parens": 0, 26 | "import/no-unresolved": 0, 27 | "import/no-extraneous-dependencies": 0, 28 | "no-plusplus": 0, 29 | "no-param-reassign": 0 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | custom: ['http://muyunyun.cn/sponsor/'] 14 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | What is the current behavior? 2 | 3 | If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. 4 | 5 | What is the expected behavior? 6 | -------------------------------------------------------------------------------- /.github/auto_assign.yml: -------------------------------------------------------------------------------- 1 | # Set to true to add reviewers to pull requests 2 | addReviewers: true 3 | 4 | # Set to true to add assignees to pull requests 5 | addAssignees: true 6 | 7 | # A list of reviewers to be added to pull requests (GitHub user name) 8 | reviewers: 9 | - MuYunyun 10 | 11 | # A list of keywords to be skipped the process that add reviewers if pull requests include it 12 | skipKeywords: 13 | - wip 14 | 15 | # A number of reviewers added to the pull request 16 | # Set 0 to add all the reviewers (default: 0) 17 | numberOfReviewers: 0 -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # see https://github.com/yi-Xu-0100/traffic-to-badge/blob/main/.github/dependabot.yml 2 | version: 2 3 | updates: 4 | # Enable version updates for npm 5 | - package-ecosystem: 'npm' 6 | directory: '/' 7 | schedule: 8 | interval: 'daily' 9 | # Maintain dependencies for GitHub Actions 10 | - package-ecosystem: 'github-actions' 11 | directory: '/' 12 | schedule: 13 | interval: 'daily' 14 | -------------------------------------------------------------------------------- /.github/stale.yml: -------------------------------------------------------------------------------- 1 | # Configuration for probot-stale - https://github.com/probot/stale 2 | 3 | # Number of days of inactivity before an Issue or Pull Request becomes stale 4 | daysUntilStale: 60 5 | 6 | # Number of days of inactivity before an Issue or Pull Request with the stale label is closed. 7 | # Set to false to disable. If disabled, issues still need to be closed manually, but will remain marked as stale. 8 | daysUntilClose: 7 9 | 10 | # Only issues or pull requests with all of these labels are check if stale. Defaults to `[]` (disabled) 11 | onlyLabels: [] 12 | 13 | # Issues or Pull Requests with these labels will never be considered stale. Set to `[]` to disable 14 | exemptLabels: 15 | - pinned 16 | - security 17 | - "[Status] Maybe Later" 18 | 19 | # Set to true to ignore issues in a project (defaults to false) 20 | exemptProjects: false 21 | 22 | # Set to true to ignore issues in a milestone (defaults to false) 23 | exemptMilestones: false 24 | 25 | # Set to true to ignore issues with an assignee (defaults to false) 26 | exemptAssignees: false 27 | 28 | # Label to use when marking as stale 29 | staleLabel: wontfix 30 | 31 | # Comment to post when marking as stale. Set to `false` to disable 32 | markComment: > 33 | This issue has been automatically marked as stale because it has not had 34 | recent activity. It will be closed if no further activity occurs. Thank you 35 | for your contributions. 36 | 37 | # Comment to post when removing the stale label. 38 | # unmarkComment: > 39 | # Your comment here. 40 | 41 | # Comment to post when closing a stale Issue or Pull Request. 42 | # closeComment: > 43 | # Your comment here. 44 | 45 | # Limit the number of actions per hour, from 1-30. Default is 30 46 | limitPerRun: 30 47 | 48 | # Limit to only `issues` or `pulls` 49 | # only: issues 50 | 51 | # Optionally, specify configuration settings that are specific to just 'issues' or 'pulls': 52 | # pulls: 53 | # daysUntilStale: 30 54 | # markComment: > 55 | # This pull request has been automatically marked as stale because it has not had 56 | # recent activity. It will be closed if no further activity occurs. Thank you 57 | # for your contributions. 58 | 59 | # issues: 60 | # exemptLabels: 61 | # - confirmed -------------------------------------------------------------------------------- /.github/workflows/greetings.yml: -------------------------------------------------------------------------------- 1 | name: Greetings 2 | 3 | on: [pull_request, issues] 4 | 5 | jobs: 6 | greeting: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/first-interaction@v1 10 | with: 11 | repo-token: ${{ secrets.GITHUB_TOKEN }} 12 | issue-message: 'Welcome your first issue here, thanks' 13 | pr-message: 'Welcome your first pr here, thanks' 14 | -------------------------------------------------------------------------------- /.github/workflows/traffic2badge.yml: -------------------------------------------------------------------------------- 1 | name: traffic2badge 2 | on: 3 | push: 4 | branches: 5 | - master 6 | schedule: 7 | - cron: '1 0 * * *' #UTC 8 | 9 | jobs: 10 | run: 11 | name: Make GitHub Traffic to Badge 12 | runs-on: ubuntu-latest 13 | steps: 14 | - name: Checkout Code 15 | uses: actions/checkout@v2.3.3 16 | 17 | - name: Get Commit Message 18 | id: message 19 | uses: actions/github-script@v3.0.0 20 | env: 21 | FULL_COMMIT_MESSAGE: '${{ github.event.head_commit.message }}' 22 | with: 23 | result-encoding: string 24 | script: | 25 | var message = `${process.env.FULL_COMMIT_MESSAGE}`; 26 | core.info(message); 27 | if (message != '') return message; 28 | var time = new Date(Date.now()).toISOString(); 29 | core.info(time); 30 | return `Get traffic data at ${time}`; 31 | 32 | - name: Set Traffic 33 | id: traffic 34 | uses: yi-Xu-0100/traffic-to-badge@v1.1.5 35 | with: 36 | my_token: ${{ secrets.TRAFFIC_TOKEN }} 37 | #(default) static_list: ${{ github.repository }} 38 | #(default) traffic_branch: traffic 39 | #(default) views_color: brightgreen 40 | #(default) clones_color: brightgreen 41 | #(default) logo: github 42 | 43 | - name: Deploy 44 | uses: peaceiris/actions-gh-pages@v3.7.3 45 | with: 46 | github_token: ${{ secrets.GITHUB_TOKEN }} 47 | publish_branch: ${{ steps.traffic.outputs.traffic_branch }} 48 | publish_dir: ${{ steps.traffic.outputs.traffic_path }} 49 | user_name: 'github-actions[bot]' 50 | user_email: 'github-actions[bot]@users.noreply.github.com' 51 | full_commit_message: ${{ steps.message.outputs.result }} 52 | 53 | - name: Show Traffic Data 54 | run: | 55 | echo ${{ steps.traffic.outputs.traffic_branch }} 56 | echo ${{ steps.traffic.outputs.traffic_path }} 57 | cd ${{ steps.traffic.outputs.traffic_path }} 58 | ls -a 59 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | 3 | npm-debug.log 4 | 5 | build 6 | 7 | yarn-error.log 8 | 9 | package-lock.json 10 | 11 | yarn.lock -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | # .npmrc 2 | registry=https://registry.npmjs.org/ 3 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | coverage 3 | build 4 | .github 5 | public -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "bracketSpacing": true, 3 | "jsxBracketSameLine": false, 4 | "printWidth": 100, 5 | "semi": false, 6 | "singleQuote": true, 7 | "tabWidth": 2 8 | } 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # https://docs.travis-ci.com/user/languages/javascript-with-nodejs/ 2 | # https://travis-ci.org/ 3 | language: node_js 4 | node_js: 5 | - '8' 6 | install: 7 | - npm install 8 | script: 9 | - npm run build 10 | branches: 11 | only: 12 | - master 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 MuYunyun 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![GitHub views](https://raw.githubusercontent.com/MuYunyun/reactSPA/traffic/traffic-reactSPA/views.svg) ![Build Status](https://travis-ci.org/MuYunyun/reactSPA.svg?branch=master) ![LICENSE MIT](https://img.shields.io/npm/l/express.svg) 2 | 3 | 该项目是对 React 技术栈以及前端工程化的相关实践。 4 | 5 | > 愿景:沉淀出一套针对中台系统的开发相对完善的使用方案。 6 | 7 | [效果展示](https://muyunyun.github.io/reactSPA),建议本地打开。 8 | 9 | - 部分模块展示: 10 | 11 | ![](http://files.cnblogs.com/files/MuYunyun/reactSPA.gif) 12 | 13 | - redux 实现一个 todoList: 14 | 15 | ![](http://files.cnblogs.com/files/MuYunyun/todoList.gif) 16 | 17 | ### Usage 18 | 19 | ``` 20 | 本地运行 21 | yarn install || npm install 22 | yarn start || npm start 23 | 24 | 打包 25 | yarn build || npm run build 26 | 27 | 发布 28 | yarn deploy || npm run deploy 29 | ``` 30 | 31 | ### Tech Stack 32 | 33 | - 编译打包: Babel Webpack(4.x) 34 | - 热更新: webpack-dev-server 35 | - UI 库: React & React-Dom(^16.11.0) 36 | - UI 组件: Antd(3.x) 37 | - 路由: react-router(4.x)、react-router-redux 38 | - 状态管理: redux (TodoList: Graghql + Relay) 39 | - JS: ES6、ES7 40 | - 样式: less 41 | - Ajax: Fetch 42 | - 跨域: CORS 43 | - 代码校验: Eslint(Airbnb 规范) 44 | - 网关: [demo](https://github.com/MuYunyun/gateway) 45 | - 表单: [daForm](https://github.com/dwd-fe/daForm) 46 | 47 | ### articles 48 | 49 | - [React 性能优化实践](https://github.com/MuYunyun/reactSPA/issues/54) 50 | - [压缩打包优化](https://github.com/MuYunyun/reactSPA/issues/53) 51 | - [css 模块化](https://github.com/MuYunyun/reactSPA/issues/52) 52 | - [使用 React 全家桶搭建一个后台管理系统](http://muyunyun.cn/posts/9bfbdbf4/) 53 | - [redux middleware 源码分析](http://muyunyun.cn/posts/7f9a92dc/) 54 | - [探寻 webpack 插件机制](https://github.com/MuYunyun/blog/issues/19) 55 | - [React16.x 特性剪辑](https://github.com/MuYunyun/blog/blob/master/BasicSkill/React周边/React16.x特性剪辑.md) 56 | 57 | ### Module 58 | 59 | - 音乐模块 60 | - 音乐列表 61 | - 工具模块 62 | - 工资、房租、身体指数、年龄的智能计算器 63 | - 富文本编辑器 64 | - 待办事项 65 | - 画廊模块 66 | - 图片瀑布流(撸了个插件 [jswaterfall](https://github.com/MuYunyun/waterfall)) 67 | - 搜索模块 68 | - 搜索引擎(集成百度、360、搜狗搜索) 69 | - 其它 70 | - one for all 表单方案 —— [daForm](https://github.com/dwd-fe/daForm) 71 | 72 | ### Project Structure 73 | 74 | ``` 75 | ├── build.js 项目打包后的文件 76 | ├── config webpack配置文件 77 | │ ├──... 78 | │ ├──webpack.config.dev.js 开发环境配置(启动速度优化) 79 | │ ├──webpack.config.prod.js 生产环境配置(打包体积优化) 80 | ├── node_modules node模块目录 81 | ├── public 82 | │   └──index.html 83 | ├── scripts 84 | │   ├── build.js 打包项目文件 85 | │   ├── start.js 启动项目文件 86 | │   └── test.js 测试项目文件 87 | ├── src 88 | │   ├── client 89 | │   │   ├── store redux中的store 90 | │   │   ├── devTools.js 开发者工具 91 | │   ├── common 核心目录 92 | │   │   ├── api 请求api层 93 | │   │   ├── actions redux中的action 94 | │   │   ├── components 通用功能组件 95 | │   │   ├── container 通用样式组件 96 | │   │   ├── images 97 | │   │   ├── pages 页面模块 98 | │   │   ├── reducers redux中的reducer 99 | │   │   ├── utils 工具类 100 | │   │   │ ├── index.js 101 | │   │   │ ├── config.js 通用配置 102 | │   │   │ ├── menu.js 菜单配置 103 | │   │   │ └── ajax.js ajax模块 104 | │   │   └── routes.js 前端路由 105 | │   └── server 服务端目录(日后用到) 106 | │   └── controller 107 | ├── .gitignore 108 | ├── package.json 109 | ├── README.md 110 | └── yarn.lock 111 | ``` 112 | 113 | ### todoList 114 | 115 | - [ ] 使用 suspense 替代 react-loadable 116 | - [ ] 状态管理库迁移至 graghql 117 | -------------------------------------------------------------------------------- /config/env.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs') 2 | const path = require('path') 3 | const paths = require('./paths') 4 | 5 | // Make sure that including paths.js after env.js will read .env variables. 6 | delete require.cache[require.resolve('./paths')] 7 | 8 | const { NODE_ENV } = process.env 9 | if (!NODE_ENV) { 10 | throw new Error('The NODE_ENV environment variable is required but was not specified.') 11 | } 12 | 13 | // https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use 14 | const dotenvFiles = [ 15 | `${paths.dotenv}.${NODE_ENV}.local`, 16 | `${paths.dotenv}.${NODE_ENV}`, 17 | // Don't include `.env.local` for `test` environment 18 | // since normally you expect tests to produce the same 19 | // results for everyone 20 | NODE_ENV !== 'test' && `${paths.dotenv}.local`, 21 | paths.dotenv 22 | ].filter(Boolean) 23 | 24 | // Load environment variables from .env* files. Suppress warnings using silent 25 | // if this file is missing. dotenv will never modify any environment variables 26 | // that have already been set. 27 | // https://github.com/motdotla/dotenv 28 | dotenvFiles.forEach(dotenvFile => { 29 | if (fs.existsSync(dotenvFile)) { 30 | require('dotenv').config({ 31 | path: dotenvFile 32 | }) 33 | } 34 | }) 35 | 36 | // We support resolving modules according to `NODE_PATH`. 37 | // This lets you use absolute paths in imports inside large monorepos: 38 | // https://github.com/facebookincubator/create-react-app/issues/253. 39 | // It works similar to `NODE_PATH` in Node itself: 40 | // https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders 41 | // Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored. 42 | // Otherwise, we risk importing Node.js core modules into an app instead of Webpack shims. 43 | // https://github.com/facebookincubator/create-react-app/issues/1023#issuecomment-265344421 44 | // We also resolve them to make sure all tools using them work consistently. 45 | const appDirectory = fs.realpathSync(process.cwd()) 46 | process.env.NODE_PATH = (process.env.NODE_PATH || '') 47 | .split(path.delimiter) 48 | .filter(folder => folder && !path.isAbsolute(folder)) 49 | .map(folder => path.resolve(appDirectory, folder)) 50 | .join(path.delimiter) 51 | 52 | // 获取客户端环境 53 | function getClientEnvironment() { 54 | const raw = { 55 | NODE_ENV: process.env.NODE_ENV || 'development' 56 | // PUBLIC_URL: publicUrl, 57 | } 58 | 59 | const stringified = { 60 | 'process.env': Object.keys(raw).reduce((env, key) => { 61 | env[key] = JSON.stringify(raw[key]) 62 | return env 63 | }, {}) 64 | } 65 | return { raw, stringified } 66 | } 67 | 68 | module.exports = getClientEnvironment 69 | -------------------------------------------------------------------------------- /config/paths.js: -------------------------------------------------------------------------------- 1 | const path = require('path') 2 | const fs = require('fs') 3 | const url = require('url') 4 | 5 | // 确保任何项目的链接已经成功加载 6 | const appDirectory = fs.realpathSync(process.cwd()) 7 | const resolveApp = relativePath => path.resolve(appDirectory, relativePath) 8 | 9 | const envPublicUrl = process.env.PUBLIC_URL 10 | 11 | function ensureSlash(path, needsSlash) { 12 | const hasSlash = path.endsWith('/') 13 | if (hasSlash && !needsSlash) { 14 | return path.substr(path, path.length - 1) 15 | } else if (!hasSlash && needsSlash) { 16 | return `${path}/` // reactSPA/ 17 | } else { 18 | return path 19 | } 20 | } 21 | 22 | // 使用 PUBLIC_URL 或者 homepage 字段来推断应用程序所在的'公共路径' 23 | const getPublicUrl = appPackageJson => envPublicUrl || require(appPackageJson).homepage 24 | 25 | function getServedPath(appPackageJson) { 26 | const publicUrl = getPublicUrl(appPackageJson) 27 | const servedUrl = envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/') // 本 demo 中,servedUrl 为 reactSPA 28 | return ensureSlash(servedUrl, true) 29 | } 30 | 31 | module.exports = { 32 | dotenv: resolveApp('.env'), 33 | appBuild: resolveApp('build'), 34 | appPublic: resolveApp('public'), 35 | appHtml: resolveApp('public/index.html'), 36 | appIndexJs: resolveApp('src/client/index.js'), 37 | appPackageJson: resolveApp('package.json'), 38 | appSrc: resolveApp('src'), 39 | yarnLockFile: resolveApp('yarn.lock'), 40 | testsSetup: resolveApp('src/__tests__/index.test.js'), 41 | appNodeModules: resolveApp('node_modules'), 42 | publicUrl: getPublicUrl(resolveApp('package.json')), 43 | servedPath: getServedPath(resolveApp('package.json')) // 本 demo 中,为 reactSPA/ 44 | } 45 | -------------------------------------------------------------------------------- /config/webpack.config.dev.js: -------------------------------------------------------------------------------- 1 | const autoprefixer = require('autoprefixer') 2 | const path = require('path') 3 | const webpack = require('webpack') 4 | const HtmlWebpackPlugin = require('html-webpack-plugin') 5 | const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin') 6 | const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin') 7 | const AnalyzeWebpackPlugin = require('analyze-webpack-plugin').default 8 | const getClientEnvironment = require('./env') 9 | const paths = require('./paths') 10 | 11 | const publicPath = '/' 12 | 13 | const publicUrl = '' 14 | const env = getClientEnvironment(publicUrl) 15 | 16 | module.exports = { 17 | mode: 'development', 18 | devtool: 'cheap-module-source-map', // https://webpack.js.org/configuration/devtool/ 19 | entry: [ 20 | `${require.resolve('webpack-dev-server/client')}?/`, 21 | require.resolve('webpack/hot/dev-server'), 22 | require.resolve('react-error-overlay'), // 开发报错,将导致启动不了 23 | paths.appIndexJs 24 | ], 25 | output: { 26 | // https://doc.webpack-china.org/configuration/output/ 27 | path: paths.appBuild, // 开发环境这句没用,但是生成环境要用 28 | pathinfo: true, // 告诉 webpack 在 bundle 中引入「所包含模块信息」的相关注释,开发环境用 29 | filename: 'static/js/bundle.js', // 这不是真实的文件,其仅仅是在开发环境下由 WebpackDevServer 提供的一个虚拟路径 30 | chunkFilename: 'static/js/[name].chunk.js', // 使用了 code splitting 后其它的 chunk 文件 31 | publicPath, 32 | devtoolModuleFilenameTemplate: info => 33 | path.resolve(info.absoluteResourcePath).replace(/\\/g, '/') 34 | }, 35 | resolve: { 36 | modules: ['node_modules', paths.appNodeModules].concat( 37 | // 设置寻找模块的先后机制 38 | process.env.NODE_PATH.split(path.delimiter).filter(Boolean) 39 | ), 40 | extensions: ['.js', '.json', '.jsx'], 41 | alias: { 42 | components: `${path.resolve(__dirname, '..')}/src/common/components`, 43 | container: `${path.resolve(__dirname, '..')}/src/common/container`, 44 | images: `${path.resolve(__dirname, '..')}/src/common/images`, 45 | pages: `${path.resolve(__dirname, '..')}/src/common/pages`, 46 | utils: `${path.resolve(__dirname, '..')}/src/common/utils`, 47 | data: `${path.resolve(__dirname, '..')}/src/server/data`, 48 | actions: `${path.resolve(__dirname, '..')}/src/common/actions`, 49 | reducers: `${path.resolve(__dirname, '..')}/src/common/reducers`, 50 | api: `${path.resolve(__dirname, '..')}/src/common/api` 51 | }, 52 | plugins: [ 53 | // 该插件把模块作用域限制在 src 和 node_module 中 54 | new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]) 55 | ] 56 | }, 57 | 58 | module: { 59 | // https://doc.webpack-china.org/configuration/module/ 60 | strictExportPresence: true, 61 | rules: [ 62 | { 63 | oneOf: [ 64 | { 65 | test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/], 66 | loader: require.resolve('url-loader'), 67 | options: { 68 | limit: 10000, 69 | name: 'static/media/[name].[hash:8].[ext]' 70 | } 71 | }, 72 | { 73 | test: /\.(js|jsx)$/, 74 | include: paths.appSrc, 75 | loader: require.resolve('babel-loader'), 76 | options: { 77 | // 缓存在 ./node_modules/.cache/babel-loader/,从而能更快地 rebuild, 78 | cacheDirectory: true, // 可能项目大了以后该属性有影响 79 | plugins: [ 80 | 'transform-decorators-legacy', 81 | ['import', { libraryName: 'antd', style: true }] 82 | ] 83 | } 84 | }, 85 | { 86 | test: /\.css$/, 87 | use: [ 88 | require.resolve('style-loader'), // 将 js 字符串(样式)插入 style 89 | { 90 | loader: require.resolve('css-loader'), // 将 css 转为 common.js 91 | options: { 92 | importLoaders: 1 93 | } 94 | }, 95 | { 96 | // "postcss" loader applies autoprefixer to our CSS. 97 | loader: require.resolve('postcss-loader'), 98 | options: { 99 | ident: 'postcss', 100 | plugins: () => [ 101 | require('postcss-flexbugs-fixes'), 102 | autoprefixer({ 103 | browsers: [ 104 | '>1%', 105 | 'last 4 versions', 106 | 'Firefox ESR', 107 | 'not ie < 9' // React doesn't support IE8 anyway 108 | ], 109 | flexbox: 'no-2009' 110 | }) 111 | ] 112 | } 113 | } 114 | ] 115 | }, 116 | { 117 | test: /\.less$/, 118 | use: [ 119 | require.resolve('style-loader'), 120 | { 121 | loader: require.resolve('css-loader'), 122 | options: { 123 | // modules: true, 124 | importLoaders: 2 125 | } 126 | }, 127 | { 128 | loader: require.resolve('postcss-loader'), 129 | options: { 130 | ident: 'postcss', // https://webpack.js.org/guides/migrating/#complex-options 131 | plugins: () => [ 132 | require('postcss-flexbugs-fixes'), 133 | autoprefixer({ 134 | browsers: [ 135 | '>1%', 136 | 'last 4 versions', 137 | 'Firefox ESR', 138 | 'not ie < 9' // React doesn't support IE8 anyway 139 | ], 140 | flexbox: 'no-2009' 141 | }) 142 | ] 143 | } 144 | }, 145 | { 146 | loader: require.resolve('less-loader'), 147 | options: { 148 | modifyVars: { '@primary-color': '#1890ff' } 149 | } 150 | } 151 | ] 152 | }, 153 | // 如果还要加 loader,请加到 file-loader 之前 154 | { 155 | exclude: [/\.js$/, /\.html$/, /\.json$/, /\.less$/], 156 | loader: require.resolve('file-loader'), 157 | options: { 158 | name: 'static/media/[name].[hash:8].[ext]' 159 | } 160 | } 161 | ] 162 | } 163 | ] 164 | }, 165 | plugins: [ 166 | // 将 Webpack 打包生成的 bundles 插入 HTML 中 script 标签中 167 | new HtmlWebpackPlugin({ 168 | inject: true, 169 | template: paths.appHtml 170 | }), 171 | // 当开启 HMR 的时候使用该插件会显示模块的相对路径,建议用于开发环境。作用没理解~ 172 | // new webpack.NamedModulesPlugin(), 173 | // 区别开发模式和发布模式的全局变量 174 | new webpack.DefinePlugin( 175 | Object.assign({}, env.stringified, { 176 | ENABLE_DEVTOOLS: true 177 | }) 178 | ), 179 | // This is necessary to emit hot updates (currently CSS only): 180 | new webpack.HotModuleReplacementPlugin(), 181 | // 防止大小写错误 182 | new CaseSensitivePathsPlugin(), 183 | // 优化 moment.js 库的体积,https://github.com/jmblog/how-to-optimize-momentjs-with-webpack 184 | new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/), 185 | new AnalyzeWebpackPlugin() // 输入 http://localhost:3000/analyze.html 查看相应信息,从而进行优化 186 | ], 187 | // 将一些在浏览器不起作用,但是引用到的库置空 188 | node: { 189 | dgram: 'empty', 190 | fs: 'empty', 191 | net: 'empty', 192 | tls: 'empty' 193 | }, 194 | // Turn off performance hints during development because we don't do any 195 | // splitting or minification in interest of speed. These warnings become 196 | // cumbersome. 197 | performance: { 198 | hints: false 199 | } 200 | } 201 | -------------------------------------------------------------------------------- /config/webpack.config.prod.js: -------------------------------------------------------------------------------- 1 | const autoprefixer = require('autoprefixer') 2 | const path = require('path') 3 | const webpack = require('webpack') 4 | const HtmlWebpackPlugin = require('html-webpack-plugin') 5 | const ManifestPlugin = require('webpack-manifest-plugin') 6 | const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin') 7 | const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin') 8 | const MiniCssExtractPlugin = require('mini-css-extract-plugin') 9 | const paths = require('./paths') 10 | const getClientEnvironment = require('./env') 11 | 12 | // 从哪里获取服务,需要一个末尾斜杠 13 | const publicPath = paths.servedPath 14 | // Source maps are resource heavy and can cause out of memory issue for large source files. 15 | const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false' 16 | const publicUrl = publicPath.slice(0, -1) 17 | const env = getClientEnvironment(publicUrl) 18 | 19 | // 下面代码出于安全考虑 20 | if (env.stringified['process.env'].NODE_ENV !== '"production"') { 21 | throw new Error('Production builds must have NODE_ENV=production.') 22 | } 23 | 24 | module.exports = { 25 | mode: 'production', 26 | // 当 webpack 遇到第一个错,标红抛出并中断运行 27 | bail: true, 28 | devtool: shouldUseSourceMap ? 'source-map' : false, 29 | entry: { 30 | IndexJs: paths.appIndexJs 31 | }, 32 | output: { 33 | path: paths.appBuild, 34 | filename: 'static/js/[name].[chunkhash:8].js', 35 | chunkFilename: 'static/js/[name].[chunkhash:8].chunk.js', 36 | publicPath, 37 | // Point sourcemap entries to original disk location 38 | devtoolModuleFilenameTemplate: info => 39 | path.relative(paths.appSrc, info.absoluteResourcePath).replace(/\\/g, '/') 40 | }, 41 | resolve: { 42 | modules: ['node_modules', paths.appNodeModules].concat( 43 | process.env.NODE_PATH.split(path.delimiter).filter(Boolean) 44 | ), 45 | extensions: ['.js', '.json', '.jsx'], 46 | alias: { 47 | components: `${path.resolve(__dirname, '..')}/src/common/components`, 48 | container: `${path.resolve(__dirname, '..')}/src/common/container`, 49 | images: `${path.resolve(__dirname, '..')}/src/common/images`, 50 | pages: `${path.resolve(__dirname, '..')}/src/common/pages`, 51 | utils: `${path.resolve(__dirname, '..')}/src/common/utils`, 52 | data: `${path.resolve(__dirname, '..')}/src/server/data`, 53 | actions: `${path.resolve(__dirname, '..')}/src/common/actions`, 54 | reducers: `${path.resolve(__dirname, '..')}/src/common/reducers`, 55 | api: `${path.resolve(__dirname, '..')}/src/common/api` 56 | }, 57 | plugins: [new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson])] 58 | }, 59 | module: { 60 | strictExportPresence: true, 61 | rules: [ 62 | { 63 | oneOf: [ 64 | { 65 | test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/], 66 | loader: require.resolve('url-loader'), 67 | options: { 68 | limit: 10000, 69 | name: 'static/media/[name].[hash:8].[ext]' 70 | } 71 | }, 72 | { 73 | test: /\.(js|jsx)$/, 74 | include: paths.appSrc, 75 | loader: require.resolve('babel-loader'), 76 | options: { 77 | plugins: [ 78 | 'transform-decorators-legacy', 79 | ['import', { libraryName: 'antd', style: true }] 80 | ], 81 | compact: true 82 | } 83 | }, 84 | { 85 | test: /\.css$/, 86 | use: [ 87 | // "style" loader turns CSS into JS modules that inject