Another sass compiler written in typescript from scratch, runnable both in node and browser environment
This project(Not Production Ready) is for people who want to understand how to write a compiler (zero dependencies) , compile steps:
- sourceCode (scanning)
- tokens (parsing)
- syntaxTree (analysis)
- intermediateRepresentation or IR (code generation + sourceMap)
- highLevelLanguage (css)
- Variables
- Nesting
- Extend/Inheritance
- Operators
- Mixins
- Modules (@import and @use(which is more efficient than @import))
npm install --save tiny-sass-compiler
import sass from "tiny-sass-compiler";
//render API
sass.render({filename:'./default.scss'},(err,result)=>{
console.log(result.code)
})
// or renderSync
const result = sass.renderSync({filename:'./default.scss'})
console.log(result.code)
import {compile} from 'tiny-sass-compiler/dist/tiny-sass-compiler.esm-browser.prod.js'
const result = compile(`
$font-stack: Helvetica, sans-serif;
$primary-color: #333;
body .test{
font: 100% $font-stack;
color: $primary-color;
}`)
console.log(result.code)
npm install -g tiny-sass-compiler
Support .scss extension for now
tiny-sass <input> [output]
The input
and output
must be a directory
Example
tiny-sass src/ dist/
will generate intermediate AST file in dist/ast and css file in dist/css
npm run test
will generate intermediate AST file in test-dist/ast and css file in test-dist/css
$font-stack: Helvetica, sans-serif;
$primary-color: #333;
body .test{
font: 100% $font-stack;
color: $primary-color;
}
CSS
body .test {
font: 100% Helvetica, sans-serif;
color: #333;
}
npm run jest
Interested in more intermediate status? Please view files in ./test-dist/ which contains ast after parse | transform and code after code generation