19

In typescript 2.4.0 I am trying to create global/environment variables using DefinePlugin using webpack. So here is the approach I am trying to take:

webpack.js Define part

new webpack.DefinePlugin({
   environment: JSON.stringify('DEVELOPMENT'),
}),

globals.ts

declare const environment: String;
console.log(environment);
export { environment };
console.log(environment);

environmentService.ts

import { IEnvironment } from 'environment';
import { environment } from '../globals';
console.log(environment);

export default class EnvironmentService implements IEnvironment  {
  environmentName: String = environment;

  get isDevelopment(): boolean {
    return this.environmentName === 'DEVELOPMENT';
  }

  get isProduction(): boolean {
    return this.environmentName === 'PRODUCTION';
  }
}

In the console log I get:

DEVELOPMENT
DEVELOPMENT
undefined

So console.log(environment); inside environmentService.ts is giving me undefined when I am expecting DEVELOPMENT and I am not sure why?

It appears it only exports the variable and not the value?

If someone can explain what I am doing wrong and why its getting undefined i would appreciate it.

Edit 1

I think I see what DefinePlugin is doing. Its not setting environment to DEVELOPMENT but rather replacing lines console.log(environment); with console.log('DEVELOPMENT'); and when it exports environment its undefined.

3
  • Where does environment or environmentName get defined? Commented Jun 20, 2017 at 13:58
  • In global.ts and DefinePlugin is replacing the value. Commented Jun 20, 2017 at 14:02
  • Where does it replace the value? You're importing DefinePlugin as IEnvironment, but you never use it... unless my thinking is wrong. Commented Jun 20, 2017 at 14:05

3 Answers 3

22

You can create a @types folder and put a declaration file global.d.ts in it, with just below content.

declare const environment: string;

TypeScript includes all @types packages by default during compilation: https://www.typescriptlang.org/tsconfig#types

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

1 Comment

Yes, declare const environment: string removes the TS warning. use :string instead of :String
5

DefinePlugin inlines code parts, thats why you need all those JSON.stringify('DEVELOPMENT') not just 'DEVELOPMENT'. It replaces global identifiers it knows with the given code parts as is. If you define environment: '"PROD" + "DUCTION"'

Then

if(environment === 'PRODUCTION') {

}

Becomes

if("PROD" + "DUCTION" === 'PRODUCTION') {

}

Something like this should work in ts2

const env: String = environment;
export { env as environment };

3 Comments

Now I get Cannot find name 'environment.
@Michael Which line?
Got it to work. I took out declare const environment: String; so when I put it back in it worked. Thanks!
4

You need update your globals.ts like this for any webpack DefinePlugin variables:

declare const environment: string;
const _environment = environment
export { _environment as environment };

More here.

Comments

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.