Leaving some components out of production, build Angular2?

Is there a way to leave the components / pieces of code of their choice during build to build the build in Angular2 / 4?

I have an application that has an administrative tool that I am using locally. I would like this administrative tool to be left without a working version of the application. I can - of course - comment out the code every time I do a build, but is there an even more elegant solution? How is conditional building on certain parts of the code?

I'm looking for something like * ngIf = "buildingForProduction" - a type of solution for something along these lines. He exists?

+3


source to share


2 answers


Building on @ cyrix answer.

When using ang cli, there is a folder called environment which by default contains the environment.prod.ts and environment.ts files. You can add more for different compilations if you want and customize your app accordingly.

These files contain an object like this:

export const environment = {
  production: false,
  myVariable: 'This variable is not included in production build'
};

      

When you build your application with

ng build --env=prod

      



Then ang cli will use environment.prod.ts. If you build with

ng build

      

Then the default environment.ts is used.

By adding variables to this environment object, you can import it into your application and read the variables. Like you, you can check if you are in the assembly or not:

import { Component } from '@angular/core';
import { environment } from './../environments/environment';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  constructor() {
    console.log(environment.production); // Logs false for default environment
  }
  title = 'app works!';
}

      

+2


source


One possible solution would be to change your tsconfig.json and add to exclude the folders or files you want to add.

"exclude": [
  "**/*file.ts", 
  "**/*config.ts"
]

      



Unfortunately you can only have one tsconfig.json file per project it would be nice to have a few similar dev, prod, hopefully this feature is coming soon https://github.com/Microsoft/TypeScript/issues/3645 . Hope it helps.

-1


source







All Articles