Skip to content

duowb/export-sass-to-json-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

export-sass-to-json-plugin

Convert the exported sass to json

index.scss

:export {
  font-size:20px;
  color:red
}

index.js

const postcssScss = require('postcss-scss');
export default function () {
  return {
    transform(code, filePath) {
      if (filePath.endsWith('scss')) {
        const root = postcssScss.parse(code);
        const cssData = {};
        root.walkDecls((i) => {
          cssData[i.prop] = i.value
        })
        return {
          code: `export default ${JSON.stringify(cssData)}`,
          map: { mappings: '' }
        };
      }
    }
  }
}

test.js

import index from './index.scss'
console.log(index)

rollup.config.js

import { defineConfig } from 'rollup';
import sassToJsonPlugin from './index'
const pkg = require('./package.json')

export default defineConfig({
  input: 'test.js',
  output: [
    { file: pkg.main, name: 'theme', format: 'umd', sourcemap: true },
    { file: pkg.module, format: 'es', sourcemap: true },
  ],
  plugins: [
    sassToJsonPlugin()
  ]
})

About

Convert the exported sass to json

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published