简体   繁体   English

standardjs + gulp:如何将源文件视为一个?

[英]standardjs + gulp: How to treat source files as one?

I'm really excited about having just introduced Standard JS into my workflow. 刚将Standard JS引入我的工作流程中,我感到非常兴奋。 My web project uses Gulp JS to watch changes and report any syntax errors. 我的Web项目使用Gulp JS监视更改并报告任何语法错误。

Similar to another question I've seen here . 类似于我在这里看到的另一个问题。 Standard JS works great for singular files. 标准JS非常适合单个文件。 However, I tend to break my scripts into separate files for legibility and sanity reasons. 但是,出于易读性和合理性考虑,我倾向于将脚本分成单独的文件。

This results in various is not defined , is assigned a value but never used , and is defined but never used warnings. 结果导致各种is not definedis assigned a value but never used ,并且is defined but never used警告。 If I manually concatenate all the files, those warnings are not thrown. 如果我手动串联所有文件,则不会引发这些警告。

I understnad this is just looping through all the files individually: 我理解这只是循环遍历所有文件:

standard --fix "dev/scripts/**/*.js"

...but I wondered if there were any flags, settings, or other resources I could use to treat all the files as one? ...但是我想知道是否有任何标志,设置或其他资源可用于将所有文件视为一个?

gulp-standard seemed promising at first glance, but still has the same problem. 乍看之下, gulp标准似乎很有希望,但仍然存在相同的问题。 It's important to me that the --fix flag is used to modify the local development files. 对我很重要,-- --fix标志用于修改本地开发文件。 I don't want to rely on editor plugins (Atom/Sublime) that need to be installed locally for each developer. 我不想依赖需要为每个开发人员本地安装的编辑器插件(Atom / Sublime)。

I'm surprised to see so few reports on this. 看到如此少的报道,我感到很惊讶。 Either no one else fragments their code; 没有其他人可以分割他们的代码。 or I'm missing something far more fundamental in the way I code. 或我在编码方式中缺少了更根本的东西。

You have three options: 您有三种选择:

1. Use a module bundler. 1.使用模块捆绑器。 Explicitly import or require shared variables. 显式importrequire共享变量。

This makes the external variables that are referenced by each file explicit. 这使得每个文件引用的外部变量都是显式的。 So, you can't reference a variable until you've required it in the current file. 因此,在当前文件中需要变量之前,您不能引用它。

browserify is a nice, simple bundler to use to get started with this npm-centric development model. browserify是一个不错的,简单的打包程序,可用于以npm为中心的开发模型入门。

Example: 例:

var myVar = require('./lib/my-module.js').myVar
console.log(myVar)

2. Use a /* global myVar */ comment. 2.使用/* global myVar */注释。

This makes it clear to readers of the file (and the StandardJS tool) that you are intentionally referencing a variable external to this file. 这使文件(和StandardJS工具)的读者可以清楚地知道您有意引用此文件外部的变量。

Example: 例:

/* global myVar */
console.log(myVar)

3. Reference the variable as a property of the global. 3.将该变量作为全局属性引用。

Example: 例:

console.log(window.myVar)

4. Concat the files, then run StandardJS on the built file. 4.合并文件,然后在生成的文件上运行StandardJS。

It sounds like this approach worked when you tried it. 听起来好像这种方法在您尝试过时有效。 It's not a bad idea to just always run it like that. 总是像这样运行它并不是一个坏主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM