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

Add option for no-rename-default to exclude some modules #136

Open
meteorlxy opened this issue Aug 28, 2024 · 6 comments
Open

Add option for no-rename-default to exclude some modules #136

meteorlxy opened this issue Aug 28, 2024 · 6 comments

Comments

@meteorlxy
Copy link

meteorlxy commented Aug 28, 2024

Some 3rd party modules may minify the released files. It would be helpful to add an option to exclude those modules manually.

For example, markdown-it-anchor:

image

Any thoughts? @SukkaW

@SukkaW
Copy link
Collaborator

SukkaW commented Aug 28, 2024

Some 3rd party modules may minify the released files.

That's a fair point, which is why this rule is included in the "warning" presets.

As for markdown-it-anchor, it does export{b as default}; after modification.

It would be helpful to add an option to exclude those modules manually.

IMHO, it is not a good idea to manually configure for every module that publishes minified dist.

SukkaW added a commit to SukkaW/eslint-plugin-import-x that referenced this issue Aug 29, 2024
SukkaW added a commit to SukkaW/eslint-plugin-import-x that referenced this issue Aug 29, 2024
SukkaW added a commit to SukkaW/eslint-plugin-import-x that referenced this issue Aug 29, 2024
SukkaW added a commit to SukkaW/eslint-plugin-import-x that referenced this issue Aug 29, 2024
@SukkaW
Copy link
Collaborator

SukkaW commented Aug 29, 2024

@meteorlxy How about we proceed this way: Ignore all defaultExportedName instances where the length is less than or equal to three.

Three characters provide 199,888 (52 * 62 * 62) possible mangled exports, which is more than sufficient for most modules as they won't have that many exports in one file or chunk. If they do, eslint-disable would suffice.

I chose three characters because two characters only yield 3,224 (52 * 62) possible mangled exports, a number that some icon libraries might easily surpass.

However, this introduces another issue: there are popular libraries, such as xo and mem, which have names shorter than three characters, and we want to validate them too.

What do you think?

@Zamiell
Copy link

Zamiell commented Aug 30, 2024

Rather than hardcode length, is it possible to detect minified modules? Could we make the assumption that a module is minified if there are no newlines in it?

@SukkaW
Copy link
Collaborator

SukkaW commented Aug 30, 2024

Rather than hardcode length, is it possible to detect minified modules? Could we make the assumption that a module is minified if there are no newlines in it?

What about comments then? webpack retains license comments by default even when minified is enabled. And webpack won't hoist those comments either.

@meteorlxy
Copy link
Author

meteorlxy commented Aug 31, 2024

Ignore all defaultExportedName instances where the length is less than or equal to three.

  • As you mentioned above, name of some famous lib could be shorter than 3. In addition, testing framework may expose it, i18n lib may expose t, etc. It might not be the default export of the module, but it's possible to be the default export of some sub path.
  • Beside 3rd party libs, users may have their own shorter default export within their source code.

Thus, ignoring exports based on char length it not ideal.


Could we make the assumption that a module is minified if there are no newlines in it?

webpack retains license comments by default even when minified is enabled. And webpack won't hoist those comments either.

We'd better not rely on those assumptions:

  1. Minifying a file does not necessarily change the name of the default export. It might minify other parts and keep the variables name. That's all configurable by the minifier.
  2. Users may manually write one-line module. It's not common but definitely possible.
  3. The comments are also optional. It could be kept or removed. All configurable, too.

IMHO, it is not a good idea to manually configure for every module that publishes minified dist.

In fact, minified module is only one of the use case of the rule options.

It's always better to provide an option for users to configure this rule in a fine-grained manner:

  • Ignore some modules
  • Override some modules' default export

For example, it could be nice to have something like this:

{
  'import/no-rename-default': [
    'warn',
    {
      modules: [
        ['markdown-it-anchor', { ignore: true }], // ignore default export name checking
        ['markdown-it-anchor', { override: 'markdownItAnchor' }], // override default export name to markdownItAnchor
      ],
    },
  ],
}

Don't think it too verbose. You can't imagine how users want to configure rules for large-scale projects.

@SukkaW
Copy link
Collaborator

SukkaW commented Aug 31, 2024

Don't think it too verbose. You can't imagine how users want to configure rules for large-scale projects.

It is a common practice to publish minified dist to npm. I personally published more than 20 npm packages that have their dist minified. Explicit configuring all of them is definitely not an option.

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

No branches or pull requests

3 participants