How to import system js SFX library

我的未来我决定 提交于 2019-12-07 14:32:34

问题


I've compiled several js files into one SFX as described in https://github.com/systemjs/builder#example---common-bundles (although using '+' instead of '&', but this part seems to be working):

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);
    });

});

but when i'm trying to import resulting js file I can't find any 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>

any help is highly appreciated!

UPDATE:

The only solution I found so far was to use sfxGlobalName and create 'special' js file containing references to all other files to be included and then include it into 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>

then imported objects can be accessed like

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);
    });

});

Is there any better way?

The sample app is on github:

git clone https://github.com/bushuyev/test_test.git
cd test_test
git checkout tags/1.0
npm install
bower install
gulp wct

回答1:


You can not import the sfx bundle via System.import API as the module you are trying to import is already compiled, meaning it is wrapped according to the chosen format (es6, cjs, amd). Same behavior you will see when trying to import a compiled cjs file (eg. Through Browserify).

To be able to import the modules, you should better bundle them whithout SFX.



来源:https://stackoverflow.com/questions/31882316/how-to-import-system-js-sfx-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!