-
Notifications
You must be signed in to change notification settings - Fork 12k
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
feat(serve): Add -main flag for entry point #3940
Comments
I just came across this question on stackverflow, that seems to be a perfect example why this might be useful |
Hi Everyone - I wrote the SO question. The scenario is pretty simple. We dont want to ship all our test code for the mock services in the production build. It should be a pretty common use case. |
Would you not be able to do your conditional imports from inside the environment files instead? And then expose whatever you need to
You're essentially asking for a split point where a file would be used to conditionally include static ES imports (and other code). In the scenario presented, the split point would be Unless I'm missing something, the only important thing is that somewhere this split point exists. |
That being said, the environments functionality really does not discriminate. It's an absolute replacement. This works:
I wouldn't recommend it overall because if some big change in the functionality is required this usecase won't be kept in mind. But currently, as far as webpack is concerned it's just a file replacement.
|
We still really need a way to run a different
...it complicates the ability to keep leveraging the Another option here (instead of or in addition to the
|
That is exactly what I had in mind. It would be awesome to have something like that! But why use |
I'm still having some difficulty seeing why it's better to switch It's true that it's perfectly doable to add in main swapping but if there's a better abstraction (and I think the env files are one) then it's better to use that. Swapping main might be common for webpack users but not so much for other build stacks. If you're using the quickstart, for instance, using a different main involves changing code in a couple of places. I know there have been a couple of theoretical examples in this issue already, but could you also give me a complete one (all the different file contents from a newly generated project) that illustrates the difference between swapping |
I for instance swap my main file because in development I want the ngrx devtools but I don't want them in my production application. I'm on mobile now but will show my code examples when on the computer. |
I'm on my computer so I can share some code. Developmentmain.dev.tsimport './polyfills';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { environment } from './environments/environment';
import { DevModule } from './app/dev.module';
platformBrowserDynamic().bootstrapModule(DevModule); dev.module.tsimport { NgModule, Component } from '@angular/core';
import { StoreModule } from '@ngrx/store';
import { StoreLogMonitorModule, useLogMonitor } from '@ngrx/store-log-monitor';
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
import { StoreUndoModule } from 'ngrx-undo';
import { AppModule } from './app/app.module';
import { rootReducer } from './shared/index';
@Component({
selector: 'application-wrapper',
template: `
<sd-app></sd-app>
<ngrx-store-log-monitor toggleCommand="ctrl-t" positionCommand="ctrl-m"></ngrx-store-log-monitor>
`
})
export class ApplicationWrapperComponent { }
@NgModule({
imports: [
StoreModule.provideStore(rootReducer),
StoreDevtoolsModule.instrumentStore({
monitor: useLogMonitor({
visible: false,
position: 'right'
})
}),
StoreUndoModule.interceptStore({
bufferSize: 20
}),
StoreLogMonitorModule,
AppModule
],
declarations: [ApplicationWrapperComponent],
bootstrap: [ApplicationWrapperComponent]
})
export class DevModule {} Productionmain.prod.tsimport './polyfills';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
import { ProdModule } from './app/prod.module';
enableProdMode();
platformBrowserDynamic().bootstrapModule(ProdModule); prod.module.tsimport { NgModule, Component } from '@angular/core';
import { StoreModule } from '@ngrx/store';
import { StoreUndoModule } from 'ngrx-undo';
import { AppModule } from './app/app.module';
import { rootReducer } from './shared/index';
@Component({
selector: 'application-wrapper',
template: ` <sd-app></sd-app>`
})
export class ApplicationWrapperComponent {}
@NgModule({
imports: [
StoreModule.provideStore(rootReducer),
StoreUndoModule.interceptStore({
bufferSize: 20
}),
AppModule
],
declarations: [ApplicationWrapperComponent],
bootstrap: [ApplicationWrapperComponent]
})
export class MainModule {} As you can see, the production module does not bootstrap and show the ngrx devtools, but the development module does. If you have any feedback on to how it can be achieved without different main files, I would be happy to hear that. But for now, this is my use case why I need something like this setting or flag. |
@SamVerschueren would this not work? Essentially exporting
|
@filipesilva I like this solution. Works well to solve the "module to bootstrap" puzzle and the ability to do the rest of the conditional stuff in the environment files. |
Cool, never thought about it like that. That would definitely work. Thanks! |
Awesome, glad to his this works for you! |
Thank you everyone for explaining the use case scenarios and thank you @filipesilva for working with us to show us a solution! |
My pleasure ^^. This is a topic very dear to me really. |
@filipesilva This approach no longer works now with beta.31
Getting the following error on
|
Will investigate. |
@filipesilva Did you happen to get a chance to look into this? I think it may be this change that broke it 2797a89 Another potential way around it...is there a way to dynamically change |
@jschwarty sorry I haven't yet had time to look into this, but @hansl seems to have an idea of what is happening and we'll try to get a fix into the next release. |
Thank you @filipesilva and @hansl, very much appreciated! |
Okay, after discussion with @jschwarty (thanks for taking time for this), we came to the conclusion that the best way to support his use case while also supporting other use cases would be to finally have support for other As a simple fix, I suggest that we add an |
And after discussing with the team, we're going to also support naming your app, so that |
❤ it! Thank you @hansl and team! |
Will this perhaps help when it comes to having multiple locale bundles? #2201 At the moment for multiple locales, people are creating multiple bundles and then redirecting via a server like: $ for lang in es en fr; do \
ng build --output-path=dist/$lang \
--aot \
-prod \
--bh /$lang/ \
--i18n-file=src/i18n/messages.$lang.xlf \
--i18n-format=xlf \
--locale=$lang; \
done |
It's still working for me though. Running |
Fixed and Merged already: #4754 |
I tried creating multiple projects for dev & prod. Looks like the projects cannot have a single code base. It keeps throwing " can't be part of 2 module declarations" error even though the modules of prod & dev are kept separate. I don't want to have copies of the same code in dev & prod projects. I came down this path only because I wanted the mock files to be excluded from production build... Am I missing something here? |
@srix55 take a look of this: #8031 (comment) |
Separating mocks from production code The problem: Mock code used for testing get bundled into production. @filipesilva 's response to this - This error happens in your setup because you have two apps that share all the TS files. The files that each app contains are defined in src/tsconfig.app.json. You use the same tsconfig for both, and that tsconfig only excludes test.ts and *.spec.ts files. The solution is to create separate tsconfig for each app, excluding the files specific to other apps.
|
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Feature requested
Would like to be able to change the main entry point script to use via a flag when running
ng serve
(as well asng build
).Reason
While leveraging
environment
constants are handy to conditionally run code in yourmain.ts
script, they don't get used by webpack (or AOT). So if you have some environment code that you don't want to include in your production build and you do the following in yourmain.ts
:Webpack will follow that function and its imports and will add that code to the bundles even when you
ng serve
orng build
.Having the ability to target different main scripts would allow for
ng serve
andng build
to be used to build the appropriate code.Then we could create different main files:
main.ts
main.other.ts
And achieve optimal bundling of our app code based on our intended environment.
Notes
I'd be more than happy to add this feature, just want to confirm that it is something that people would feel would be worthy.
@hansl @robwormald
The text was updated successfully, but these errors were encountered: