0

I'm using HTML5 WebWorkers, and i faced such problem (while compiling my scripts with closure compiler):

I have two files (.js) with this code inside:

...

var encoder = null;

...

In other words i have two global variables with the same name.

These files will be used as a source for both workers that i want to launch, and as i know, both files will be executed in their own scope, and there would be no conflicts during runtime.

One more condition is that i want to compile both files (modules) in one time

--module module1:1

--js file

--module module2:1

--js file2.js

But, Closure Compiler detects that conflict and says, that module2 should depend on module1, because both have same global variables.

What should i do?

Jim Blackler
  • 22,946
  • 12
  • 85
  • 101
bonbonez
  • 6,658
  • 2
  • 15
  • 16

1 Answers1

3

There are a few different options:

  1. Use your variable as a property on the global (window) object. Defined as such: window.encoder = window['encoder'] || {};
  2. Add a @suppress {duplicate} annotation to the second variable definition.
  3. Add an immediately executed anonymous function wrapper to move your variable definition out of the global scope. You might then need to explicitly export your variable to re-expose it globally.
Chad Killingsworth
  • 14,360
  • 2
  • 34
  • 57