Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
scss-parser
Advanced tools
The scss-parser npm package is a tool for parsing SCSS (Sassy CSS) code into an Abstract Syntax Tree (AST). This allows developers to analyze, manipulate, and transform SCSS code programmatically.
Parsing SCSS to AST
This feature allows you to parse SCSS code into an Abstract Syntax Tree (AST). The AST can then be used for further analysis or transformation of the SCSS code.
const scssParser = require('scss-parser');
const input = '$color: #333; .class { color: $color; }';
const ast = scssParser.parse(input);
console.log(JSON.stringify(ast, null, 2));
Traversing the AST
This feature allows you to traverse the AST to find specific nodes, such as variables, mixins, or rules. This can be useful for analyzing or modifying specific parts of the SCSS code.
const scssParser = require('scss-parser');
const createQueryWrapper = require('query-ast');
const input = '$color: #333; .class { color: $color; }';
const ast = scssParser.parse(input);
const $ = createQueryWrapper(ast);
const variables = $.find('variable');
variables.each((node) => {
console.log(node);
});
Modifying the AST
This feature allows you to modify the AST, which can be useful for transforming SCSS code. For example, you can change variable names, update values, or add new rules.
const scssParser = require('scss-parser');
const createQueryWrapper = require('query-ast');
const input = '$color: #333; .class { color: $color; }';
const ast = scssParser.parse(input);
const $ = createQueryWrapper(ast);
const variables = $.find('variable');
variables.each((node) => {
node.value.value = '$new-color';
});
console.log(JSON.stringify(ast, null, 2));
PostCSS is a tool for transforming CSS with JavaScript plugins. It can parse CSS into an AST, which can then be modified and stringified back into CSS. While it is primarily focused on CSS, it can also handle SCSS with the help of plugins. Compared to scss-parser, PostCSS offers a broader range of plugins and functionalities for CSS processing.
Gonzales-PE is a CSS parser that can handle various CSS-like syntaxes, including SCSS, Sass, and Less. It parses code into an AST, which can be traversed and modified. Compared to scss-parser, Gonzales-PE supports a wider range of syntaxes but may have a steeper learning curve.
Rework is a plugin framework for CSS preprocessing. It parses CSS into an AST, which can be transformed using plugins. While it is not specifically designed for SCSS, it can be extended to support SCSS with additional plugins. Compared to scss-parser, Rework is more focused on CSS and may require more setup to handle SCSS.
let { parse, stringify } = require('scss-parser')
// Create an AST from a string of SCSS
let ast = parse('.hello { color: $red; }')
// Modify the AST (see below for a better way to do this)
ast.value[0].value[0].value[0].value[0].value = 'world'
// Convert the modified AST back to SCSS
let scss = stringify(ast) // .world { color: $red; }
For an easy way to traverse/modify the generated AST, check out QueryAST
let { parse, stringify } = require('scss-parser')
let createQueryWrapper = require('query-ast')
// Create an AST
let ast = parse('.hello { color: red; } .world { color: blue; }')
// Create a function to traverse/modify the AST
let $ = createQueryWrapper(ast)
// Make some modifications
$('rule').eq(1).remove()
// Convert the modified AST back to a string
let scss = stringify($().get(0))
Clone the repository, then:
npm install
# requires node >= 5.0.0
npm test
Copyright (c) 2016, salesforce.com, inc. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of salesforce.com, inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
FAQs
A library to parse/stringify SCSS
We found that scss-parser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.