question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

import/order doesn't sort assets that aren't assigned to a variable (e.g. `import './file.css'`)

See original GitHub issue

When sorting imports, any assets that aren’t assigned to a variable are ignored, and break/mess with sorting of other imports.

e.g.

import mapboxgl from 'mapbox-gl';

import './file.json';
import './file.css';
import './file.png';
import React from 'react';
import { ToastContainer } from 'react-toastify';

import { ErrorBoundary } from '@app/components';
import { useAccount, useUser } from 'src/state';

import mygif from './file.gif';
import Routes from './Routes';

If I change the above and assign assets to a variable, sorting is applied as expected.

import mapboxgl from 'mapbox-gl';
import React from 'react';
import { ToastContainer } from 'react-toastify';

import { ErrorBoundary } from '@app/components';
import { useAccount, useUser } from 'src/state';

import b from './file.css';
import mygif from './file.gif';
import a from './file.json';
import c from './file.png';
import Routes from './Routes';

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
ljharbcommented, Feb 13, 2020

I totally agree that it’s a smell - but a rule that risks making people do the wrong thing, without knowing why, isn’t a good rule.

It might be possible to make this warnable, but never to make it autofixable.

3reactions
quezakcommented, Feb 12, 2020

Your arguments are correct for complicated cases, but it would be nice if you could optionally configure to do something with the side-effect-only imports. I’m currently using a vscode plugin (TypeScript Import Sorter) that puts them on the top, in a separate group, plus a tslint rule that checks that.

IMHO side-effect imports should be used cautiously and only if absolutely necessary, so

  • it’s a good thing to explicitly separate them to make them stand out
  • while in some cases the order of side-effect imports may be important, if they need to be mixed in between regular imports, it’s a bad thing (and IMHO starts to smell of bad code design)

I’m using side-effect imports only for “global” stuff like reflect-metadata & tsconfig-paths/register on the backend, some jest plugins in tests, or storybook plugins on the frontend. I’ve just moved from tslint to eslint, and I’d like to keep enforcing grouping side-effect imports together 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues - GitHub
import /order doesn't sort assets that aren't assigned to a variable (e.g. import './file.css' ) #1639.
Read more >
Next.js Global CSS cannot be imported from files other than ...
js, and it was previously working? I've moved my main.scss import to the pages/_app.js page, but the styles still aren't coming through. This ......
Read more >
import - CSS: Cascading Style Sheets - MDN Web Docs
The @import CSS at-rule is used to import style rules from other valid stylesheets. An @import rule must be defined at the top...
Read more >
The Django admin site
from django.contrib import admin from myapp.models import Author class ... In the preceding example, the ModelAdmin class doesn't define any custom values ...
Read more >
Example Code To Simplify Your Imports and Exports
It can also be due to odd import file formats that aren't fully compatible ... WP All Import doesn't touch product stock when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found