# 一、前言
小程序给我们暴露了两个参数
require和module,require用来在模块中加载其他模块,module用来将模块中的方法暴露出去
module.exports = function(){}
所以只要需要让第三方库的代码使用这种形式的
export就可以了
# 二、构建Redux的微信小程序包
打一个
Redux包,让它可以兼容微信小城的加载方式
git clone https://github.com/reactjs/redux.git
npm install
# 详细内容可以到redux项目的package.json中查看
# 这些命令是是使用webpack构建UMD模式的包。也就是说所有的代码,包括依赖的库都会被打包到一个文件中,并且自带一段模块加载代码,文件可以在dist目录下找到
npm run build:umd && npm run build:umd
用编辑器打开
dist目录下的redux.js文件
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["Redux"] = factory();
else
root["Redux"] = factory();
})(this, function() {
...
})
- 这段代码是用来加载模块的,里面的factory函数的返回的内容是用webpack提供的loader组织起来的redux的代码和第三方依赖。
- 如果我们把这个文件拷贝到小程序中,只需要让程序能正常进入第三行代码,就能把Redux加载进来
- 将第二行代码:
if(typeof exports === 'object' && typeof module === 'object')修改成:if(typeof module === 'object') - 这样修改的原因是,在微信小程序的环境中是没有exports变量的,所以就没办法正确进入这个分支,删除之后就可以正确进入
- 我们拷贝到
libs目录下,那么我们在程序中使用时,只要当做是一个本地模块去require就可以了var redux = require('./libs/redux.js') - 我们可以通过类似的方法,使用
Webpack打包第三方库,就可以集成任何库了
# 三、集成Redux-devtools
因为微信小程序的开发环境是定制的,暂时没有发现办法直接安装
redux-devtool的插件
安装remote-redux-devtools
- 原版的
remote-redux-devtools使用的一个websocket的依赖会使用原生的WebSocket,小程序是不支持的,所以需要改成小程序的websocket实现,修改好的代码 https://github.com/poetries/wx-redux-immutable-template/blob/master/wx-redux-immutable-template/public/libs/remote-redux-devtools.js - 把代码下载到工程目录里面就可以用了
安装和启动remotedev-server
npm install -g remotedev-server
remotedev --hostname=localhost --port=5678
因为没办法用
npm安装到本地(微信小程序会尝试去加载项目目录中的所有js),所以这里使用全局安装,第二条命令是启动remotedev-server,hostname和port分别指定为localhost和5678
集成devtool
在
store下集成devtool
const {createStore, compose} = require('./libs/redux.js');
const devTools = require('./libs/remote-redux-devtools.js').default;
const reducer = require('./reducers/index.js')
function configureStore() {
return createStore(reducer, compose(devTools({
hostname: 'localhost',
port: 5678,
secure: false
})));
}
module.exports = configureStore;
把
devtool使用redux的compose加到store中去。hostname和port是指定为之前启动remotedev-server启动时候指定的参数。保存之后重启一下小程序,如果没有报错的话就OK了
- 可以在浏览器中访问
localhost:5678
# 四、小程序中集成immutable
Immutable是Immutable.js,下面就来说说微信小程序如何使用第三方库Immutable.js
Immutable使用了UMD模块化规范
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.Immutable = factory());
}(this, function () { 'use strict';var SLICE$0 = Array.prototype.slice;
....
}));
修改
Immutable代码,注释原有模块导出语句,使用module.exports = factory()强制导出
(function(global, factory) {
/*
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.Immutable = factory());
*/
module.exports = factory();
}(this, function() {
导入修改好的
immutable到小程序中即可 https://github.com/poetries/wx-redux-immutable-template/blob/master/wx-redux-immutable-template/public/libs/immutable.js