npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sass-ast

v0.0.2

Published

Sass abstract syntax tree generator

Downloads

56

Readme

sass-ast

DEPRECATED. This project has evolved into Sass Thematic

Designed to reconstitute a Sass codebase into a single Abstract Syntax Tree (AST) of lexically-parsed grammar. This is useful for performing validations and/or extracting portions of your Sass styleset for tailored purposes.

Under the hood, sass-ast is just a lightweight bridge between the fabulous gonzales-pe CSS lexer, and file-importer for reconstituiting source trees.

Install

Install NPM package:

npm install sass-ast --save-dev

Usage

var sassAST = require('sass-ast');

sassAST.parse({
    file: 'lib/test',
    includePaths: ['./styles/']
  },
  function(err, ast) {
    if (err) throw err;
    console.log(ast);
  });

sassAST.parse( options, callback )

SassAST is a wrapper around file-importer, which is designed to generally mirror the configuration of node-sass.

Required options, one or both:

  • file: String path to the file to load and parse. This may be an absolute path, or else a relative path from process.cwd() (or the provided cwd option). Uses ./ by default.

  • data: String data to parse. When provided, file read is skipped and the provided string is parsed as file contents. You may still provide a file option as path context for mapping imports.

Optional options:

  • cwd: Path of the directory to resolve file reference and includePaths from. Uses process.cwd() by default.

  • includePaths: Array of base paths to search while perform file lookups. These should be absolute directory paths, or else relative to process.cwd() (or the provided cwd option).

  • extensions: Array of file extensions to search while performing lookups. Set as ['.scss'] by default (for standard Sass import behavior). You could set this to, say, ['.txt'] to import a tree of plain text files.

How it works

  • file-importer is used to reconstitute the file tree of a Sass codebase, combining files referenced via @import statements.

  • As files are imported, each file is parsed into an AST by gonzales, and then merged into its parent AST in place of the original @import rule. Imported stylesheets are assigned importer and file properties to annotate where and how the source was loaded.

The net result is a complete gonzales AST object, composed of deeply-nested source trees. Each imported stylesheet retains its own stylesheet node and line numbers. To flatten a source tree and its line numbering, you may call .toCSS('scss') on the full tree, and then reparse it.

Example:

In index.scss:

@import 'sibling';
.index {}

In sibling.scss:

.sibling {}

Run sassAST parser:

var sassAST = require('sass-ast');

sassAST.parse({file: 'test/index'}, function(err, ast) {
  if (err) throw err;
  console.log(JSON.stringify(ast));
});

Resulting (abbreviated) Gonzales tree:

{
  "type": "stylesheet",
  "content": [
    {
      "type": "stylesheet",
      "content": [
        {
          "type": "ruleset",
          "content": [ "~~ AST: .sibling {} ~~" ],
          "start": { },
          "end": { }
        }
      ],
      "start": { },
      "end": { },
      "importer": [ "~~ AST: @import 'sibling'; ~~" ],
      "file": "/path/to/test/sibling.scss"
    },
    {
      "type": "space",
      "content": "\n",
      "start": { },
      "end": { }
    },
    {
      "type": "ruleset",
      "content": [ "~~ AST: .index {} ~~" ],
      "start": { },
      "end": { }
    }
  ],
  "start": { },
  "end": { },
  "importer": [],
  "file": "/path/to/test/index.scss"
}

Calling the Gonzales .toCSS('scss') on this tree would yield:

.sibling {}

.index {}

Issues

This library is a tiny bridge between two primary tools that do the heavy lifting. Most issues should probably be reported directly to their specific project:

Thanks! Reporting directly to the appropriate project will make sure your report is reviewed.