0

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 1

0

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I use simple ts2js
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.
I would like to continue to use ts2js.. without other plugin-compiler
i guess the only option is to generate config.ts dynamically.
can be an example please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.