前端开发的脚手架架构

前端开发规范-react/react-router/redux/webpack/es6开发配置

项目地址: https://github.com/fangyongbao/react-router-redux-webpack

image.png

这里的webpack配置是针对多页面开发, 单页面中进行路由跳转,这里需要5个相关配置文件:

<pre class="prettyprint" style="padding: 8px; color: rgb(68, 68, 68); border-radius: 2px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 20px; font-size: 14px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">package.json 定义项目依赖模块 以及 定义开启服务器和打包命令 helpers.js 获取入口文件和html文件 webpack.base.config.js 定义webpack基础配置 webpack.dev.config.js 定义webpack开发配置 webpack.prod.config.js 定义webpack生产配置</pre>

改项目中包含了:

<pre class="prettyprint" style="padding: 8px; color: rgb(68, 68, 68); border-radius: 2px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 20px; font-size: 14px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">路由跳转 数据请求模块封装 webview与js交互模块封装 配置文件分离 自动打包多个html文件 以一个简单的例子实现 dispatch->action->reducer->state->view流程 定义共用sass文件 提取css
css自动添加浏览器前缀 上线包添加静态资源版本号 上线包css/js压缩 定义第三方字体库</pre>

使用方式:

<pre class="prettyprint" style="padding: 8px; color: rgb(68, 68, 68); border-radius: 2px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 20px; font-size: 14px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">1.npm install webpack-dev-server -g 全局安装服务器 2.到package.json所在目录执行npm install 安装依赖模块 3.npm run start 开启服务器(包含热更新) 4.浏览器访问localhost:80 5.npm run build打包</pre>

webpack相关配置文件

package.json文件:

<pre class="prettyprint" style="padding: 8px; color: rgb(68, 68, 68); border-radius: 2px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 20px; font-size: 14px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> { "name": "pass", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "dev": "webpack-dev-server --inline --hot --port 3000 --progress --profile --colors --watch --display-error-details --display-cached --config ./webpack.dev.config.js", "build": "webpack --progress --profile --colors --display-error-details --display-cached --bail --config ./webpack.prod.config.js" }, "author": "maicon", "license": "ISC", "devDependencies": { "babel-core": "^6.0.0", "babel-loader": "^6.0.0", "babel-plugin-transform-runtime": "^6.0.0", "babel-preset-es2015": "^6.0.0", "babel-preset-react": "^6.0.0", "copy-webpack-plugin": "^3.0.1", "css-loader": "^0.23.0", "express": "^4.13.3", "extract-text-webpack-plugin": "^1.0.1", "file-loader": "^0.9.0", "glob": "^7.0.6", "html-webpack-plugin": "^2.8.1", "json-loader": "^0.5.4", "node-sass": "^3.8.0", "sass-loader": "^4.0.0", "style-loader": "^0.13.1", "url-loader": "^0.5.7", "webpack": "^1.12.2" }, "dependencies": { "babel-polyfill": "^6.13.0", "es6-promise": "^3.2.1", "jquery": "^3.1.0", "md5": "^2.2.1", "react": "^15.3.0", "react-dom": "^15.3.0", "react-redux": "^4.4.5", "react-router": "^2.7.0", "redux": "^3.5.2", "redux-thunk": "^2.1.0" } }</pre>

helpers.js文件

<pre class="prettyprint" style="padding: 8px; color: rgb(68, 68, 68); border-radius: 2px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 20px; font-size: 14px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">var glob = require('glob'); var path = require('path'); var HtmlWebpackPlugin = require('html-webpack-plugin'); function getHtmlPlugin() { var buildPath = ''; var plugins = []; glob.sync('./.html').forEach(function (name) { //[^/]不匹配这个集合中的任何一个字符 var n = name.match(/([^/]+).html/)[1]; plugins.push( new HtmlWebpackPlugin({ filename:buildPath+n+'.html', template:name, inject:false }) ) }); return plugins; } function getEntry() { var entry = {}; glob.sync('./entry/.js').forEach(function (name) { console.log(name); //[^/]不匹配这个集合中的任何一个字符 var n = name.match(/([^/]+).js/)[1]; entry[n] = name; }); return entry; }; exports.getHtmlPlugin = getHtmlPlugin; exports.getEntry = getEntry;</pre>

webpack.base.config.js文件

<pre class="prettyprint" style="padding: 8px; color: rgb(68, 68, 68); border-radius: 2px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 20px; font-size: 14px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">var path = require('path'); var webpack = require('webpack'); var helpers = require('./helpers'); module.exports = { //入口文件 entry: helpers.getEntry(), //输出文件 output: { path: path.join(__dirname, '/dist'), //热更新资源存放目录 //publicPath:'/', filename: 'js/[name].js', }, resolve: { //定义模块缩写名称 alias: { 'common.scss':path.join(__dirname,'/src/assets/scss/common.scss') }, //resolve 指定可以被 import 的文件后缀 extensions: ['', '.js', '.css', '.scss'] }, module: { //加载器配置 loaders: [ { test:/.js?$/, loader:'babel-loader', exclude: /node_modules/, query:{ presets:['es2015','react'] } }, { test: /.scss/, loader: 'style-loader!css-loader!sass-loader' }, { test: /.css$/, loader: 'style-loader!css-loader' }, { test: /.(jpe?g|png|gif|svg)$/i, loader: 'url-loader?limit=1000&name=images/[name].[ext]', query:{} }, { test: /.(woff2?|eot|ttf|otf)(?.*)?$/, loader: 'url-loader', query: { limit: 10000, name: 'fonts/[name].[ext]' } } ] }, //插件项 plugins: [ ] }</pre>

webpack.dev.config.js文件

<pre class="prettyprint" style="padding: 8px; color: rgb(68, 68, 68); border-radius: 2px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 20px; font-size: 14px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> var path = require('path'); var webpack = require('webpack'); var helpers = require('./helpers'); var commonWebpack = require('./webpack.base.config'); //var CopyWebpackPlugin = require('copy-webpack-plugin'); const METADATA = { host: '.' }; module.exports = Object.assign({}, commonWebpack, { //供html使用的变量 metadata: METADATA, //开发环境设定独立的插件 plugins: [ //commonsPlugin 可以用于分析模块的共用代码, 单独打一个包出来 new webpack.optimize.CommonsChunkPlugin('js/common.js'), //代码热替换 new webpack.HotModuleReplacementPlugin(), //将图片拷贝到指定位置 // new CopyWebpackPlugin([{ // from: 'src/assets/images', // to: 'images' // }]), //React 官方提供的代码是已经合并的, 这个是 Webpack 不推荐的用法, //在合并话的代码上进行定制有点麻烦, Webpack 提供了设置环境变量来优化代码的方案: new webpack.DefinePlugin({ //配置组件中使用的变量,组件中可以直接使用{metadata.host} metadata:JSON.stringify(METADATA), "process.env": { NODE_ENV: JSON.stringify("production") } }) //concat进行数组合并 ].concat(helpers.getHtmlPlugin()) }); </pre>

webpack.prod.config.js文件

<pre class="prettyprint" style="padding: 8px; color: rgb(68, 68, 68); border-radius: 2px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 20px; font-size: 14px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">var path = require('path'); var webpack = require('webpack'); var helpers = require('./helpers'); var commonWebpack = require('./webpack.base.config'); var ExtractTextPlugin = require('extract-text-webpack-plugin') //var CopyWebpackPlugin = require('copy-webpack-plugin'); const METADATA = { host: '.' }; module.exports = Object.assign({}, commonWebpack, { //供html使用的变量 metadata: METADATA, plugins: [ //commonsPlugin 可以用于分析模块的共用代码, 单独打一个包出来 new webpack.optimize.CommonsChunkPlugin('js/common.js'), new webpack.HotModuleReplacementPlugin(), //将图片拷贝到指定位置 // new CopyWebpackPlugin([{ // from: 'src/assets/images', // to: 'images' // }]), // 提取css为单文件 //new ExtractTextPlugin("css/[name].css"), //压缩js代码 new webpack.optimize.UglifyJsPlugin({ output: { comments: false }, compress: { warnings: false } }), //React 官方提供的代码是已经合并的, 这个是 Webpack 不推荐的用法, //在合并话的代码上进行定制有点麻烦, Webpack 提供了设置环境变量来优化代码的方案: new webpack.DefinePlugin({ //配置组件中使用的变量,组件中可以直接使用{metadata.host} metadata:JSON.stringify(METADATA), "process.env": { NODE_ENV: JSON.stringify("production") } }) //concat进行数组合并 ].concat(helpers.getHtmlPlugin()) });</pre>

1.预备知识

这个课程的内容是用React写一个新闻网页,我们用npm来管理项目,且使用webpack模块打包机进行打包。

1.1 关于npm

npm通常称为node包管理器。顾名思义,它的主要功能就是管理node包,包括:安装、卸载、更新、查看、搜索、发布等;

在进行此项目之前,我有一定的npm基础,所以在做项目的过程中,没有遇到太多问题。想了解npm的小伙伴可以参考下面两篇文章:


主要回顾一下npm配置国内资源

npm是一个非常大的JavaScript包,但是在国外,所以由于网速的原因,我们使用npm进行安装的时候,有时会失败。

还好,淘宝做了一个npm的国内镜像。我们安装之后,使用cnpm install [xxx] 命令使用淘宝的镜像进行安装了。cnpm的安装和使用淘宝 NPM 镜像说的很清晰。

另一种方式是直接修改npm的配置项register,将npm所在的位置修改为淘宝的地址,Mac 的做法如下:在命令行中输入如下命令

cd ~ //进入根目录
atom .npmrc //用atom打开.npmrc文件

.npmrc文件中的内容修改如下:

registry = https://registry.npm.taobao.org

这样我们使用 npm install [xxx] 就是从淘宝镜像进行安装了。

注:关于npm配置国内资源,慕课老师写了一篇文章,讲的很是明白使用 CNPM 进行 Ionic 环境的安装与配置

1.2 关于webpack

webpack是一个强大的模块打包机,本项目中使用了ReactES6的语法,所以使用webpack是非常合适的呢!

webpack的内容还是很多的,比如说配置文件webpack.config.jsloader的配置等等;我在学习webpack的过程中,还是花了很多力气的。想学习webpack的小伙伴,我推荐下面这篇文章(真心讲的很好):


我们主要说说webpack的热加载的配置,这个功能真的很好用。

  1. 常规情况下,我们配置好webpack.config.js文件之后,使用webpack命令进行打包,然后手动刷新页面。就是说,我们在编写代码的过程中,要不断的执行webpack命令,再手动刷新,才能看到效果。

  2. 还有一种厉害一点的方法,webpack --watch 命令来动态监听文件的改变并实时打包,输出新 bundle.js 文件,但是还是要我们手动进行刷新。

  3. 最厉害的就是热加载, webpack-dev-server主要是启动了一个使用 expressHttp服务器 。它的作用主要是用来伺服资源文件 。此外这个 Http服务器 和 client 使用了 websocket 通讯协议,原始文件作出改动后, webpack-dev-server 会实时的编译,但是最后的编译的文件并没有输出到目标文件夹,实时编译后的文件都保存到了内存当中。因此使用webpack-dev-server进行开发的时候都看不到编译后的文件。

    • 首先安装 npm install webpack-dev-server

    • 终端输入命令 webpack-dev-server --inline

这样我们在浏览器输入 http://localhost:8080/ 就能看到我们编写的页面了,并能时时刷新。

需要注意的是:

  • webpack-dev-server并不能读取你的webpack.config.js的配置output,你在webpack.config.js里面的配置output属性是你用webpack打包时候才起作用的,对webpack-dev-server并不起作用

  • webpack-dev-server打包生产的文件并不会添加在你的项目目录中。你启动webpack-dev-server后,你在目标文件夹中是看不到编译后的文件的,实时编译后的文件都保存到了内存当中,它默认打包的文件名是bundle.js。

  • 因此在使用热加载时,我们引入的文件应该是webpack-dev-server打包生产的文件(引用bundle.js文件需要直接引用根目录下面的!)

    <script type="text/javascript" src="bundle.js"></script>
    

    这样就能享受时时刷新的待遇啦!

2.框架

项目中使用AntDesign框架来书写样式,AntDesign框架的很多组件很好用,比如:TabsFormCarouselModalMenuButton等;
AntDesign官网 是中文的,学习起来容易一些呢!

React项目的重点当然是React了,React的核心思想是数据驱动,我们从后台取到数据,再将取到的数据用 this.setState()进行保存,从而导致页面重新render。关于React我也在学习的过程中,下面的三篇文献是我最近正在学习的:


阮一峰大神曾说过:真正学会 React 是一个漫长的过程。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容