Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug(webpack): Webpack do not find bootstrap code if app is bootstrapped asynchronously #3540

Closed
cladera opened this issue Dec 13, 2016 · 50 comments
Labels
P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent type: bug/fix

Comments

@cladera
Copy link
Contributor

cladera commented Dec 13, 2016

OS?

Mac OSX (Sierra)

Versions.

angular-cli: 1.0.0-beta.22-1
node: 6.5.0
os: darwin x64

Repro steps.

  1. Configure i18n according to angular's cookbook
  2. run ng serve

The log given by the failure.

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 (/Users/cgcladera/Projects/ws-v4/webcastudio-wms/node_modules/@ngtools/webpack/src/entry_resolver.js:121:15)
    at AotPlugin._setupOptions (/Users/cgcladera/Projects/ws-v4/webcastudio-wms/node_modules/@ngtools/webpack/src/plugin.js:129:58)
    at new AotPlugin (/Users/cgcladera/Projects/ws-v4/webcastudio-wms/node_modules/@ngtools/webpack/src/plugin.js:37:14)
    at Object.exports.getWebpackNonAotConfigPartial (/Users/cgcladera/Projects/ws-v4/webcastudio-wms/node_modules/angular-cli/models/webpack-build-typescript.js:20:13)
    at new NgCliWebpackConfig (/Users/cgcladera/Projects/ws-v4/webcastudio-wms/node_modules/angular-cli/models/webpack-config.js:23:42)
    at Class.run (/Users/cgcladera/Projects/ws-v4/webcastudio-wms/node_modules/angular-cli/tasks/serve-webpack.js:20:22)
    at /Users/cgcladera/Projects/ws-v4/webcastudio-wms/node_modules/angular-cli/commands/serve.js:102:26
    at process._tickCallback (internal/process/next_tick.js:103:7)

Mention any other details that might be useful.

I'm having this issue since I upgraded my project from beta.18 to beta.22.

After following i18n documentation your main.ts gets like this:

import './polyfills.ts';

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import {
  enableProdMode
} from '@angular/core';
import { AppModule } from './app/';
import {environment} from './environments/environment';
import {getTranslationProviders} from './app/i18n-providers';

if (environment.production) {
  enableProdMode();
}

getTranslationProviders(environment.i18n.supportedLanguages).then(providers => {
  const options = { providers: providers };
  platformBrowserDynamic().bootstrapModule(AppModule, options);
});

The problem seems to be with bootstrapping the application asynchronously. If I do not get the translation providers and directly bootstrap the module ng serve runs normally.

@janis91
Copy link

janis91 commented Dec 13, 2016

Same here:

OS?

Debian 8

Versions.

angular-cli: 1.0.0-beta.22-1
node: 6.9.2
os: linux x64

Repro steps.

Was this an app that wasn't created using the CLI? What change did you do on your code? etc.
Update to beta.22-1

The log given by the failure.

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 (/home/user/workspace/project/node_modules/@ngtools/webpack/src/entry_resolver.js:121:15)
at AotPlugin._setupOptions (/home/user/workspace/project/node_modules/@ngtools/webpack/src/plugin.js:129:58)
at new AotPlugin (/home/user/workspace/project/node_modules/@ngtools/webpack/src/plugin.js:37:14)
at Object.exports.getWebpackNonAotConfigPartial (/home/user/workspace/project/node_modules/angular-cli/models/webpack-build-typescript.js:20:13)
at new NgCliWebpackConfig (/home/user/workspace/project/node_modules/angular-cli/models/webpack-config.js:23:42)
at Class.run (/home/user/workspace/project/node_modules/angular-cli/tasks/serve-webpack.js:20:22)
at /home/user/workspace/project/node_modules/angular-cli/commands/serve.js:102:26
at process._tickCallback (internal/process/next_tick.js:103:7)

Mention any other details that might be useful.

import './polyfills.ts';

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
import { AppModule } from './app/';

if (environment.production) {
  enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule)
  .then(success => console.log(`Bootstrap success`))
  .catch(err => console.error(err));

I guess the .then() function is the reason for this behaviour. If I delete the .then and .catch functions it works as expected. Is there another suggested way for handling bootstrapping errors?

@n0v1
Copy link

n0v1 commented Dec 14, 2016

I think #2887 is about the same error and proposes different work arounds.

@cladera
Copy link
Contributor Author

cladera commented Dec 16, 2016

Upgraded to beta.23 and still having this issue

@thinkingmedia
Copy link

I'm getting this on my build machine. Same problem.

@jakpaw
Copy link

jakpaw commented Dec 22, 2016

Same for me.

@derdeka
Copy link

derdeka commented Dec 22, 2016

Same for me. +1 for a bugfix.

@yfain
Copy link

yfain commented Dec 26, 2016

beta.24. Getting the same error when coded the AppModule and AppComponent inside the main.ts.

Didn't have this issue in beta.21.

@Anastasoff
Copy link

Hit the same issue, but after I removed

.then(success => console.log(`Bootstrap success`))
.catch(err => console.error(err));

from
platformBrowserDynamic().bootstrapModule(AppModule)
the issue disappeared.

@yfain
Copy link

yfain commented Dec 28, 2016

@Anastasoff I'm getting the same error #3744 without using then().

@florent1933
Copy link

florent1933 commented Dec 28, 2016

Same problem because I use Polymer with angular and I need to bootstrap my app like that:

document.addEventListener('WebComponentsReady', function() {
  platformBrowserDynamic().bootstrapModule(AppModule);
});

@filipesilva filipesilva added package: @ngtools/webpack P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent type: bug/fix labels Dec 30, 2016
@misty000
Copy link

I write "entryModule: 'app/app.module#AppModule'" to the AotPlugin options in "webpack-build-typescript.js", it works.
So give a way to config the entryModule? :)

@batcoder1
Copy link

update to "version": "1.0.0-beta.24", and the same problem with

platformBrowserDynamic().bootstrapModule(AppModule);

angular-cli: 1.0.0-beta.24
node: 7.3.0
os: win32 x64
@angular/common: 2.4.1
@angular/compiler: 2.4.1
@angular/core: 2.4.1
@angular/forms: 2.4.1
@angular/http: 2.4.1
@angular/platform-browser: 2.4.1
@angular/platform-browser-dynamic: 2.4.1
@angular/router: 3.4.1
@angular/compiler-cli: 2.4.1

@batcoder1
Copy link

solved! :)

when move files in webstorm, refact with extension and this is the problem.

change this:
import { AppModule } from 'app/app.module.ts';

to:
import { AppModule } from 'app/app.module';

@heyauri
Copy link

heyauri commented Jan 5, 2017

same for me.

@yuri1969
Copy link

yuri1969 commented Jan 5, 2017

This AoT detection bug should have been solved in a post beta.24 release. See #2887.

For now, you can use following horrible hack snippet. You have to add following code PAST YOUR bootstrap sequence:

// <-- your custom init platformBrowserDynamic().bootstrapModule(AppModule) is located here

// HACK: horrible workaround for AoT bootstrap detection bug
let hackThis = false; 
if (hackThis) {
  platformBrowserDynamic().bootstrapModule(AppModule);
}

@tulinisarg
Copy link

tulinisarg commented Jan 16, 2017

@yuri1969
doesn't work for me. Following is the bootstrap code.
`import './polyfills.ts';

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';

import { BAppModule } from './bApp/';
import { BSystemService } from './bGlobal/BSystemService';

let bSystemService : BSystemService = BSystemService.getInstance();

if (environment.production) {
enableProdMode();
}

if(bSystemService.deviceType == 'MOBILE'){
document.addEventListener("deviceready", function() {
platformBrowserDynamic().bootstrapModule(BAppModule);
}, false);
}
else{
platformBrowserDynamic().bootstrapModule(BAppModule);
}

let hackThis = false;
if (hackThis) {
platformBrowserDynamic().bootstrapModule(BAppModule);
}
`

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 (/home/jwuser/projects/Repo/bitbucket/ghs/node_modules/@ngtools/webpack/src/entry_resolver.js:121:15) at AotPlugin._setupOptions (/home/jwuser/projects/Repo/bitbucket/ghs/node_modules/@ngtools/webpack/src/plugin.js:158:54) at new AotPlugin (/home/jwuser/projects/Repo/bitbucket/ghs/node_modules/@ngtools/webpack/src/plugin.js:17:14) at Object.exports.getWebpackNonAotConfigPartial (/home/jwuser/projects/Repo/bitbucket/ghs/node_modules/angular-cli/models/webpack-build-typescript.js:20:13) at new NgCliWebpackConfig (/home/jwuser/projects/Repo/bitbucket/ghs/node_modules/angular-cli/models/webpack-config.js:26:42) at Class.run (/home/jwuser/projects/Repo/bitbucket/ghs/node_modules/angular-cli/tasks/serve-webpack.js:20:22) at /home/jwuser/projects/Repo/bitbucket/ghs/node_modules/angular-cli/commands/serve.js:108:26 at process._tickCallback (internal/process/next_tick.js:103:7)

@yroul
Copy link

yroul commented Jan 16, 2017

Same here.

I used to get things works based on @yuri1969 "hack" but since this morning, i get the same error as @tulinisarg

See code below.

import './polyfills.ts';

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
import { AppModule } from './app/app.module';

import { getTranslationProviders } from './app/i18n-provider';

if (environment.production) {
enableProdMode();
}

getTranslationProviders().then(providers => {
const options = { providers };
platformBrowserDynamic().bootstrapModule(AppModule, options);

});

let hackThis = false;
if (hackThis) {
platformBrowserDynamic().bootstrapModule(AppModule);
}

There is my package.json :

"dependencies": {
"@angular/common": "^2.4.0",
"@angular/compiler": "^2.4.0",
"@angular/compiler-cli": "^2.4.0",
"@angular/core": "^2.4.0",
"@angular/forms": "^2.4.0",
"@angular/http": "^2.4.0",
"@angular/platform-browser": "^2.4.0",
"@angular/platform-browser-dynamic": "^2.4.0",
"@angular/platform-server": "^2.4.0",
"@angular/router": "~3.4.0",
"@types/lodash": "^4.14.39",
"@types/rx": "^2.5.33",
"concurrently": "^3.1.0",
"core-js": "^2.4.1",
"lodash": "^4.17.2",
"rx": "4.1.0",
"ts-helpers": "^1.1.1",
"zone.js": "^0.7.2"
},
"devDependencies": {
"@reactivex/rxjs": "^5.0.0-rc.4",
"@types/jasmine": "^2.2.30",
"@types/node": "^6.0.42",
"angular-cli": "1.0.0-beta.24",
"codelyzer": "1.0.0-beta.1",
"graceful-fs": "4.1.11",
"jasmine-core": "2.4.1",
"jasmine-spec-reporter": "2.5.0",
"karma": "1.2.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-remap-istanbul": "^0.2.1",
"protractor": "4.0.9",
"ts-node": "1.2.1",
"tslint": "3.13.0",
"typescript": "~2.0.3"
}

@tulinisarg
Copy link

@yroul Can you try writing the bootstrap code in a function? It works for me.

here are the dependencies
"dependencies": { "@angular/common": "^2.4.1", "@angular/compiler": "^2.4.1", "@angular/core": "^2.4.1", "@angular/forms": "^2.4.1", "@angular/http": "^2.4.1", "@angular/platform-browser": "^2.4.1", "@angular/platform-browser-dynamic": "^2.4.1", "@angular/router": "^3.4.1", "angular2-onsenui": "^1.0.0-rc.3", "core-js": "^2.4.1", "onsenui": "^2.0.5", "rxjs": "^5.0.2", "ts-helpers": "^1.1.2", "zone.js": "^0.7.5" }

main.ts

`import './polyfills.ts';

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';

import { BAppModule } from './bApp/';
import { BSystemService } from './bGlobal/BSystemService';

let bSystemService: BSystemService = BSystemService.getInstance();

if (environment.production) {
enableProdMode();
}

if (bSystemService.deviceType == 'MOBILE') {
document.addEventListener('deviceready', function() {
bootstrapNow();
});
} else {
bootstrapNow();
}

function bootstrapNow() {
platformBrowserDynamic().bootstrapModule(BAppModule);
}`

@yuri1969
Copy link

@tulinisarg @yroul I've tried that hack of mine on beta.24. It needs retesting on any newer Angular-cli.

I can look into it later.

@yroul
Copy link

yroul commented Jan 16, 2017

@tulinisarg @yuri1969

I was able to make it work using @tulinisarg method (putting bootstrap code in function) or by simply removing @yuri1969 's hack.

Here is my main.ts (without yuri's hack)

import './polyfills.ts';

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
import { AppModule } from './app/app.module';

import { getTranslationProviders } from './app/i18n-provider';

if (environment.production) {
enableProdMode();
}
/**
*

  • It looks like the "hack" below does not work anymore since 01/16/2017
  • Disable I18n provider and async loading
    */
    getTranslationProviders().then(providers => {
    const options = { providers };
    platformBrowserDynamic().bootstrapModule(AppModule, options);

});

my package.json

"dependencies": {
"@angular/common": "^2.4.0",
"@angular/compiler": "^2.4.0",
"@angular/compiler-cli": "^2.4.0",
"@angular/core": "^2.4.0",
"@angular/forms": "^2.4.0",
"@angular/http": "^2.4.0",
"@angular/platform-browser": "^2.4.0",
"@angular/platform-browser-dynamic": "^2.4.0",
"@angular/platform-server": "^2.4.0",
"@angular/router": "~3.4.0",
"@types/lodash": "^4.14.39",
"@types/rx": "^2.5.33",
"concurrently": "^3.1.0",
"core-js": "^2.4.1",
"lodash": "^4.17.2",
"rx": "4.1.0",
"ts-helpers": "^1.1.1",
"zone.js": "^0.7.2"
},
"devDependencies": {
"@reactivex/rxjs": "^5.0.0-rc.4",
"@types/jasmine": "^2.2.30",
"@types/node": "^6.0.42",
"angular-cli": "1.0.0-beta.24",
"codelyzer": "1.0.0-beta.1",
"graceful-fs": "4.1.11",
"jasmine-core": "2.4.1",
"jasmine-spec-reporter": "2.5.0",
"karma": "1.2.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-remap-istanbul": "^0.2.1",
"protractor": "4.0.9",
"ts-node": "1.2.1",
"tslint": "3.13.0",
"typescript": "~2.0.3"
}

Then

rm -rf node_modules && npm cache clean && npm install && ng build --prod

@hikalkan
Copy link

hikalkan commented Jan 16, 2017

This happens because static parser founds multiple bootstrap code. We moved them into another file and called from main.ts. And to satisfy parser, we added a fake bootstrap code which is like that:

let hackThis = false;
if (hackThis) {
    platformBrowserDynamic().bootstrapModule(AppModule);
}

But notice that parser looks for exactly "bootstrapModule" naming. So, the extracted method (moved to another file) should have a different named method!

I hope angular-cli team finds a good way of this. Because it's very natural that we can have multiple startup modules and use one of them conditionally.

@yuri1969
Copy link

yuri1969 commented Jan 18, 2017

Yes, they have added a check for multiple bootstraps.

As @hikalkan says you need to separate your "real" bootstrap to another class/method in different file from main.ts and leave only the hacky bootstrap there. Then you need to call that method from the main.ts.

@BnSmth
Copy link

BnSmth commented Jan 22, 2017

I also ran in to this problem. And while it may not be the solution to this case, what caused it for me was this addition:

platformBrowserDynamic().bootstrapModule(AppModule)
    .then(_ => console.log('Bootstrap success'))
    .catch(err => console.error(err));

This is recommended in the official style guide however resulted in the same error as the OP. Removing the .then and .catch allowed for successful ng serve.

@buddyackerman
Copy link

@BnSmth I have this same issue. I only have one bootstrap call and if I add a catch to it it fails to compile. This has been this way for a while. I'm currently on CLI.24 but have had this problem since I started using the CLI at ver 19. Not sure why they leave this bug version after version after version.

@ukaaa
Copy link

ukaaa commented Jan 27, 2017

Same issue here with 1.0.0-beta.24.

I'm commenting here to hopefully get notified when this issue is closed so we can put the recommended .then and .catch back.

@Probotect0r
Copy link

I still getting this on beta26. I don't even have the .then and .catch. I upgraded an older project.

@buddyackerman
Copy link

@Probotect0r Updating to beta.26 solved the problem for me. Is your project too large to maybe create a new project using the beta.26 CLI and then move your "old" code to it to see if it breaks?

@fernandolguevara
Copy link

fernandolguevara commented Feb 1, 2017

There is no posible to have N calls to "bootstrapModule" or "bootstrapModuleFactory" in your main file

the entry_resolver.js file, in @ngtools/webpack/src/, is limited to have only one bootstrap mode, so if you need to do something like this

if(isUserLoggedIn)  {
    platformBrowserDynamic().bootstrapModule(AppModule);
} else {
    platformBrowserDynamic().bootstrapModule(LoginModule);
}

// or 

var moduleToBootstrap = null

if(isUserLoggedIn)  {
    moduleToBootstrap  = AppModule;
} else {
    moduleToBootstrap  = LoginModule;
}

platformBrowserDynamic().bootstrapModule(moduleToBootstrap);


// or 

import { AppModule as FooBar } from './app/app.module';

platformBrowserDynamic().bootstrapModule(FooBar);

or any other logic to bootstrap your application, you have to find another way to do that.

I hope this information is of use to you.

@Probotect0r
Copy link

@buddyackerman Yea thats what I ended up doing.

@kuncevic
Copy link

kuncevic commented Feb 1, 2017

@MickL I am on beta.24. The hack thing was working untill I reinstall npm_moudles. So there is some internal module that breaking the thing. So if you do fresh npm install on beta.24 the hack won't work any more.

my devDependencies:

"devDependencies": {
    "@angular/compiler-cli": "2.4.0",
    "@types/jasmine": "2.5.38",
    "@types/node": "6.0.52",
    "@types/whatwg-fetch": "0.0.33",
    "angular-cli": "1.0.0-beta.24",
    "codelyzer": "2.0.0-beta.4",
    "jasmine-core": "2.5.2",
    "jasmine-spec-reporter": "2.5.0",
    "karma": "1.2.0",
    "karma-chrome-launcher": "2.0.0",
    "karma-cli": "1.0.1",
    "karma-jasmine": "1.1.0",
    "karma-remap-istanbul": "0.2.2",
    "protractor": "4.0.14",
    "ts-node": "1.2.1",
    "tslint": "4.1.1",
    "typescript": "2.0.10"
  }

Will try to migrate to beta.28 and see if it will help.

UPDATE:
I can confirm that with beta.28.3 all working without a hack.
That is the list of packages that beta.28.3 is bringing in:

+-- @angular/common@2.4.5
+-- @angular/compiler@2.4.5
+-- @angular/compiler-cli@2.4.5
+-- @angular/core@2.4.5
+-- @angular/forms@2.4.5
+-- @angular/http@2.4.5
+-- @angular/platform-browser@2.4.5
+-- @angular/platform-browser-dynamic@2.4.5
+-- @angular/router@3.4.5
+-- @types/jasmine@2.5.38
+-- @types/node@6.0.62
+-- angular-cli@1.0.0-beta.28.3
+-- codelyzer@2.0.0-beta.4
+-- core-js@2.4.1
+-- jasmine-core@2.5.2
+-- jasmine-spec-reporter@2.5.0
+-- karma@1.2.0
+-- karma-chrome-launcher@2.0.0
+-- karma-cli@1.0.1
+-- karma-jasmine@1.1.0
+-- karma-remap-istanbul@0.2.2
+-- protractor@4.0.14
+-- rxjs@5.1.0
+-- ts-helpers@1.1.2
+-- ts-node@1.2.1
+-- tslint@4.4.2
+-- typescript@2.0.10
`-- zone.js@0.7.6

@nagarsuresh
Copy link

thanks @hikalkan for explaining the issue but i did a little more hacky solution than moving it to separate file.
looks like cli is not able to detect the below and it works :)

switch (id) {
  case 'left':
    platform['bootstrapModule'](App1Module); break;
  case 'right':
    platform['bootstrapModule'](App2Module);
}

@tavelli
Copy link

tavelli commented Mar 10, 2017

@snagar78 what imports are you using for that to work?

@nagarsuresh
Copy link

@tavelli there are no special imports for that the just the ones which are needed, anyways you can look at the sample at
https://github.com/snagar78/Ng2-MultRoot-App

let me know if that helps.

@mmenik
Copy link

mmenik commented Mar 21, 2017

I need to bootstrap the app like this, but don't works

document.addEventListener('WebComponentsReady', () => {
platformBrowserDynamic().bootstrapModule(AppModule);
});

@africhild
Copy link

working with angular polymer I need to bootstrap this way but it is not working

document.addEventListener('WebComponentsReady', () => {
platformBrowserDynamic().bootstrapModule(AppModule);
});

@sjselvan
Copy link

sjselvan commented Mar 31, 2017

double check your { AppModule } import in main.ts file . I had a wrong path to the app module in the import statement.

@ahashem
Copy link

ahashem commented Apr 9, 2017

Is their an update for this issue after "@angular/cli": "1.0.0" release?

@magleahy
Copy link

magleahy commented Apr 12, 2017

I just got the latest Angular-CLI:

    _                      _                 ____ _     ___
   / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
  / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
 / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
/_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
               |___/
@angular/cli: 1.0.0
node: 7.5.0
os: darwin x64
@angular/animations: 4.0.2
@angular/common: 4.0.2
@angular/compiler: 4.0.2
@angular/compiler-cli: 4.0.2
@angular/core: 4.0.2
@angular/forms: 4.0.2
@angular/http: 4.0.2
@angular/platform-browser: 4.0.2
@angular/platform-browser-dynamic: 4.0.2
@angular/platform-server: 4.0.2
@angular/router: 4.0.2
@angular/cli: 1.0.0

And then I tried to look at using AOT via instructions on https://angular.io/docs/ts/latest/cookbook/aot-compiler.html

I run

npm run build:aot

> angular-latest@0.0.0 build:aot /Users/magleahy/Desktop/projects/angular-latest
> ngc -p tsconfig-aot.json && rollup -c rollup-config.js

⚠️   'default' is imported from external module 'rollup' but never used

and then

ng serve

and get

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

@TRAHOMOTO
Copy link

Hello guys! Here another @@ck for this issue ... Hey @snagar78! Thank you bro!

I use it for "multi-root" application with Drupal

Works with
"@angular/common": "^4.0.2",
"@angular/compiler": "^4.0.2",
"@angular/core": "^4.0.2",
"@angular/forms": "^4.0.2",
"@angular/http": "^4.0.2",
"@angular/platform-browser": "^4.0.2",
"@angular/platform-browser-dynamic": "^4.0.2",

// main.ts
...
if (environment.production) {
    enableProdMode();
}
// Here your "statically analyzable bootstrap code"
const platform = platformBrowserDynamic();
platform.bootstrapModule(AppEmptyModule);
...
// Sync start
const module = getSomeHowModuleToStart()
platform['bootstrapModule'](module) .then( v => /* do somthing with v */ });
...
// Or Async start
getSomeHowModuleToStart().then(module => platform['bootstrapModule'](module) );
... 

Empty component

//empty-component.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-empty',
  template: '',
})
export class EmptyComponent {
  constructor() { }
}

And empty module

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import {EmptyComponent } from './components';


@NgModule({
  declarations: [EmptyComponent],
  imports: [BrowserModule],
  providers: [],
  bootstrap: [EmptyComponent]
})
export class AppEmptyModule { }

@Angular2Guy
Copy link

I tried to implement the Angular.io i18n Cookbook for the Jit Compiler. https://angular.io/docs/ts/latest/cookbook/i18n.html#!#jit
The cookbook is written for SystemJs Projects and not for Angular Cli.
One piece is missing for me. In the i18n-providers.ts file the *.xlf files are read with System.import and a text plugin. That does not work for me in the Angular Cli project.

`import { TRANSLATIONS, TRANSLATIONS_FORMAT, LOCALE_ID } from '@angular/core';

export function getTranslationProviders(): Promise<Object[]> {
// Get the locale id from the global
const locale = navigator.language.split('-')[0] as string;

// return no providers if fail to get translation file for locale
const noProviders: Object[] = [];
// No locale or U.S. English: no translation providers
if (!locale || locale === 'de') {
return Promise.resolve(noProviders);
}
// Ex: 'locale/messages.es.xlf`
return getTranslationFilesWithWebpack(locale)
.then( (translations: string ) => [
{ provide: TRANSLATIONS, useValue: translations },
{ provide: TRANSLATIONS_FORMAT, useValue: 'xlf' },
{ provide: LOCALE_ID, useValue: locale }
]);
//do not swallow missing translation files.
// .catch(() => noProviders); // ignore if file not found
}

declare var require: any;

function getTranslationFilesWithWebpack(locale: string) {
const translationFile = ./locale/messages.${locale}.xlf;
return ???
}`

The question is how are the files read in a Webpack Angular Cli environment?

@Angular2Guy
Copy link

There is a solution to get Angular i18n to run with the Jit compiler.

  1. put "raw-loader": "~0.5.1" in the devDependencies
  2. put this in i18n-providers.ts:
    function getTranslationFilesWithWebpack(locale: string) : Promise {
    return Promise.resolve(require('raw-loader!'+'./locale/messages.'+locale+'.xlf'));
    }
  3. put this in main.ts:
    translateFn.getTranslationProviders().then(providers => {
    const options = { providers };
    platformBrowserDynamic().bootstrapModule(AppModule, options);
    });

It works then.

@tavelli
Copy link

tavelli commented Apr 26, 2017

@snagar78 using your workaround i was able to get AOT to successfully compile. but when i go to run compiled code in browser i get this error and apps fail to load:

Uncaught Error: No NgModule metadata found for 't'.

@filipesilva
Copy link
Contributor

The original issue (trying to use i18n as per the cookbook) has been solved by the addition of --i18n-file and other flags in ng build.

Please open a new issue for other problems you might be experiencing.

@rinelb
Copy link

rinelb commented Jul 19, 2017

i fixed it by

import { AppModule } from './app/';

to:

import { AppModule } from 'app/app.module';

@ravishtiwari
Copy link

I was facing the same problem, fixed it by moving all dynamic bootstrapping code to a separate module, so my main.ts looks something like:

import { bootstrapDynamicModules } from './bootstrapper';
platformBrowserDynamic().bootstrapModule(EmptyModule);
bootstrapDynamicModules();

EmptyModule, as the name suggests in basically empty, with one empty component, which is statically bootstrapped and rest other modules, I bootstrap dynamically in bootstrapper.ts.

@ravishtiwari
Copy link

Be aware everyone, this has it's own limitation. I am not sure if any one else is facing similar issue, but, I get following error when trying to dynamically bootstrap different modules based on querySelector (read component selector):

polyfills.b41b6f97cd69f92028c5.bundle.js:1 Uncaught Error: No NgModule metadata found for 'n'.
    at t.resolve (vendor.3ed19c27dace42460c9c.bundle.js:1)
    at t.getNgModuleMetadata (vendor.3ed19c27dace42460c9c.bundle.js:1)
    at t._loadModules (vendor.3ed19c27dace42460c9c.bundle.js:1)
    at t._compileModuleAndComponents (vendor.3ed19c27dace42460c9c.bundle.js:1)
    at t.compileModuleAsync (vendor.3ed19c27dace42460c9c.bundle.js:1)
    at e._bootstrapModuleWithZone (vendor.3ed19c27dace42460c9c.bundle.js:1)
    at e.bootstrapModule (vendor.3ed19c27dace42460c9c.bundle.js:1)

@krisclarkdev
Copy link

I fixed this error because I forgot to export the module at the end of app.module.ts

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent type: bug/fix
Projects
None yet
Development

No branches or pull requests