Skip to content

Commit 09227c8

Browse files
authored
chore(harmonycss): 每个组件构建全量的css (#3316)
1 parent 8a1c4a0 commit 09227c8

File tree

1 file changed

+64
-4
lines changed

1 file changed

+64
-4
lines changed

scripts/build-taro.mjs

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,70 @@ async function buildCSS(themeName = '') {
428428
join(`${dist}/cjs`, cssPath, `${themeDir}/css.js`),
429429
cssContent.join('\n'),
430430
)
431-
// copy harmonycss
431+
}
432+
}
433+
434+
// 构建样式
435+
async function buildHarmonyCSS(themeName = '') {
436+
const componentScssFiles = await glob(['src/packages/**/*.scss'], {
437+
ignore: ['src/packages/**/demo.scss'],
438+
})
439+
440+
const variables = await readFile(
441+
join(
442+
__dirname,
443+
`../src/styles/variables${themeName ? `-${themeName}` : ''}.scss`,
444+
),
445+
)
446+
for (const file of componentScssFiles) {
447+
const scssContent = await readFile(join(__dirname, '../', file), {
448+
encoding: 'utf8',
449+
})
450+
// countup 是特例
451+
const base = basename(file)
452+
const loadPath = join(
453+
__dirname,
454+
'../src/packages',
455+
base.replace('.scss', ''),
456+
)
457+
const cssPath = relative('src', loadPath)
458+
// 删除 import
459+
// 写入 style.scss
460+
const atRules = []
461+
const postcssRes = await postcss([
462+
{
463+
postcssPlugin: 'remove-atrule',
464+
AtRule(root) {
465+
if (root.name === 'import') {
466+
if (root.params.indexOf('\'../../styles') > -1) {
467+
atRules.push(root.params)
468+
root.params = root.params.replace('../../', '../../../../')
469+
return
470+
}
471+
}
472+
},
473+
},
474+
])
475+
.process(scssContent, { from: loadPath, syntax: scss })
476+
.then((result) => {
477+
return result
478+
})
479+
const themeDir = themeName ? `style-${themeName}` : 'style'
480+
const code = sass.compileString(
481+
variables + '\n' + postcssRes.css.replaceAll('../../../../', '../../'),
482+
{
483+
loadPaths: [loadPath],
484+
},
485+
)
432486
if (file.indexOf('countup') === -1) {
433-
const harmonyCss = join(__dirname, '../', file.replace('scss', 'harmony.css'))
434-
await copy(harmonyCss, join(`${dist}/cjs`, cssPath, 'style/style.harmony.css'))
435-
await copy(harmonyCss, join(`${dist}/es`, cssPath, 'style/style.harmony.css'))
487+
await dest(
488+
join(`${dist}/es`, cssPath, `${themeDir}/style.harmony.css`),
489+
code.css,
490+
)
491+
await dest(
492+
join(`${dist}/cjs`, cssPath, `${themeDir}/style.harmony.css`),
493+
code.css,
494+
)
436495
}
437496
}
438497
}
@@ -497,6 +556,7 @@ console.timeEnd('Copy Styles')
497556

498557
console.time('Build CSS')
499558
await buildCSS()
559+
await buildHarmonyCSS()
500560
console.timeEnd('Build CSS')
501561

502562
console.time('Build jmapp CSS')

0 commit comments

Comments
 (0)