【问题标题】:How to correctly bundle an Angular2-JS/Typescript app via Gulp?如何通过 Gulp 正确捆绑 Angular2-JS/Typescript 应用程序?
【发布时间】:2016-11-17 12:15:00
【问题描述】:

注意:我知道有几个类似的问题(这里提到的存储库来自其中之一)但我仍然不明白什么不起作用

具体来说,我尝试在以下位置复制简单应用程序的逻辑:https://github.com/templth/angular2-packaging

问题是(见下面我的 Gulp 配置文件)我在 ./dist 中只得到 3 个文件:

  • app.min.js 包含: var __decorate = (this && this.__decorate) || ...
  • index.html
  • vendors.ts

发生的情况是应用程序只显示“正在加载...”,没有错误,如果我转到“源”(Chrome 开发工具)我看到 没有真正加载(除了 3我在 ./dist 目录中看到的文件)

我也试过了:

  • 绕过 Typescript 并捆绑 JS 文件,但结果是所有文件都在那里,但我明白了
  • 不丑化

require 未定义

我的 Gulp 配置文件

'use strict';

const gulp = require('gulp');
const ts = require('gulp-typescript');
const uglify = require('gulp-uglify');
const concat = require('gulp-concat');
var htmlreplace = require('gulp-html-replace');
var addsrc = require('gulp-add-src');

gulp.task('app-bundle', function () {
    var tsProject = ts.createProject('tsconfig.json', {
        typescript: require('typescript'),
        outFile: 'app.js'
    });

    var tsResult = gulp.src([
        'node_modules/**/*.ts.d',
        'typings/**/*.ts.d',
        'app/**/*.ts'
    ]).pipe(ts(tsProject));

    return tsResult.js
        .pipe(addsrc.append('config-prod.js'))
        .pipe(concat('app.min.js'))
        .pipe(uglify())
        .pipe(gulp.dest('./dist'));
});

gulp.task('vendor-bundle', function() {
    gulp.src([
        'node_modules/core-js/client/shim.min.js',
        'node_modules/zone.js/dist/zone.js',
        'node_modules/reflect-metadata/Reflect.js',
        'node_modules/systemjs/dist/system.src.js'
    ])
        .pipe(concat('vendors.min.js'))
        .pipe(uglify())
        .pipe(gulp.dest('./dist'));
});

gulp.task('html-replace',[ 'app-bundle', 'vendor-bundle' ], function() {
    gulp.src('index.html')
        .pipe(htmlreplace({
            'vendor': 'vendors.min.js',
            'app': 'app.min.js'
        }))
        .pipe(gulp.dest('dist'));
});

// define which tasks should Gulp launch
gulp.task('default', ['app-bundle', 'vendor-bundle','html-replace']);

【问题讨论】:

    标签: angular gulp production


    【解决方案1】:

    您应该使用名为“systemjs builder”的特殊 gulp 模块。 此外,您应该使用 tsconfig.js 中的编译参数作为“commonjs”。 此外,它应该是该文件中包含的 npm 引用。 所有这些都应该可以解决您的问题。

    【讨论】:

    • @thanks Artem 提供详细的答案。更改为 commonjs 不会破坏应用程序吗?此外,我并没有真正得到“应该是该文件中包含的 npm 引用......”。你说谁?
    • 我收到了你的最后一条评论,问题是我还有一个指定依赖项的类型。甚至不是初始的(只是在没有进一步更改的情况下绑定命令之后)。真的有必要在 tsconfig.json 文件中指定它们吗?
    • 实际上我发现了这个github.com/templth/angular2-packaging(它确实使用了systemjs builder)。我仍然有同样的问题,但我会弄清楚的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    • 1970-01-01
    相关资源
    最近更新 更多