bug(webpack): Webpack do not find bootstrap code if app is bootstrapped asynchronously
OS?
Mac OSX (Sierra)
Versions.
angular-cli: 1.0.0-beta.22-1
node: 6.5.0
os: darwin x64
Repro steps.
- Configure i18n according to angular's cookbook
- 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.
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 { }
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?
There is a solution to get Angular i18n to run with the Jit compiler.
- put "raw-loader": "~0.5.1" in the devDependencies
- put this in i18n-providers.ts:
function getTranslationFilesWithWebpack(locale: string) : Promise
{ return Promise.resolve(require('raw-loader!'+'./locale/messages.'+locale+'.xlf'));
} - put this in main.ts: translateFn.getTranslationProviders().then(providers => { const options = { providers }; platformBrowserDynamic().bootstrapModule(AppModule, options); });
It works then.
@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'.
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.
i fixed it by
import { AppModule } from './app/';
to:
import { AppModule } from 'app/app.module';
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.
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)
I fixed this error because I forgot to export the module at the end of app.module.ts
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.