reactjs打包问题
引用旧js模块时,无法build成功:
./src/utils/roslib.js
Line 420:2: Expected an assignment or function call and instead saw an expression no-unused-expressions
Line 1175:39: 'define' is not defined no-undef
Line 1177:5: 'define' is not defined no-undef
Line 1584:62: Unexpected use of 'self' no-restricted-globals
Line 1584:85: Unexpected use of 'self' no-restricted-globals
Line 2300:26: 'io' is not defined no-undef
Line 3106:12: 'bson' is not defined
所以下载源码后决定重新打包一次。
包括roslib、ros2d都是用CommonJS打包的。
解决方案
1、ros2d使用模块化写法,代码打包或者整理后,可以在文件末尾增添:
export default ROS2D
当然同时也要注意修改其中代码使其符合es6规范,可以根据报错提示,在未声明的初次变量前面添加var
等等。
工程中引用时:
import ROS2D from '../lib/ros2d.js'
2、roslib使用了commonJS规范,没有办法简单改为export形式。所以在npm下载后,或者下载好源码整理进入工程以后,使用require方法引用:
var ROSLIB = require('roslib');
请注意除了react工程,原本的ros2d文件中也要这么写。而且所有的require语句必须写在import的语句的下方。
3、同时要注意其他模块的引用。
createjs无法使用import或者require的方式引用。
首先下载别人改好的模块:npm install createjs-cmd
,直接引入即可/
import createjs from 'createjs-cmd';
注意
每个js文件一创建,都有一个
var exports = module.exports = {}
,使exports和module.exports都指向一个空对象。 module.exports和exports所指向的内存地址相同。