【问题标题】:SystemJS: loading build fileSystemJS:加载构建文件
【发布时间】:2016-09-27 02:56:58
【问题描述】:

我的 SystemJS 文件如下所示:

(function(global) {
  // map tells the System loader where to look for things
  var map = {
    'angular2-boot':              'app', // 'dist',
    '@angular':                   'node_modules/@angular',
    'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
    'rxjs':                       'node_modules/rxjs'
  };
  // packages tells the System loader how to load when no filename and/or no extension
  var packages = {
//    'app':                        { main: 'js/main.js',  defaultExtension: 'js' },
    'app':                        { main: 'dist/build.js',  defaultExtension: 'js' },
    'rxjs':                       { defaultExtension: 'js' },
    'angular2-in-memory-web-api': { defaultExtension: 'js' }
  };
  var ngPackageNames = [
    ...
  ];
  // Add package entries for angular packages
  ngPackageNames.forEach(function(pkgName) {
    packages['@angular/'+pkgName] = { main: pkgName + '.umd.js', defaultExtension: 'js' };
  });
  var config = {
    map: map,
    packages: packages
  };
  System.config(config);
})(this);

在此,我在 tsconfig.js 中使用 outDir 选项将我的打字稿转译到另一个目录中,并在使用 main: 'js/main.js' 的包中引用它,它工作正常。

但是,当我尝试使用 outFile 选项转换为单个文件并使用 main: 'dist/build.js' 引用该文件时。它不呈现页面,我在控制台中看不到任何错误。

我的 index.html 看起来像这样:

<html>
<head>
    <title>Angular 2 QuickStart</title>
    <base href="/">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="app/css/styles.css">
    <!-- 1. Load libraries -->
    <!-- Polyfill(s) for older browsers -->
    <script src="node_modules/core-js/client/shim.min.js"></script>
    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/reflect-metadata/Reflect.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <!-- 2. Configure SystemJS -->
    <script src="systemjs.config.js"></script>
    <script>
      System.import('angular2-boot').catch(function(err){ console.error(err); });
    </script>
</head>
<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app>
</body>
</html>

我的 tsconfig.js 文件如下所示:

{
  "compilerOptions": {
    "target": "es5",
    "module": "amd",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    "outDir": "app/js",
    "outFile": "app/dist/build.js"
  }
}

当我加载页面时,它显示正在加载...。要使用 SystemJS 加载单个输出转译文件,我还需要做什么?

【问题讨论】:

  • 查看浏览器中的网络选项卡,因为我怀疑文件未加载。
  • 我看到 build.js 正在加载。它有200个返回码,我也可以看到build.js源码。

标签: angular systemjs tsc


【解决方案1】:

当您转译为单个文件时,它会生成一个名为“bundle”的特殊格式的文件。当你导入它时,它里面的所有模块都会被加载并注册到 SystemJS 中,但是什么都没有执行。因此,加载包后,您需要在 index.html 中添加第二个导入,这将实际启动您的应用程序,如下所示:

    System.import('app/dist/build.js').then(function() {
        System.import('main');
    })
    .catch(function(err){ console.error(err); });

此代码使用显式路径加载包:app/dist/build.js,然后加载名为main 的模块,因为该名称必须与包中define 调用中给出的模块名称完全匹配。因此,此代码中的任何内容均未引用 systemjs.config.js 中定义的 angular-boot 或 app 包,因此无需在该文件中进行任何更改。

也可以加载一个带有普通&lt;script&gt;标签的包,然后你只需要一个System.import('main')。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    相关资源
    最近更新 更多