【问题标题】:Angular2 "Getting Started" deployment with SystemJS使用 SystemJS 的 Angular2“入门”部署
【发布时间】:2016-03-21 14:36:57
【问题描述】:

对于整个 SystemJS 来说有点新意,所以我可能会完全了解它文档中的重要部分。我对使用 Browserify 捆绑东西非常熟悉,但是在部署时,整个 SystemJS 让我摸不着头脑。没有双关语,因为我喜欢 SystemJS。

鉴于以下配置文件具有与 Angular2 5 分钟快速入门相同的配置:

<!-- 1. Load libraries -->
<!-- IE required polyfills, in this exact order -->
<script src="node_modules/es6-shim/es6-shim.min.js"></script>
<script src="node_modules/systemjs/dist/system-polyfills.js"></script>
<script src="node_modules/angular2/es6/dev/src/testing/shims_for_IE.js"></script>   

<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>

<!-- 2. Configure SystemJS -->
<script src="app.config.js"></script>
<script>
  System.import('app/main')
        .then(null, console.error.bind(console));
</script>

我的 Gulpfile 中使用 SystemJS Builder 的以下内容:

gulp.task('system-builder', function (cb) {
    var builder = new Builder('./production/public/', './production/public/app.config.js');
    fs.copy('./client/node_modules', './production/public/node_modules', function(err) {
        builder.bundle('./production/public/js/app/app.boot.js', './production/public/js/app/app.js')
        .then(function() {
            console.log('Build complete');
            cb();
        })
        .catch(function(err) {
            console.log('Build error');
            console.log(err);
        });
    });
});

我现在有一个 app.js 文件。回到 HTML 文件,如何在捆绑状态下使用应用程序代码?因为在进行以下更新时出现angular2-polyfills.js:322 Error: SyntaxError: Unexpected identifier 错误。请注意,我已将 app.config.js 替换为 /js/app/app.js:

<!-- 1. Load libraries -->
<!-- IE required polyfills, in this exact order -->
<script src="node_modules/es6-shim/es6-shim.min.js"></script>
<script src="node_modules/systemjs/dist/system-polyfills.js"></script>
<script src="node_modules/angular2/es6/dev/src/testing/shims_for_IE.js"></script>   

<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>

<!-- 2. Configure SystemJS -->
<script src="/js/app/app.js"></script>
<script>
    System.import('/js/app/app.boot').then(null, console.error.bind(console));
</script>

我在使用 JSPM 时看到了一百万次点击,但在决定使用更多库之前,我想知道 SystemJS 是如何本地处理这个问题的。

【问题讨论】:

    标签: gulp angular systemjs


    【解决方案1】:

    好的,想通了。

    基本上,您不需要对 html 文件执行任何操作,只需保持原样即可。

    但是,我需要使用 build.buildStatic 而不是 build.bundle 来使 SystemJS Builder 自动执行

    所以...以下工作:

    JS

    gulp.task('system-builder', function (cb) {
        var builder = new Builder('./production/public/', './production/public/app.config.js');
        fs.copy('./client/node_modules', './production/public/node_modules', function(err) {
            builder.buildStatic('./production/public/js/app/app.boot.js', './production/public/js/app/app.js')
            .then(function() {
                console.log('Build complete');
                cb();
            })
            .catch(function(err) {
                console.log('Build error');
                console.log(err);
            });
        });
    });
    

    HTML(保持在开发模式)

    <!-- 1. Load libraries -->
    <!-- IE required polyfills, in this exact order -->
    <script src="node_modules/es6-shim/es6-shim.min.js"></script>
    <script src="node_modules/systemjs/dist/system-polyfills.js"></script>
    <script src="node_modules/angular2/es6/dev/src/testing/shims_for_IE.js"></script>   
    
    <script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="node_modules/rxjs/bundles/Rx.js"></script>
    <script src="node_modules/angular2/bundles/angular2.dev.js"></script>
    
    <!-- 2. Configure SystemJS -->
    <script src="app.config.js"></script>
    <script>
      System.import('app/main')
            .then(null, console.error.bind(console));
    </script>
    

    【讨论】:

    • 嗨@Mitchell,关于您的代码只有 2 个问题; 1)您不应该在 HTML 中删除 app.config.js 的导入并改用 app.js 吗? 2) 关于 HTML 中的 node_modules 导入,您是否将这些文件夹/文件包含在部署文件夹中?还是用捆绑包就够了?
    • 嗨@david,是的,这篇文章是在测试阶段,现在有些事情已经改变了,那就是它在 RC4 中。由于我最近没有机会刷新自己,我想您的第一个问题可能不再适用,也可能不再适用。至于你的第二个问题,你确实必须在你的构建目录中包含这些 node_modules,但我一直在使用 gulp 将它们聚合到一个 libs.js 中。
    猜你喜欢
    • 1970-01-01
    • 2016-07-07
    • 2016-12-12
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    • 2016-10-09
    • 1970-01-01
    • 2016-10-21
    相关资源
    最近更新 更多