How to consume TypeScript module:system export with systemjs in browser

流过昼夜 提交于 2020-01-04 12:23:10

问题


I’m trying to use systemjs, and I have exported this code with tsc: https://github.com/quantumjs/solar-popup/blob/master/package.json#L10

{
  "compilerOptions": {
    "module": "system",
    "target": "es5",
    "sourceMap": true,
    "outFile":"./build/solar-popup.js",
    "sourceRoot": "./src/SolarPopup.ts",
    "rootDir": "./src/"

  },
  "exclude": [
    "node_modules"
  ]
}

However then trying to consume it in the browser the imported object doesn't contain any of the exports. I think this is due to the solar-popup.js not containing any exports, just System.register calls

The export looks like this:

System.register("SolarPopup", ["ModalBackground", "constants"], function (exports_4, context_4) {
    "use strict";
    var __moduleName = context_4 && context_4.id;
    var ModalBackground_1, constants_2, SolarPopup;
    return {
        setters: [
            function (ModalBackground_1_1) {
                ModalBackground_1 = ModalBackground_1_1;
            },
            function (constants_2_1) {
                constants_2 = constants_2_1;

etc


回答1:


When you have these two options in tsconfig.json

"module": "system",
"outFile":"./build/solar-popup.js",

TypeScript generates single output file that contains several System.register calls, each registering a module with its own name:

 System.register("SolarPopup", ["ModalBackground", "constants"], function ...

"SolarPopup" is a module name here. SystemJS interprets such file as a bundle, not a module.

The result of importing a bundle is an empty object, and a side effect is that all modules inside are registered and are available immediately for importing without need to fetch them.

So you need an additional import to get a module from the bundle. Try this:

System.import('../build/solar-popup.js').then(function() {
    System.import('SolarPopup').then(function(SolarPopup) {
        console.dir(SolarPopup);
    });
});


来源:https://stackoverflow.com/questions/41161080/how-to-consume-typescript-modulesystem-export-with-systemjs-in-browser

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