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.

Does jscodeshift work with css/scss/etc..?

See original GitHub issue

Hey there,

First off, thanks for the awesome looking tool. I thought I’d write a simple codemod to change px to em’s and wanted to use jscodeshift to do it.

Here’s the transform I’m using: http://astexplorer.net/#/usDeoLTPFE/2

And here’s the jscodeshift command I was running:

jscodeshift . -t ./codemods/scss-px-to-em.js -v 1 --extensions scss

I must be doing something wrong because the output is totally wrong. So, is it possible to use jscodeshift to do postcss transforms?

Thanks very much!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
benjamncommented, Jun 28, 2016

The techniques that recast uses are generic enough to work for any language that has an AST, though you’d have to implement pretty-printer cases for all those AST types.

That said, CSS formatting tends to be much more regular than JS formatting, so you could more easily get away with just parsing, transforming, and pretty-printing your CSS, without so much effort to preserve idiomatic formatting.

1reaction
danieldelcorecommented, Apr 16, 2022

Hey folks, it is possible to do as suggested above via PostCSS.

I’ve written a guide here, hope it helps: www.codeshiftcommunity.com/docs/css-codemods

Here’s an example:


const plugin = (): Plugin => {
  const processed = Symbol("processed");

  return {
    postcssPlugin: "UsingTokens",
    Declaration: (decl) => {
      // Transformation goes here
    },
  };
};

export default async function transformer(file: FileInfo) {
  return await postcss([plugin()]).process(file.source).css;
}

The main downside with this approach is that you aren’t able to leverage the “API” & AST of jscodeshift and recast. So you will need to rely solely on PostCSS. Also since jscodeshift uses dependency injection to expose it’s API, distributing codemods that use dependencies may be problematic.

An idea for jscodeshift future architectural direction could be to refactor the “Runner” to be generic and allow authors of transforms to import the AST/Transformation library of choice.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Write Code to Rewrite Your Code: jscodeshift - Toptal
The jscodeshift toolkit is great for working with codemods. Think of codemods as a scripted find and replace functionality that can read and...
Read more >
Automatic refactoring with jscodeshift/codemods | by Juntao Qiu
How jscodeshift works is: firstly you need to define a transform script, this script need to respect the protocol so it can be...
Read more >
jscodeshift example | Better world by better software
Take a simple calc.js that exports a single function that adds two numbers. ... Any client can load the function directly using require('./calc') ......
Read more >
Writing your very first codemod with jscodeshift - Medium
AST: A tree representation of a piece of source code. When working with JavaScript tools that allow you to inspect/modify an AST, this...
Read more >
An Introduction to jscodeshift - The Blog of Feifan Zhou
The “real” AST is more pedantic, but has the same shape: jscodeshift provides a way to work with AST data structures: finding and...
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