Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 4293

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T10:39:06+00:00 2024-11-26T10:39:06+00:00

Stylelint plugin using Typescript

  • 61k

Stylelint is a useful tool to maintain styles code quality and lint CSS as well as its preprocessors. It already has a vast amount of rules and plugins that will cover most of the cases. But from time to time, you may need something specific to your project that has little or no value for other teams and therefore isn't worth a place in npm.

In the case of my project, global CSS imports were used multiple times across different components. This mistake, without proper attention, resulted in an additional 700kb of useless CSS. However, it could have been easily avoided with the help of a pre-commit hook and a custom Stylelint plugin.

But this article is not about the plugin itself (it is well described in a doc), but about using its TypeScript version.

What's the problem with that?

Languages used in a Stylelint project

Since Stylelint is written in JS, it is not possible to just add a reference to a plugin written in TS, meaning there can be several possible solutions:

  • precompile the TS before launching Stylelint
  • use ts-node

ts-node – TS execution engine that lets you run TS code without compiling it.

The easiest way to use a plugin

Just register ts-node in a JS config. That being said styleling.config.js needs only a few lines of code besides including the plugin in the config.

require('ts-node').register({     project: require('node:path').join(__dirname, 'tsconfig.json') }); 
Enter fullscreen mode Exit fullscreen mode

But this ease of use comes with a price. It may be suitable for smaller projects with a small number of styling files to lint. Or when Stylelint is used in a pre-commit hook (which means there will not be that many files in most cases).

Several dozen files already cause a noticeable delay of several seconds. Linting all files for rather large projects results in a Node exception since a new instance of ts-node will be created for every linted file.

Fix performance penalty

The solution for the problem described above is to register ts-node once. This can be achieved with a modified command that launches Stylelint.

node --require ts-node/register ./node_modules/stylelint/bin/stylelint.js 
Enter fullscreen mode Exit fullscreen mode

Stylelint API

If you don't want to mix TS and JS in a config file making, it invalid, it is also possible to create a script that will use the Stylelint API. The benefit is that it is possible to run linting without a custom plugin, and it is also slightly faster.

package.json

"stylelint": "node ts-node --project tsconfig.json stylelint.ts" 
Enter fullscreen mode Exit fullscreen mode

stylelint.ts

import stylelint from 'stylelint'; import config from './stylelint.config';  const [n, cmd, ...files] = process.argv;  async function lint() {   try {     const report = await stylelint.lint({         files: !files.length ? ['**/*.less'] : files,         config: {             ...config,             plugins: [                 // path to plugin             ],             rules: {                 ...config.rules                 'my-custom-rule': true             }         }     });      // check if the report has errors and exit if any   } catch {     process.exit(1);   } };  lint(); 
Enter fullscreen mode Exit fullscreen mode

Performance comparison

1 file 50 files 800 files
Vanilla JS Plugin 0.55s 1.8s 16.1s
Stylelint API 0.93s 1.8s 14.6s
ts-node singleton 2s 3.5s 16.5s
ts-node in config file 2s 4.9s max call stack error

javascriptstylelinttypescriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.