How do I share global javascript variables among multiple files prior to compiling?

761 Views Asked by At

What's the best way to handle linting when there are multiple files that will be compiled to a single file that share global variables/functions. For example:

file_1.js:

{
const my_flag = 1;
}

file_2.js:

{
  if (my_flag) {
  // etc.

When the two files are compiled and combined, there's no problem. But file_1.js throws a linting error related to an unused variable, and file_2.js throws a linting error related to an undefined variable.

I relize I could ignore the specific lines related to the issue, but that's defeating the purpose of linting the files. What's the best way to share the information between the files during the linting process?

2

There are 2 best solutions below

0
On BEST ANSWER

The .eslintrc configuration file allows naming globals which solved the problem:

"globals": {
  "my_global": true,
  "another_global": true,
  "third_global": true
}

http://eslint.org/docs/user-guide/configuring#specifying-globals

0
On

with eslint you can tell you script that a variable is global:

/* global my_flag */

Put this line before my_flag is used in your second file (generally this is the first line of a file). This will avoid linting error about undefined variable my_flag