如何在编译之前在多个文件之间共享全局javascript变量?

时间:2017-08-14 20:24:26

标签: javascript eslint gulp-eslint

当有多个文件被编译为共享全局变量/函数的单个文件时,处理linting的最佳方法是什么。例如:

file_1.js:

/*Tabulky tymy*/

.svazTymy {
    overflow: auto;
    padding:10px;

}

.svazTymy  td{
    border: 1px solid black;
    padding: 5
}


.svazTymy tr:nth-child(even) {
    background: #006eb9;
    color:#fff;}
.svazTymy table tr:nth-child(1) td {
    font-weight : bolder;}   

.svazTymy tr:nth-child(odd) {background: #FFF}

.svazTymy table tr:nth-child(10) td {
    border-bottom : 3px solid red;}

.svazTymy tr.beroun {
    background-color: #825f5f;
}

file_2.js:

{
const my_flag = 1;
}

编译和组合这两个文件时,没有问题。但是 file_1.js 会抛出与未使用的变量相关的linting错误,而 file_2.js 会抛出与未定义变量相关的linting错误。

我放心,我可以忽略与问题相关的特定行,但这会破坏linting文件的目的。在linting过程中,在文件之间共享信息的最佳方式是什么?

2 个答案:

答案 0 :(得分:1)

使用eslint,您可以告诉脚本变量是全局变量:

/* global my_flag */

在第二个文件中使用my_flag之前放置此行(通常这是文件的第一行)。这样可以避免关于undefined变量my_flag

的linting错误

答案 1 :(得分:0)

.eslintrc配置文件允许命名 globals 来解决问题:

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

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

相关问题