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.
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.
2 Answers
Reset to default 4You may need to declare the variable in your typescript file so the piler knows its exists:
declare var x: any;
Adapted from this answer
ProvidePlugin from webpack should do the job, they have a pretty good example here