如何导入系统js SFX库

2024-04-24

我已将多个 js 文件编译为一个 SFX,如中所述https://github.com/systemjs/builder#example---common-bundles https://github.com/systemjs/builder#example---common-bundles(虽然使用“+”而不是“and”,但这部分似乎有效):

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 文件时,我找不到任何库:

<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>

非常感谢任何帮助!

UPDATE:

到目前为止我发现的唯一解决方案是使用 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;

索引.html:

 <script src="./bower_components/system.js/dist/system-polyfills.js"></script>

<script src="elements.js"></script>

然后可以像这样访问导入的对象

elements.myTest

gulp:

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

您无法通过 System.import API 导入 sfx 包,因为您尝试导入的模块已经编译,这意味着它是根据所选格式(es6、cjs、amd)进行包装的。当尝试导入编译的 cjs 文件时(例如通过 Browserify),您会看到相同的行为。

为了能够导入模块,您应该最好在没有 SFX 的情况下捆绑它们。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何导入系统js SFX库 的相关文章

随机推荐