How to import js SFX system library

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

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 try to import the resulting js file, I cannot find the 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 appreciated!

UPDATE:

The only solution I have found so far has been to use sfxGlobalName and create a "special" js file containing links to all other files to be included and then include it in the 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 the 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 a better way?

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

      

+3


source to share


1 answer


You cannot import the sfx package via the System.import API, as the module you are trying to import is already compiled, meaning it is wrapped according to the format of your choice (es6, cjs, amd). You will see this behavior when trying to import a compiled cjs file (for example via Browserify).



In order to be able to import modules, it is better to bundle them with SFX.

+1


source







All Articles