简体   繁体   English

Angular2项目,Js和HTML的捆绑和最小化

[英]Bundling and minification of angular2 project, Js and Html

Been working on an angular2 project for a while now, after updating it from angular1 (loving angular2 btw). 从angular1更新后(喜欢angular2 btw),现在已经在angular2项目上工作了一段时间。

Now i would like to bundle the project, but unsure what is the best bundler to use? 现在我想捆绑项目,但是不确定最好使用什么捆绑器? Also i see some posts saying that they split the bundle into multiple files (like app.bundle.js and vendors.bundle.js) and others have 1 large file. 我也看到一些帖子说他们将捆绑包分成多个文件(例如app.bundle.js和vendor.bundle.js),另一些则有1个大文件。 What is the best method? 最好的方法是什么? I always throught that many files were better because browsers can download multiple files at the same time? 我总是说很多文件更好,因为浏览器可以同时下载多个文件? Do you need to use a gulp task for all this, or something else completely? 您是否需要为此使用gulp任务,或者完全使用其他任务?

Also, how can i minify the HTML templates in an angular2 app? 另外,如何缩小angular2应用程序中的HTML模板?

You can either use Webpack or Systemjs-Builder to bundle your app. 您可以使用Webpack或Systemjs-Builder捆绑您的应用程序。 There are tons of seed projects from which you can take hints eg this and this and you can use this in your gulp task to inline the templates with your build. 有很多种子项目,您可以从中获得一些提示,例如thisthis,并且您可以在gulp任务中使用this来将模板内联到构建中。

Now i would like to bundle the project, but unsure what is the best bundler to use? 现在我想捆绑项目,但是不确定最好使用什么捆绑器?

All answers to your questions will be subjective, but it is factual that Webpack offers plugins and configuration settings to suit your needs. 您对问题的所有回答都是主观的,但Webpack确实会提供适合您需要的插件和配置设置。

Also i see some posts saying that they split the bundle into multiple files (like app.bundle.js and vendors.bundle.js) and others have 1 large file. 我也看到一些帖子说他们将捆绑包分成多个文件(例如app.bundle.js和vendor.bundle.js),另一些则有1个大文件。 What is the best method? 最好的方法是什么?

Splitting into multiple files is definitely the most common method. 拆分为多个文件绝对是最常见的方法。 It allows you to keep your application code separated from your dependencies, which makes debugging less of a nightmare. 它使您可以将应用程序代码与依赖项分开,从而减少调试的噩梦。

The part of your Webpack config file that handles this might look something like this: Webpack配置文件中处理此问题的部分可能看起来像这样:

  // our angular app
  entry: { 'vendor': './src/vendor.ts', 'main': './src/main.ts' },

  // Config for our build files
  output: {
    path: '',
    filename: '[name].bundle.js',
    sourceMapFilename: '[name].map',
    chunkFilename: '[id].chunk.js'
  },

Do you need to use a gulp task for all this, or something else completely? 您是否需要为此使用gulp任务,或者完全使用其他任务?

If you use Webpack, then Gulp is not required at all. 如果您使用Webpack,则根本不需要Gulp。 I'd recommend just using NPM scripts. 我建议只使用NPM脚本。 Don't quote me on this, but I think Webpack can just flat out replace Gulp for Angular2 projects. 不要在此引用我,但我认为Webpack可以完全替代Angular2项目的Gulp。

Also, how can i minify the HTML templates in an angular2 app? 另外,如何缩小angular2应用程序中的HTML模板?

Webpack has a HTML minify plugin for this. Webpack为此提供了一个HTML minify 插件

You can use it like so: 您可以这样使用它:

loaders: [
        {
                test: /\.html$/,
                name: "mandrillTemplates",
                loader: 'raw!html-minify' // raw is another loader
        }
    ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM