Angular(バージョン2、4、6、...)を本番用にライブのウェブサーバーにバンドルするには、どのような方法がありますか?
回答にはAngularのバージョンを含めてください。そうすれば、後続のリリースに移行したときに、よりよく追跡することができます。
npm install -g @angular/cli
を実行します。ng build --prod(ディレクトリが
projectFolder` のときにコマンドラインで実行)
prod` バンドルをproductionにするためのフラグを立てる(productionフラグに含まれるオプションのリストはAngular documentationを参照)。
以下のコマンドで、リソースをBrotli compressionで圧縮します。
for i in dist/*; do brotli $i; done
.
バンドルはデフォルトで projectFolder/dist(/$projectFolder for 6) に生成されます。
Angular 8.2.11
with CLI 8.3.13
and option CSS with Angular routingを使用した場合のサイズです。
dist/main-[es-version].[hash].js
あなたのアプリケーションがバンドルされています [ ES5 サイズ: 新しい Angular CLI アプリケーションが空の場合 188 KB, 44 KB 圧縮].dist/polyfill-[es-version].[hash].bundle.js
the polyfill dependencies (@angular, RxJS...) bundled [ ES5 size: 122 KB for new Angular CLI application empty, 36 KB compressed].dist/index.html
。dist/runtime-[es-version].[hash].bundle.js
webpack ローダー。dist/style.[hash].bundle.css
スタイル定義です。dist/assets
Angular CLIのアセット設定からコピーされたリソースです。ng serve --prod` コマンドを使うと、アプリケーションのプレビューを見ることができます。このコマンドはローカルの HTTP サーバーを起動し、プロダクションファイルを含むアプリケーションに http://localhost:4200 を使ってアクセスできるようにします。
本番環境で使用するには、dist
フォルダ内のすべてのファイルを任意のHTTPサーバにデプロイする必要があります。
バンドルは projectFolder / bundles / に生成されます。
bundles/dependencies.bundle.js
[ size: ~ 1 MB (as as small as possible) ] を出力します。bundles/app.bundle.js
[ size: depends on your project, mine is ~ 0.5 MB ] 。var gulp = require('gulp'),
tsc = require('gulp-typescript'),
Builder = require('systemjs-builder'),
inlineNg2Template = require('gulp-inline-ng2-template');
gulp.task('bundle', ['bundle-app', 'bundle-dependencies'], function(){});
gulp.task('inline-templates', function () {
return gulp.src('app/**/*.ts')
.pipe(inlineNg2Template({ useRelativePaths: true, indent: 0, removeLineBreaks: true}))
.pipe(tsc({
"target": "ES5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": true,
"noImplicitAny": false
}))
.pipe(gulp.dest('dist/app'));
});
gulp.task('bundle-app', ['inline-templates'], function() {
// optional constructor options
// sets the baseURL and loads the configuration file
var builder = new Builder('', 'dist-systemjs.config.js');
return builder
.bundle('dist/app/**/* - [@angular/**/*.js] - [rxjs/**/*.js]', 'bundles/app.bundle.js', { minify: true})
.then(function() {
console.log('Build complete');
})
.catch(function(err) {
console.log('Build error');
console.log(err);
});
});
gulp.task('bundle-dependencies', ['inline-templates'], function() {
// optional constructor options
// sets the baseURL and loads the configuration file
var builder = new Builder('', 'dist-systemjs.config.js');
return builder
.bundle('dist/app/**/*.js - [dist/app/**/*.js]', 'bundles/dependencies.bundle.js', { minify: true})
.then(function() {
console.log('Build complete');
})
.catch(function(err) {
console.log('Build error');
console.log(err);
});
});
{
"name": "angular2-quickstart",
"version": "1.0.0",
"scripts": {
***
"gulp": "gulp",
"rimraf": "rimraf",
"bundle": "gulp bundle",
"postbundle": "rimraf dist"
},
"license": "ISC",
"dependencies": {
***
},
"devDependencies": {
"rimraf": "^2.5.2",
"gulp": "^3.9.1",
"gulp-typescript": "2.13.6",
"gulp-inline-ng2-template": "2.0.1",
"systemjs-builder": "^0.15.16"
}
}
(function(global) {
// map tells the System loader where to look for things
var map = {
'app': 'app',
'rxjs': 'node_modules/rxjs',
'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
'@angular': 'node_modules/@angular'
};
// packages tells the System loader how to load when no filename and/or no extension
var packages = {
'app': { main: 'app/boot.js', defaultExtension: 'js' },
'rxjs': { defaultExtension: 'js' },
'angular2-in-memory-web-api': { defaultExtension: 'js' }
};
var packageNames = [
'@angular/common',
'@angular/compiler',
'@angular/core',
'@angular/forms',
'@angular/http',
'@angular/platform-browser',
'@angular/platform-browser-dynamic',
'@angular/router',
'@angular/router-deprecated',
'@angular/testing',
'@angular/upgrade',
];
// add package entries for angular packages in the form '@angular/common': { main: 'index.js', defaultExtension: 'js' }
packageNames.forEach(function(pkgName) {
packages[pkgName] = { main: 'index.js', defaultExtension: 'js' };
});
var config = {
map: map,
packages: packages
};
// filterSystemConfig - index.asp's chance to modify config before we register it.
if (global.filterSystemConfig) { global.filterSystemConfig(config); }
System.config(config);
})(this);
となっています。
var map = {
'app': 'dist/app',
};
dist-systemjs.config.js
タグを配置しても、プログラムは実行されますが、依存関係のあるバンドルは無視され、node_modules
フォルダから依存関係が読み込まれます。<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<base href="/"/>
<title>Angular</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<my-app>
loading...
</my-app>
<!-- Polyfill(s) for older browsers -->
<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.min.js"></script>
<script src="node_modules/reflect-metadata/Reflect.js"></script>
<script src="node_modules/systemjs/dist/system.js"></script>
<script src="dist-systemjs.config.js"></script>
<!-- Project Bundles. Note that these have to be loaded AFTER the systemjs.config script -->
<script src="bundles/dependencies.bundle.js"></script>
<script src="bundles/app.bundle.js"></script>
<script>
System.import('app/boot').catch(function (err) {
console.error(err);
});
</script>
</body>
</html>
今のところ最高の出来です :)
Angular.ioのクイックスタートチュートリアルがあります。Angular.ioにクイックスタートのチュートリアルがありますが、このチュートリアルを参考にして、簡単なgulpタスクを追加して、すべてをdistフォルダにバンドルして、サーバーにコピーするだけで動作するようにしました。Jenkis CIでうまく動作するように、node_modulesがキャッシュされ、コピーする必要がないように、すべてを最適化しました。
ソースコードとサンプルアプリはGithubにあります: https://github.com/Anjmao/angular2-production-workflow
1.typecriptsがコンパイルしたjsファイルとdistフォルダを削除 2.appフォルダ内のタイプスクリプトファイルをコンパイル 3.SystemJs bundler を使用して、ブラウザのキャッシュ更新用に生成されたハッシュを含むすべてのファイルを dist フォルダにバンドルします。 4.gulp-html-replaceを使用して、index.htmlのスクリプトをバンドルされたバージョンに置き換え、distフォルダにコピーします。 5.assetsフォルダ内のものをすべてdistフォルダにコピー
Node:しかし、私は angular-cli を使うことを強くお勧めします。なぜなら、必要なワークフローがすべて揃っていて、今では完璧に動作しているからです。というのも、必要なワークフローがすべて揃っていて、今では完璧に動作しているからです。私たちはすでに本番環境でこれを使用していますが、angular-cliにはまったく問題がありません。