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 rule(s) to sort keys in nuxt.config.ts to keep everything in order #465

Closed
2 of 4 tasks
stefanobartoletti opened this issue Jul 9, 2024 · 6 comments · Fixed by #491
Closed
2 of 4 tasks

Add rule(s) to sort keys in nuxt.config.ts to keep everything in order #465

stefanobartoletti opened this issue Jul 9, 2024 · 6 comments · Fixed by #491
Labels
enhancement New feature or request

Comments

@stefanobartoletti
Copy link

Describe the feature

As a possible new feature, I think that it could be useful to have a rule to keep nuxt.config.ts clean and tidy by sorting its keys with specific criteria.

The inspiration for this is the jsonc/sort-keys rule that is often used to keep package.json ordered according to a specific pattern.

Especially when a project grows, nuxt.config.ts can become quite large and contain many configuration items.

A possible example could be

export default defineNuxtConfig({
  // place "modules" section first, and keep all entries alphabetically sorted.
  modules: [
    '@nuxt/image',
    '@nuxtjs/seo',
    'nuxt-icon',
    // and so on
  ],
  
  // other builtin config items
  css: ['~/assets/css/fontface.css'],
  components: { 
    // ...
  }
  
  // modules options, maybe alphabetically
  
  image: {
    //
  }
  
  // and so on

})

This is just an example of possible sorting criteria, but there can be even better I think.

I don't know if it is a good idea to have some preset rules for this, or if it is just something best left to the user.

Additional information

  • Would you be willing to help implement this feature?
  • Could this feature be implemented as a module?

Final checks

@antfu antfu added enhancement New feature or request and removed pending triage labels Aug 29, 2024
@antfu
Copy link
Member

antfu commented Aug 29, 2024

I'd love to have that! It does not seem very hard to implement, but it more like how we can agree with a specific order.

Here are the top-level keys we have extracted from our schema file.

[
  "components",
  "imports",
  "pages",
  "telemetry",
  "devtools",
  "vue",
  "app",
  "spaLoadingTemplate",
  "plugins",
  "css",
  "unhead",
  "builder",
  "sourcemap",
  "logLevel",
  "build",
  "optimization",
  "extends",
  "compatibilityDate",
  "theme",
  "rootDir",
  "workspaceDir",
  "srcDir",
  "serverDir",
  "buildDir",
  "appId",
  "buildId",
  "modulesDir",
  "analyzeDir",
  "dev",
  "test",
  "debug",
  "ssr",
  "modules",
  "dir",
  "extensions",
  "alias",
  "ignoreOptions",
  "ignorePrefix",
  "ignore",
  "watch",
  "watchers",
  "hooks",
  "runtimeConfig",
  "appConfig",
  "$schema",
  "devServer",
  "future",
  "features",
  "experimental",
  "generate",
  "_majorVersion",
  "_legacyGenerate",
  "_start",
  "_build",
  "_generate",
  "_prepare",
  "_cli",
  "_requiredModules",
  "_nuxtConfigFile",
  "_nuxtConfigFiles",
  "appDir",
  "_installedModules",
  "_modules",
  "nitro",
  "routeRules",
  "serverHandlers",
  "devServerHandlers",
  "postcss",
  "router",
  "typescript",
  "vite",
  "webpack"
]

@antfu
Copy link
Member

antfu commented Aug 29, 2024

I roughly ordered them from my understanding: https://github.com/nuxt/eslint/blob/feat/rule-config-orders/packages/eslint-plugin/src/rules/nuxt-config-keys-order/keys.ts

/cc @danielroe @atinux Do you like this direction (later we could enable it in @nuxt/eslint? If so, do you have any suggestions for the order?

@stefanobartoletti
Copy link
Author

stefanobartoletti commented Aug 29, 2024

Thanks @antfu that looks awesome!

I like the general idea that seems to emerge, so those that probably are the most used keys are close to the top (in example, it is very handy to have modules visible without having to scroll the file).

Also, for sub-keys, in example the list of installed modules, a simple alphabetical sorting could be enforced too, to guarantee an order even there (helpful for diff's)

I would add that in addition to the official keys, there will be the modules config entries, and maybe they can be put at the end of the official keys in a simple alphabetical order too.

@dsvgl
Copy link

dsvgl commented Aug 29, 2024

Please then make this an opt-in. Or at least opt-out. 🙏🏻

@manniL
Copy link
Member

manniL commented Aug 30, 2024

@dsvgl As it is an ESLint rule, you can always configure/override it 😋

What'd be your preference on the rule itself?

@dsvgl
Copy link

dsvgl commented Aug 30, 2024

@manniL yes, overriding is possible. I don’t want this feature at all so, I‘d rather have a module option (like currently done for eg. tooling and stylistic) to opt-out instead of manually overriding this in the eslint.config file.
Also, not sure if having it „on“ by default is the best default. Other module options are initially false. Having it being false too (opt-in) would be more in line with the other options.

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

Successfully merging a pull request may close this issue.

4 participants