gpt4 book ai didi

typescript - 我如何实际部署 Angular 2 + Typescript + systemjs 应用程序?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:43:05 24 4
gpt4 key购买 nike

在 angular.io 上有一个使用 typescript 和 systemjs 的快速入门教程。现在我已经运行了那个 miniapp,我将如何着手创建可部署的东西?我找不到任何关于它的信息。

我是否需要任何额外的工具,System.config 中的任何额外设置?

(我知道我可以使用 webpack 并创建一个 bundle.js,但我想使用教程中使用的 systemjs)

有人可以分享他们使用此设置(Angular 2、TypeScript、systemjs)的构建过程

最佳答案

在这个级别要理解的关键是使用下面的配置,你不能直接连接编译后的 JS 文件。

在 TypeScript 编译器配置中:

{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"declaration": false,
"stripInternal": true,
"module": "system",
"moduleResolution": "node",
"noEmitOnError": false,
"rootDir": ".",
"inlineSourceMap": true,
"inlineSources": true,
"target": "es5"
},
"exclude": [
"node_modules"
]
}

在 HTML 中

System.config({
packages: {
app: {
defaultExtension: 'js',
format: 'register'
}
}
});

事实上,这些 JS 文件将包含匿名模块。匿名模块是一个使用 System.register 但没有模块名称作为第一个参数的 JS 文件。这是当 systemjs 配置为模块管理器时,typescript 编译器默认生成的内容。

因此,要将所有模块放入单个 JS 文件,您需要在 TypeScript 编译器配置中利用 outFile 属性。

您可以在 gulp 中使用以下内容来执行此操作:

const gulp = require('gulp');
const ts = require('gulp-typescript');

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

gulp.task('tscompile', function () {
var tsResult = gulp.src('./app/**/*.ts')
.pipe(ts(tsProject));

return tsResult.js.pipe(gulp.dest('./dist'));
});

这可以与其他一些处理相结合:

  • 丑化已编译的 TypeScript 文件
  • 创建一个app.js文件
  • 为第三方库创建一个vendor.js文件
  • 创建一个 boot.js 文件来导入引导应用程序的模块。此文件必须包含在页面末尾(当加载所有页面时)。
  • 更新 index.html 以考虑这两个文件

gulp 任务中使用了以下依赖项:

  • 一饮而尽
  • gulp-html-替换
  • gulp-typescript
  • gulp-uglify

以下是一个示例,因此可以对其进行调整。

  • 创建app.min.js文件

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

    var tsResult = gulp.src('app/**/*.ts')
    .pipe(ts(tsProject));

    return tsResult.js.pipe(concat('app.min.js'))
    .pipe(uglify())
    .pipe(gulp.dest('./dist'));
    });
  • 创建vendors.min.js文件

    gulp.task('vendor-bundle', function() {
    gulp.src([
    'node_modules/es6-shim/es6-shim.min.js',
    'node_modules/systemjs/dist/system-polyfills.js',
    'node_modules/angular2/bundles/angular2-polyfills.js',
    'node_modules/systemjs/dist/system.src.js',
    'node_modules/rxjs/bundles/Rx.js',
    'node_modules/angular2/bundles/angular2.dev.js',
    'node_modules/angular2/bundles/http.dev.js'
    ])
    .pipe(concat('vendors.min.js'))
    .pipe(uglify())
    .pipe(gulp.dest('./dist'));
    });
  • 创建boot.min.js文件

    gulp.task('boot-bundle', function() {
    gulp.src('config.prod.js')
    .pipe(concat('boot.min.js'))
    .pipe(uglify())
    .pipe(gulp.dest('./dist'));
    });

    config.prod.js 仅包含以下内容:

     System.import('boot')
    .then(null, console.error.bind(console));
  • 更新index.html文件

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

    index.html 如下所示:

    <html>
    <head>
    <!-- Some CSS -->

    <!-- build:vendor -->
    <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/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>
    <script src="node_modules/angular2/bundles/http.dev.js"></script>
    <!-- endbuild -->

    <!-- build:app -->
    <script src="config.js"></script>
    <!-- endbuild -->
    </head>

    <body>
    <my-app>Loading...</my-app>

    <!-- build:boot -->
    <!-- endbuild -->
    </body>
    </html>

请注意,System.import('boot'); 必须在主体的末尾完成,以等待您的所有应用程序组件从 app.min 注册。 js 文件。

我不在这里描述处理 CSS 和 HTML 缩小的方法。

关于typescript - 我如何实际部署 Angular 2 + Typescript + systemjs 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40758468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com