Skip to content

Latest commit

 

History

History
58 lines (51 loc) · 1.06 KB

README.md

File metadata and controls

58 lines (51 loc) · 1.06 KB

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()
  ]
})