1
0
mirror of https://github.com/nolanlawson/pinafore synced 2024-12-12 18:28:03 +01:00
Pinafore-Web-Client-Frontend/bin/build-sass.js

43 lines
1.7 KiB
JavaScript
Raw Normal View History

import sass from 'sass'
import path from 'path'
import fs from 'fs'
import { promisify } from 'util'
import cssDedoupe from 'css-dedoupe'
import { TextDecoder } from 'text-encoding'
const writeFile = promisify(fs.writeFile)
const readdir = promisify(fs.readdir)
2018-01-14 02:41:15 +01:00
const globalScss = path.join(__dirname, '../src/scss/global.scss')
const defaultThemeScss = path.join(__dirname, '../src/scss/themes/_default.scss')
const customScrollbarScss = path.join(__dirname, '../src/scss/custom-scrollbars.scss')
const themesScssDir = path.join(__dirname, '../src/scss/themes')
const assetsDir = path.join(__dirname, '../static')
2018-01-14 02:41:15 +01:00
async function renderCss (file) {
return sass.renderSync({ file, outputStyle: 'compressed' }).css
}
2018-01-14 02:41:15 +01:00
async function compileGlobalSass () {
2019-08-03 22:49:37 +02:00
const mainStyle = (await Promise.all([defaultThemeScss, globalScss].map(renderCss))).join('')
const scrollbarStyle = (await renderCss(customScrollbarScss))
2018-01-14 02:41:15 +01:00
return `<style>\n${mainStyle}</style>\n` +
`<style media="all" id="theScrollbarStyle">\n${scrollbarStyle}</style>\n`
2018-01-14 02:41:15 +01:00
}
2018-02-09 07:29:29 +01:00
async function compileThemesSass () {
const files = (await readdir(themesScssDir)).filter(file => !path.basename(file).startsWith('_') && !path.basename(file).startsWith('.'))
2018-01-14 02:41:15 +01:00
await Promise.all(files.map(async file => {
let css = await renderCss(path.join(themesScssDir, file))
css = cssDedoupe(new TextDecoder('utf-8').decode(css)) // remove duplicate custom properties
2019-08-03 22:49:37 +02:00
const outputFilename = 'theme-' + path.basename(file).replace(/\.scss$/, '.css')
await writeFile(path.join(assetsDir, outputFilename), css, 'utf8')
2018-01-14 02:41:15 +01:00
}))
}
export async function buildSass () {
2019-08-03 22:49:37 +02:00
const [result] = await Promise.all([compileGlobalSass(), compileThemesSass()])
return result
2018-01-14 02:41:15 +01:00
}