Git Product home page Git Product logo

js-imports-sort's Introduction

js-imports-sort

A simple script to sort Javascript imports the way I want

Rules

Order

  1. No names (side-effect)
    import "module"
  2. Everything
    import * from "module"
  3. Defaults
    import a from "module"
  4. Named
    import { a, b } from "module"

Sorting

  • The source is prioritized over the import
  • Special characters comes first
  • Numbers comes second
  • Lowercase letters comes third
  • Uppercase letters comes fourth
  • In case an import contains both defaults and named, the defaults are prioritized and put first

Features

  • Supports multiline imports
  • Adds a newline between imports from different types

Limitations

  • Doesn't work yet with require() syntax
  • Can't really sort the imports from a module (ex : import {z, b}, s, a from "c" becomes import a, s, {b, z} from "c"), due to how it perceives mixed imports
  • Doesn't support semicolons at the end of the import
  • Doesn't support comments
  • Doesn't deduplicate imports
  • ESLint plugin not yet implemented

Example

import "@mdi/font/css/materialdesignicons.css"
import "vuetify/styles"

import * from "node:fs"

import js from "@eslint/js"
import vuetify, { transformAssetUrls } from "vite-plugin-vuetify"

import { defineNuxtPlugin } from "#app"
import { createVuetify } from "vuetify"
import {
  en,
  fr
} from "vuetify/locale"

Usage

Install the package

npm install js-imports-sort
  • In a javascript file
import sortImports from "js-imports-sort"

const code = `
import { createVuetify } from "vuetify"
import js from "@eslint/js"
import vuetify, { transformAssetUrls } from "vite-plugin-vuetify"
import * from "node:fs"
import { defineNuxtPlugin } from "#app"
import "vuetify/styles"
import {
  en,
  fr
} from "vuetify/locale"
import "@mdi/font/css/materialdesignicons.css"
`

const sortedCode = sortImports(code)
console.log(sortedCode)
  • In a shell
npx js-imports-sort@latest <file> -r

Params available :

  • -r or --replace : Replace the file content with the sorted imports
  • -o <path> or --output <path> : Output the sorted imports in a different file. Have to be used with -r
  • -l or --log : Output the sorted imports in the console

At least one of -r or -l has to be used

js-imports-sort's People

Contributors

edm115 avatar mend-bolt-for-github[bot] avatar renovate[bot] avatar

Stargazers

 avatar  avatar

Watchers

 avatar

js-imports-sort's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

npm
package.json
  • @babel/core 7.24.5
  • @babel/preset-env 7.24.5
  • @rollup/plugin-babel 6.0.4
  • @rollup/plugin-commonjs 25.0.8
  • @rollup/plugin-node-resolve 15.2.3
  • cross-env 7.0.3
  • rimraf 5.0.7
  • rollup 4.18.0
  • rollup-plugin-visualizer 5.12.0

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.