如何编译捆绑包中的一些Typescript文件并使用SystemJS加载它?

如何编译捆绑包中的一些Typescript文件并使用SystemJS加载它?,typescript,systemjs,gulp-typescript,Typescript,Systemjs,Gulp Typescript,我在TypeScript中有两个文件(稍后我将创建更多),我想生成一个唯一的JS文件,其他人将使用它作为库 此时,通过将两个文件连接起来并使用SystemJS将它们加载到HTML文件的最简单的练习,我得到了以下错误: Error: (SystemJS) Multiple anonymous defines in module Error: (SystemJS) Multiple anonymous defines in module Error: (SystemJS) Multiple

我在TypeScript中有两个文件(稍后我将创建更多),我想生成一个唯一的JS文件,其他人将使用它作为库

此时,通过将两个文件连接起来并使用SystemJS将它们加载到HTML文件的最简单的练习,我得到了以下错误:

Error: (SystemJS) Multiple anonymous defines in module 
Error: (SystemJS) Multiple anonymous defines in module 
Error: (SystemJS) Multiple anonymous System.register
If loading a bundle, ensure all the System.register calls are named.
我使用的是TypeScript v1.8和SystemJS v0.19.38。 我有以下两个文件:

Utils.ts 梅因酒店 tsconfig.json 我正在使用GulpTypeScript编译并生成一个独特的JS文件。 并生成此文件:

Main.js 最后,在我的HTML中,我加载了以下内容:

<head>    
<script src="MyLibv1.0/vendor/system.js"></script>
 <script>
    System.config({
         baseURL: "MyLibv1.0/"
    });
    System.defaultJSExtensions = true;
    System.import("Main").catch(function(e)
    {
        console.error(e);
    });
</script>
...
如果我在tsconfig.json中从

“模块”:“amd”

致:

“模块”:“系统”

然后在Chrome中,我得到了这个错误:

Error: (SystemJS) Multiple anonymous defines in module 
Error: (SystemJS) Multiple anonymous defines in module 
Error: (SystemJS) Multiple anonymous System.register
If loading a bundle, ensure all the System.register calls are named.

为什么??我做错了什么?

您应该使用SystemJS Builder进行此操作。 创建捆绑包作为构建管道的一部分,然后在浏览器中加载捆绑包

var path = require("path");
var Builder = require('systemjs-builder');

// optional constructor options 
// sets the baseURL and loads the configuration file 
var builder = new Builder('path/to/baseURL', 'path/to/system/config-file.js');

builder
.bundle('local/module.js', 'outfile.js')
.then(function() {
  console.log('Build complete');
})
.catch(function(err) {
  console.log('Build error');
  console.log(err);
});

您应该使用SystemJS Builder进行此操作。 创建捆绑包作为构建管道的一部分,然后在浏览器中加载捆绑包

var path = require("path");
var Builder = require('systemjs-builder');

// optional constructor options 
// sets the baseURL and loads the configuration file 
var builder = new Builder('path/to/baseURL', 'path/to/system/config-file.js');

builder
.bundle('local/module.js', 'outfile.js')
.then(function() {
  console.log('Build complete');
})
.catch(function(err) {
  console.log('Build error');
  console.log(err);
});

串联模块文件不会产生可由SystemJS加载的有效捆绑包。一种可能的方法是在tsconfig.json中设置
outFile
,而不是
outDir
——参见Ah super@artem。。现在我没有错误了。但是在我的Module.ts中有一个console.log,它不工作。有什么想法吗?那么这似乎和谢谢@artem的问题一样。我添加了一个带有FUNCTION的导出类,并调用了两次,但没有查看。您知道从SystemJS加载函数的示例吗?包含使用gulp的完整示例,也发布于。在该示例中,启动应用程序的导入是。您不能真正“加载函数”,您总是加载一个模块,然后您可以调用该模块导出的函数。串联模块文件不会生成可由SystemJS加载的有效捆绑包。一种可能的方法是在tsconfig.json中设置
outFile
,而不是
outDir
——参见Ah super@artem。。现在我没有错误了。但是在我的Module.ts中有一个console.log,它不工作。有什么想法吗?那么这似乎和谢谢@artem的问题一样。我添加了一个带有FUNCTION的导出类,并调用了两次,但没有查看。您知道从SystemJS加载函数的示例吗?包含使用gulp的完整示例,也发布于。在该示例中,启动应用程序的导入是。你不能真的“加载一个函数”,你总是加载一个模块,然后你可以调用一个由该模块导出的函数。