|
1 | | -import { readFile } from 'node:fs/promises'; |
| 1 | +import { mkdir, readFile, writeFile } from 'node:fs/promises'; |
| 2 | +import path from 'node:path'; |
2 | 3 | import { resolve } from 'node:path'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
3 | 5 | import autoprefixer from 'autoprefixer'; |
| 6 | +import { globby } from 'globby'; |
4 | 7 | import postcss from 'postcss'; |
| 8 | +import * as sass from 'sass-embedded'; |
| 9 | +import report from './report.mjs'; |
| 10 | + |
| 11 | +const toDist = path.join.bind( |
| 12 | + null, |
| 13 | + path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../dist') |
| 14 | +); |
5 | 15 |
|
6 | 16 | const stripComments = () => { |
7 | 17 | return { |
@@ -32,3 +42,87 @@ export async function compileSass(src, compiler) { |
32 | 42 | const out = _postProcessor.process(compiled.css).css; |
33 | 43 | return out.charCodeAt(0) === 0xfeff ? out.slice(1) : out; |
34 | 44 | } |
| 45 | + |
| 46 | +export async function buildThemes(isProduction = false) { |
| 47 | + const start = performance.now(); |
| 48 | + |
| 49 | + const [compiler, paths] = await Promise.all([ |
| 50 | + sass.initAsyncCompiler(), |
| 51 | + globby('src/styles/themes/{light,dark}/*.scss'), |
| 52 | + ]); |
| 53 | + |
| 54 | + try { |
| 55 | + await Promise.all( |
| 56 | + paths.map(async (sassFile) => { |
| 57 | + const outputFile = isProduction |
| 58 | + ? toDist( |
| 59 | + sassFile.replace(/\.scss$/, '.css').replace('src/styles/', '') |
| 60 | + ) |
| 61 | + : sassFile.replace(/\.scss$/, '.css.ts'); |
| 62 | + |
| 63 | + if (isProduction) { |
| 64 | + await mkdir(path.dirname(outputFile), { recursive: true }); |
| 65 | + writeFile(outputFile, await compileSass(sassFile, compiler), 'utf-8'); |
| 66 | + } else { |
| 67 | + writeFile( |
| 68 | + outputFile, |
| 69 | + fromTemplate(await compileSass(sassFile, compiler)), |
| 70 | + 'utf-8' |
| 71 | + ); |
| 72 | + } |
| 73 | + }) |
| 74 | + ); |
| 75 | + } catch (err) { |
| 76 | + await compiler.dispose(); |
| 77 | + report.error(err); |
| 78 | + process.exit(1); |
| 79 | + } |
| 80 | + |
| 81 | + await compiler.dispose(); |
| 82 | + |
| 83 | + if (!isProduction) { |
| 84 | + report.success( |
| 85 | + `Themes generated in ${((performance.now() - start) / 1000).toFixed(2)}s` |
| 86 | + ); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +export async function buildComponents(isProduction = false) { |
| 91 | + const start = performance.now(); |
| 92 | + const [compiler, paths] = await Promise.all([ |
| 93 | + sass.initAsyncCompiler(), |
| 94 | + globby([ |
| 95 | + 'src/components/**/*.base.scss', |
| 96 | + 'src/components/**/*.common.scss', |
| 97 | + 'src/components/**/*.shared.scss', |
| 98 | + 'src/components/**/*.material.scss', |
| 99 | + 'src/components/**/*.bootstrap.scss', |
| 100 | + 'src/components/**/*.indigo.scss', |
| 101 | + 'src/components/**/*.fluent.scss', |
| 102 | + ]), |
| 103 | + ]); |
| 104 | + |
| 105 | + try { |
| 106 | + await Promise.all( |
| 107 | + paths.map(async (path) => |
| 108 | + writeFile( |
| 109 | + path.replace(/\.scss$/, '.css.ts'), |
| 110 | + fromTemplate(await compileSass(path, compiler)), |
| 111 | + 'utf-8' |
| 112 | + ) |
| 113 | + ) |
| 114 | + ); |
| 115 | + } catch (err) { |
| 116 | + await compiler.dispose(); |
| 117 | + report.error(err); |
| 118 | + process.exit(1); |
| 119 | + } |
| 120 | + |
| 121 | + await compiler.dispose(); |
| 122 | + |
| 123 | + if (!isProduction) { |
| 124 | + report.success( |
| 125 | + `Component styles generated in ${((performance.now() - start) / 1000).toFixed(2)}s` |
| 126 | + ); |
| 127 | + } |
| 128 | +} |
0 commit comments