Skip to content
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

Request: rule for banning synchronous imports of arbitrary packages #1245

Open
ryami333 opened this issue Dec 17, 2018 · 1 comment
Open

Comments

@ryami333
Copy link

ryami333 commented Dec 17, 2018

Managing bundle sizes and enforcing code-splitting in large applications can be delicate, and there is currently no mechanism which I know of to enforce that specific libraries must not be imported with ordinary import moment from 'moment'; syntax, and instead must be split like const momentPromise = import('moment').

Proposed API:

  "rules": {
    "import/no-synchronous-dependencies": ["error", {
      "packages": ['moment', 'zxcvbn']
    }]
  }

Example No. 1, errors:

import moment from 'moment'

Example No. 2, errors:

const moment = require('moment')

Example No. 3, passes

const momentLoader = import('moment')

Would there be an appetite for this? If there would be, I would consider making a PR of my own.

@ljharb
Copy link
Member

ljharb commented Dec 17, 2018

This already exists overall: no-restricted-imports and no-restricted-modules.

I’d say the import rule could perhaps have an extra config option that indicates whether it’s redtricted for sync, async, or both.

However, both of those are core rules (not in this plugin), import() is stage 3, eslint only supports stage 4 and above, and i don’t think it’d make sense to add an entire rule here when it’d be a simple option in the future in core.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants