fis中mod.js与localForage中的require冲突问题
mod.js 使用版本: /**
- file: mod.js
- ver: 1.0.8
- update: 2014/11/7 *
- https://github.com/zjcqoo/mod */
localforage 使用版本: /*! localForage -- Offline Storage, Improved Version 1.2.6 https://mozilla.github.io/localForage (c) 2013-2015 Mozilla, Apache License 2.0 */
问题,在测试localforage示例代码,假设直接使用其example/index.html功能正常,但放到fis框架中报错。测试的js代码为: // Forcing localstorage here. Feel free to switch to other drivers :) localforage.setDriver(localforage.LOCALSTORAGE).then(function() { var key = 'STORE_KEY'; // var value = 'What we save offline'; var value = new Uint8Array(8); value[0] = 65 // var value = undefined; var UNKNOWN_KEY = 'unknown_key';
localforage.setItem(key, value, function() {
console.log('Saved: ' + value);
localforage.getItem(key, function(err, readValue) {
console.log('Read: ', readValue);
});
// Since this key hasn't been set yet, we'll get a null value
localforage.getItem(UNKNOWN_KEY, function(err, readValue) {
console.log('Result of reading ' + UNKNOWN_KEY, readValue);
});
});
});
报错内容:
Uncaught (in promise) [ModJS] Cannot find module ./drivers/localstorage
对应抛异常的是mod.js 下97行,代码如下:
//
// init module
//
var factory = factoryMap[id];
if (!factory) {
throw '[ModJS] Cannot find module ' + id + '';
}
错误原因: localforage 本身也有一个require('./drivers/localstorage')函数,与mod.js中的require冲突。并被mod.js中的require覆盖,导致报错
localforage是mozilla的开源项目,使用的人还挺多,希望能帮助解决下。
你使用这个库是 AMD 规范的,而 mod.js 不是,就像大部分的前端模块化框架一样,都占用了 require、define 这些全局函数,所以是互斥的。这并不是哪个有问题或者哪个没问题,只是不合适。
如果你想要在 mod.js 下使用你这个库就得改源码来适应。如果不想改你就得使用支持 AMD 规范的 require.js 来代替 mod.js。
但前提是你需要修改你现在的项目使用 AMD 规范。具体规范参考规范文档。并且使用 FIS 团队提供的 AMD 解决方案,参考 https://github.com/fex-team/fis-amd-demo
另外,前端模块化框架很多,每个都会影响到编程习惯,所以需要谨慎选择。
ok 谢谢,暂选d3