简体   繁体   中英

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

I'm really excited about having just introduced Standard JS into my workflow. My web project uses Gulp JS to watch changes and report any syntax errors.

Similar to another question I've seen here . Standard JS works great for singular files. 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. 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. It's important to me that the --fix flag is used to modify the local development files. I don't want to rely on editor plugins (Atom/Sublime) that need to be installed locally for each developer.

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. Explicitly import or require shared variables.

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.

Example:

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

2. Use a /* global myVar */ comment.

This makes it clear to readers of the file (and the StandardJS tool) that you are intentionally referencing a variable external to this file.

Example:

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

3. Reference the variable as a property of the global.

Example:

console.log(window.myVar)

4. Concat the files, then run StandardJS on the built file.

It sounds like this approach worked when you tried it. It's not a bad idea to just always run it like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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