Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AOT (Ahead-Of-Time) Compilation with ES5

Is it possible to do AOT compilation in Angular using only ES5?

More to the point, can I use the NGTools Webpack plugin with ES5?

I know TypeScript is the preferable language of choice for Angular, however my place of employment did not approve using TypeScript for our Angular project. My hands are a bit tied, and I don't want performance to suffer on the client due to this.

Some relevant info on my project:

  • Webpack 2 for build/packaging
  • Written in ES2015 transpiled to ES5 using Babel

I have looked all over and have not been able to find a clear answer on this, I would really appreciate any info anyone can provide.

Thanks in advance!

like image 585
Matt Dempsey Avatar asked Jun 08 '17 16:06

Matt Dempsey


People also ask

Why would you use ahead of time AOT compilation?

The Angular ahead-of-time (AOT) compiler converts your Angular HTML and TypeScript code into efficient JavaScript code during the build phase before the browser downloads and runs that code. Compiling your application during the build process provides a faster rendering in the browser.

What is the difference between AOT compilation and JIT compilation?

Just-in-Time (JIT) is a type of compilation that compiles your app in the browser at runtime. Ahead-of-Time (AOT) is a type of compilation that compiles your app at build time.

Why Android uses ahead of time AOT rather than just-in-time JIT compilation?

Faster rendering — With AOT, the browser downloads a pre-compiled version of the application. The browser loads executable code so it can render the application immediately, without waiting to compile the app first.

What are the ways to control AOT compilation?

When you use the Angular AOT compiler, you can control your app compilation in two ways: By providing template compiler options in the tsconfig. json file. By specifying Angular metadata.


2 Answers

In you tsconfig.json file set the target to es5.

    "target": "es5",
like image 68
Praneeth Reddy Avatar answered Oct 03 '22 05:10

Praneeth Reddy


Angular is supposed to be fully ES5 compatible.

The AOT compiling process uses the metadata attached to components. This is how it finds the templates and styles that need compiling.

TypeScript is the preferred method for writing components. As it allows you to use the @Component annotation function to attach metadata .

ES6 is secondary method and Angular supports ES6 decorators to attach metadata to components.

ES5 is more basic. There isn't any automatic way to attach metadata, but the AOT compiler will look for a annotations array attached to the object's prototype.

This is how you would do it using ES5

HeroComponent.annotations = [
     new ng.core.Component({
        selector: 'hero-view',
        template: '<h1>{{title}}: {{getName()}}</h1>'
     })
];

So to answer your question. Yes, you can still use AOT with ES5.

like image 26
Reactgular Avatar answered Oct 03 '22 04:10

Reactgular