How to pass a parameter Angular-2 script using Gulp? In order to Angular-2 could get the value of this variable at startup.
To start gulp was about: gulp start --type=dev and that the Angulyar-2 could get the value of the variable "type"
1 Answer
It depends on which tool you are using to compile TypeScript.
Use DefinePlugin if you are compiling with gulp-webpack or webpack-stream, it does exactly what you want.
If you are using gulp plugins like gulp-typescript, gulp-ts or gulp-typescript-compiler then you could emulate variable injection using gulp-replace or gulp-preprocess. Also check this answer, you could generate TypeScript config in similar manner.
Declare global var (add *.d.ts file if necessary):
export interface IConfig {param:boolean}
declare var config:IConfig;
In you gulp task:
var config = {};
if(process.argv.indexOf('--name') > -1){
config.param = true;
}
else {
config.param = false;
}
fs.writeFileSync('path../config.js', 'var config='+JSON.stringify(config));
Import config.js using <script> tag.
5 Comments
Denys Adamenko
I use simple ts2js
kemsky
It looks like ts2js does not follow gulp way (stream, pipes). Is fairly simple to setup one of gulp-typescript, gulp-ts or gulp-typescript-compiler plugins.
Denys Adamenko
I would like to continue to use ts2js.. without other plugin-compiler
kemsky
i guess the only option is to generate config.ts dynamically.
Denys Adamenko
can be an example please?