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.
Add a comment
|
2 Answers
You may need to declare the variable in your typescript file so the compiler knows its exists:
declare var x: any;
Comments
ProvidePlugin from webpack should do the job, they have a pretty good example here
2 Comments
rld001
I don't understand how to do this with my own file referring to the documentation's examples
luanped
just to clarify my understanding of your problem, currently with your global variable x, do you mean typescript compiler is simply complaining that you are trying to access an undefined variable hence not compiling (but functionally works if you do something like window['x']. or are you saying that the variable is not being globally scoped but file scoped instead?