@trivago/prettier-plugin-sort-imports

repository·main·Indexed 26 days ago

https://github.com/trivago/prettier-plugin-sort-imports

A Prettier plugin that automatically sorts import declarations based on user-provided Regular Expression patterns. It supports custom grouping via the importOrder configuration, including special tokens for third-party and builtin modules. Compatible with TypeScript, JavaScript, Vue, Svelte, Ember, and Angular, with options to control specifier sorting, case sensitivity, and group separation.

Tokens
2.4K
Snippets
10
Records
28
Agent score
87%

What's inside @trivago/prettier-plugin-sort-imports

  1. Configure import ordering in Prettier

    main

    To use the plugin, add the importOrder configuration to your Prettier config file.

    Important: If you are using pnpm or Prettier v3.x, you may need to explicitly include the plugin in your configuration to ensure it is loaded correctly.

    module.exports = {
      "printWidth": 80,
      "tabWidth": 4,
      "trailingComma": "all",
      "singleQuote": true,
      "semi": true,
      "importOrder": ["^@core/(.*)$", "^@server/(.*)$", "^@ui/(.*)$", "^[./]"],
      "importOrderSeparation": true,
      "importOrderSortSpecifiers": true,
      "plugins": ["@trivago/prettier-plugin-sort-imports"]
    }
  2. Install @trivago/prettier-plugin-sort-imports

    main

    Install the plugin as a development dependency using your preferred package manager.

    Note for Vue users: If you are formatting .vue SFC files, ensure @vue/compiler-sfc is installed in your dependency tree.

    # npm
    npm install --save-dev @trivago/prettier-plugin-sort-imports
    
    # yarn
    yarn add --dev @trivago/prettier-plugin-sort-imports
    
    # pnpm
    pnpm add -D @trivago/prettier-plugin-sort-imports
  3. Run Prettier in a codebase using the plugin

    main

    To use the @trivago/prettier-plugin-sort-imports plugin in any project, first install the package, then run the Prettier CLI targeting your source files (e.g., TypeScript, TSX, or JavaScript files) with the --write flag.

    ./node_modules/.bin/prettier --write '**/*.{ts,tsx,js}'
  4. Migrate from v2.x.x to v3.x.x

    main

    When upgrading from version 2 to version 3, apply the following changes to your Prettier configuration:

    1. Rename Parser Plugins: Replace the deprecated experimentalBabelParserPluginsList with importOrderParserPlugins.
    2. Sort Import Specifiers: Use the importOrderSortSpecifiers boolean flag to enable sorting of specifiers within an import declaration (e.g., { b, a } becomes { a, b }).
    3. Control Third-Party Placement: Use the special <THIRD_PARTY_MODULES> token within your importOrder array to define exactly where third-party imports should be positioned relative to your local/internal modules.
    4. Case Sensitivity: Use importOrderCaseInsensitive to disable case-sensitive sorting if desired.
    5. Group Separation: Use importOrderSeparation to add separation between different import groups.
  5. Debug the plugin using Node inspector

    main

    To debug the plugin's execution, you can insert debugger statements in the source code and run Prettier with the --inspect-brk flag. This command points to the compiled plugin entry point and uses the local configuration to format the example file.

    yarn run compile && node --inspect-brk ./node_modules/.bin/prettier --config .prettierrc --plugin lib/src/index.js examples/example.ts
  6. Debug unit tests using Node inspector

    main

    To debug the project's unit tests, insert debugger statements in the test files and run Jest with the --inspect-brk flag.

    To run all tests:

    node --inspect-brk ./node_modules/.bin/jest -i

    To debug a specific unit test file, provide the name or relative path:

    node --inspect-brk ./node_modules/.bin/jest -i <name-or-relative-path-of-the-file-file>
  7. Configure importOrderParserPlugins

    main

    A collection of plugins for the Babel parser to support specific syntaxes. This is useful for frameworks like Angular or when using experimental decorators.

    Type: Array<string>
    Default: ["typescript", "jsx"]

    To pass options to a plugin, pass it as a JSON string within the array: "[\"decorators\", { \"decoratorsBeforeExport\": true }]".