-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow require() but forbid module.exports #548
Comments
Are you doing so frequently enough that if (process.env.NODE_ENV == "production") {
require('../foo.css'); // eslint-disable-line import/no-commonjs
} would be burdensome? |
Yep, I think so. Our current workaround is (ab)using |
what if i.e. you can set // allowDynamicRequire: true
const fs = require('fs') // still reported
if (process.env.NODE_ENV == "production") {
require('../foo.css'); // fine
} |
That would cover most cases, but in some cases we explicitly use if (!window.Promise) {
require('./promise-shim');
}
// Load Foo *after* we have shim in place.
const Foo = require('./Foo').default; |
@benmosher I don't mind having this, but this should then probably be included in v2. At least if we want to split the rule into two and then remove the original one. Either that or have an option that allows either |
Yeah, I'm leaning towards flags on this. So probably no breaking changes, I still like the default rule just reporting on all CJS usage, regardless of whatever modifications/new rules. |
Agreed on the flags and defaults. |
Yep, that would work for me. |
actually I'm thinking the setting should be require: "allow" | "deny" (default) | "dynamic" @Wilfred you could use |
also not super-attached to the word |
@benmosher What would dynamic be? A require with an expression as the argument, or one in a conditional statement/other construct? |
i.e. const someVariedImport = (someCondition) ? require('one') : require('other'); |
Expression as the argument would |
dynamic is used as the term for no-dynamic-require, maybe that's not a good name too. Where I see requires the most in the codebases, is with hot-reloading, so import foo from './foo';
if (module.hot) {
const foo = require('./foo');
//...
}
Yes, that looks like what it is doing. |
@[potential implementer]: don't feel like you have to implement |
I'm porting a codebase to ES6. In some cases we explicitly want to use
require()
, becauseimport
must be at the top of the file.For example, we sometimes write:
or in tests we sometimes want to only require things at certain points. We also conditionally require polyfills.
Could we add a configuration option to allow
require()
insideno-commonjs
? Alternatively, would it make sense to split the rule intono-commonjs-require
andno-commonjs-exports
?The text was updated successfully, but these errors were encountered: