2020-05-29 05:06:24 +01:00
|
|
|
import sass from 'sass'
|
2018-12-18 01:21:29 +00:00
|
|
|
import path from 'path'
|
|
|
|
import fs from 'fs'
|
2019-01-19 23:06:25 +00:00
|
|
|
import { promisify } from 'util'
|
2019-02-12 05:04:19 +00:00
|
|
|
import cssDedoupe from 'css-dedoupe'
|
2021-07-05 04:19:04 +01:00
|
|
|
import textEncodingPackage from 'text-encoding'
|
|
|
|
const { TextDecoder } = textEncodingPackage
|
2018-12-18 01:21:29 +00:00
|
|
|
|
2019-01-19 23:06:25 +00:00
|
|
|
const writeFile = promisify(fs.writeFile)
|
|
|
|
const readdir = promisify(fs.readdir)
|
2021-07-05 04:19:04 +01:00
|
|
|
const __dirname = path.dirname(new URL(import.meta.url).pathname)
|
2018-01-14 01:41:15 +00:00
|
|
|
|
2019-01-19 21:32:36 +00: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')
|
2018-12-11 15:31:48 +00:00
|
|
|
const assetsDir = path.join(__dirname, '../static')
|
2018-01-14 01:41:15 +00:00
|
|
|
|
2018-11-24 08:41:36 +00:00
|
|
|
async function renderCss (file) {
|
2020-05-29 05:06:24 +01:00
|
|
|
return sass.renderSync({ file, outputStyle: 'compressed' }).css
|
2018-11-24 08:41:36 +00:00
|
|
|
}
|
2018-01-14 01:41:15 +00:00
|
|
|
|
2018-11-24 08:41:36 +00:00
|
|
|
async function compileGlobalSass () {
|
2019-08-03 21:49:37 +01:00
|
|
|
const mainStyle = (await Promise.all([defaultThemeScss, globalScss].map(renderCss))).join('')
|
|
|
|
const scrollbarStyle = (await renderCss(customScrollbarScss))
|
2018-01-14 01:41:15 +00:00
|
|
|
|
2018-12-18 01:21:29 +00:00
|
|
|
return `<style>\n${mainStyle}</style>\n` +
|
|
|
|
`<style media="all" id="theScrollbarStyle">\n${scrollbarStyle}</style>\n`
|
2018-01-14 01:41:15 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
async function compileThemesSass () {
|
2020-01-12 18:19:29 +00:00
|
|
|
const files = (await readdir(themesScssDir)).filter(file => !path.basename(file).startsWith('_') && !path.basename(file).startsWith('.'))
|
2018-01-14 01:41:15 +00:00
|
|
|
await Promise.all(files.map(async file => {
|
2019-02-12 05:04:19 +00:00
|
|
|
let css = await renderCss(path.join(themesScssDir, file))
|
|
|
|
css = cssDedoupe(new TextDecoder('utf-8').decode(css)) // remove duplicate custom properties
|
2019-08-03 21:49:37 +01:00
|
|
|
const outputFilename = 'theme-' + path.basename(file).replace(/\.scss$/, '.css')
|
2019-02-12 05:04:19 +00:00
|
|
|
await writeFile(path.join(assetsDir, outputFilename), css, 'utf8')
|
2018-01-14 01:41:15 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2018-12-18 01:21:29 +00:00
|
|
|
export async function buildSass () {
|
2019-08-03 21:49:37 +01:00
|
|
|
const [result] = await Promise.all([compileGlobalSass(), compileThemesSass()])
|
2018-12-18 01:21:29 +00:00
|
|
|
return result
|
2018-01-14 01:41:15 +00:00
|
|
|
}
|