M1X
M1X

Reputation: 5354

Angular 4 AOT Compiler

I want to set up the AOT compiler for my application

When I run node_modules/.bin/ngc -p tsconfig-aot.json it compiles successfully and gives no errors.

But when I try to run ng serve it gives me this error:

Tried to find bootstrap code, but could not. Specify either statically analyzable bootstrap code or pass in an entryModule to the plugins options.

Error: Tried to find bootstrap code, but could not. Specify either statically analyzable bootstrap code or pass in an entryModule to the plugins options.
    at Object.resolveEntryModuleFromMain (/path/node_modules/@ngtools/webpack/src/entry_resolver.js:128:11)
    at AotPlugin._setupOptions (/path/node_modules/@ngtools/webpack/src/plugin.js:143:50)
    at new AotPlugin (/path/node_modules/@ngtools/webpack/src/plugin.js:26:14)
    at _createAotPlugin (/path/node_modules/@angular/cli/models/webpack-configs/typescript.js:55:12)
    at Object.exports.getNonAotConfig (/path/node_modules/@angular/cli/models/webpack-configs/typescript.js:70:19)
    at NgCliWebpackConfig.buildConfig (/path/node_modules/@angular/cli/models/webpack-config.js:27:37)
    at Class.run (/path/node_modules/@angular/cli/tasks/serve.js:38:98)
    at check_port_1.checkPort.then.port (/path/node_modules/@angular/cli/commands/serve.js:110:26)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

main.ts

import { platformBrowser }    from '@angular/platform-browser';
import { AppModuleNgFactory } from '../aot/src/app/main/app.module.ngfactory';

console.log('Running AOT compiled');
platformBrowser().bootstrapModuleFactory(AppModuleNgFactory);

Upvotes: 1

Views: 906

Answers (1)

Max Koretskyi
Max Koretskyi

Reputation: 105439

node_modules/.bin/ngc -p tsconfig-aot.json compiles all files into output folder. You can use any http server to run files from that folder.

However this is not how it's usually done when developing with angular-cli. ng serve uses its own development server that doesn't care about output folder. As stated in the official docs:

ng serve builds the application and starts a web server.

... below are the additional options.

-- aot

So you need to use --aot option:

ng serve --aot

And you don't need to modify the main.ts file. It should have boostrapModule and not boostrapModuleFactory method.

Upvotes: 1

Related Questions