rld001
rld001

Reputation: 153

How to access JS global variable in Typescript file, using webpack

If I have a global variable var x in my main.js file, which i'd like to be accessible in a typescript file which is bundled by webpack into the same build/bundle.js, how would i go about this? I cannot access it directly as i assumed i might (them being in the same bundle at the end). I have read of export/import/require but cannot find how to apply this in the context of a webpack bundle.

Upvotes: 3

Views: 4874

Answers (2)

tony
tony

Reputation: 1326

You may need to declare the variable in your typescript file so the compiler knows its exists:

declare var x: any;

Adapted from this answer

Upvotes: 4

luanped
luanped

Reputation: 3198

ProvidePlugin from webpack should do the job, they have a pretty good example here

Upvotes: 0

Related Questions