为什么 Angular build 使用 'es5' 和 'es2015' 而不是 'es6' (或者根本没有后缀)创建文件?

Why does Angular build create files with 'es5' and 'es2015' but not 'es6' (or no suffix at all)?

我最近下载了 Angular CLI (@angular/cli 9.0.1)。然后我继续创建一个新的应用程序,以便我可以创建一个新的 Angular 元素,将其打包并在另一个应用程序中使用它。

在关注了一些博客之后,我遇到的每个博客的最后一步都在谈论从 dist/ 文件夹下生成的文件创建单个 JS 文件。例如:https://blog.bitsrc.io/using-angular-elements-why-and-how-part-1-35f7fd4f0457

Then using the cat command, we are concatenating the runtime.js, polyfills.js, scripts.js, and main.js files from the dist/angular-app folder into a angularapp.js file inside the preview folder.

运行 ng build angular-app --prod --output-hashing=none 似乎生成的文件名为:

我搜索了每个包含术语 es5es2015 的文件,并将其更改为 es6,但它仍然产生相同的 es5es2015 文件名。我在这里做错了什么?

他们是the same thing

第 6 版 - ECMAScript 2015

  • 6th Edition ECMAScript -> ES6
  • ECMAS脚本2015 -> ES2015

同一事物的不同名称。 Angular 只是选择将其命名为 es2015。

ES2015 与 ES6 相同。 ECMAScript 6 于 2015 年发布。

一些历史:

https://codeburst.io/javascript-wtf-is-es6-es8-es-2017-ecmascript-dca859e4821c

Angular 不会将 JavaScript 个文件捆绑到一个文件中。

您可以添加构建步骤以将文件连接在一起。

concat-build.js:

var concat = require('concat');
const es5 = ['./dist/app/runtime-es5.js','./dist/app/polyfills-es5.js','./dist/app/main-es5.js'];
const es2015= ['./dist/app/runtime-es2015.js','./dist/app/polyfills-es2015.js','./dist/app/main-es2015.js'];
concat(es5, './dist/app/elements-es5.js');
concat(es2015, './dist/app/elements-es2015.js');

package.json:

"scripts": {
   "concat": "node ./concat-builds.js",
   "build": "ng build --prod --output-hashing=none && npm run concat"
}

不要对 ES5 和 ES2015 构建感到困惑,因为 Angular 团队根据模块的加载方式拆分包(不特定于 JavaScript 版本)。

支持模块的Web浏览器将加载ES2015版本而不是ES5版本,但两者建议在Html.

如果您只想使用单个文件,那么您必须使用旧的 ES5 版本,并且应该按如下方式提供:

<script src="elements-es5.js">

建议按如下方式提供这两个文件,浏览器会加载合适的版本:

<script src="elements-es5.js" nomodule defer>
<script src="elements-es2015.js" type="module">

请注意:

Older browsers will ignore the type="module" version, and newer browsers will skip the nomodule version.