我已经按照https://github.com/systemjs/builder#example---common-bundles中的描述将几个js文件编译到一个SFX中(虽然使用'+‘而不是'&',但这部分似乎可以工作):
gulp.task('systemjs', ['copy-assets'], function(done){
return new Builder({
baseURL: '.',
// opt in to Babel for transpiling over Traceur
transpiler: 'traceur'
// etc. any SystemJS config
}).buildSFX('src/elements/my-test.js + src/elements/my-test2.js + src/elements/model/user.js', 'dist/elements.js')
.then(function() {
console.log('Build complete');
}).catch(function(err) {
console.log('Build error');
console.log(err);
});
});但是当我尝试导入生成的js文件时,我找不到任何lib:
<script src="./bower_components/system.js/dist/system-csp-production.js"></script>
<script>
System.import('elements.js').then(function(m) {
console.log(m);//never invoked
});
</script>任何帮助都是非常感谢的!
更新:
到目前为止,我找到的唯一解决方案是使用sfxGlobalName并创建“特殊的”js文件,该文件包含对要包含的所有其他文件的引用,然后将其包含到html中:
all.js:
import {MyTest} from 'src/elements/my-test.js';
export var myTest = MyTest;
import {MyTest2} from 'src/elements/my-test2.js';
export var myTest2 = MyTest2;index.html:
<script src="./bower_components/system.js/dist/system-polyfills.js"></script>
<script src="elements.js"></script>然后可以访问导入的对象,如下所示
elements.myTest一口吞下:
gulp.task('systemjs', ['copy-assets'], function(done){
return new Builder({
baseURL: '.',
// opt in to Babel for transpiling over Traceur
transpiler: 'traceur'
// etc. any SystemJS config
}).buildSFX('src/elements/all.js', 'dist/elements.js', { sfxGlobalName: 'elements', minify: false, sourceMaps:false })
.then(function() {
console.log('Build complete');
}).catch(function(err) {
console.log('Build error');
console.log(err);
});
});有没有更好的办法?
示例应用程序位于github上:
git clone https://github.com/bushuyev/test_test.git
cd test_test
git checkout tags/1.0
npm install
bower install
gulp wct发布于 2015-08-11 05:13:55
您不能通过System.import API导入sfx包,因为您尝试导入的模块已经编译,这意味着它是按照选择的格式(es6、cjs、amd)包装的。在尝试导入已编译的cjs文件时,您将看到相同的行为(例如,通过Browserify)。
为了能够导入模块,最好不要使用SFX将它们捆绑在一起。
https://stackoverflow.com/questions/31882316
复制相似问题