@@ -17,32 +17,35 @@ const sass = require("sass");
1717const path = require ( "path" ) ;
1818const minimist = require ( "minimist" ) ;
1919const sassExtract = require ( "sass-extract" ) ;
20- const themeConfig = require ( "../src/themes/mweb-config" ) ;
21- const { adjust } = require ( "./utils" ) ;
20+ const { adjust, getFileName, getFileNameWithoutExtension } = require ( "./utils" ) ;
2221const args = minimist ( process . argv . slice ( 2 ) , {
2322 string : [ "file" , "platform" , "themeDir" , "distDir" ] ,
2423 default : {
25- platform : "mweb " , // juejin , typora
24+ platform : "mweb3 " , // mweb4 , typora
2625 themeDir : "src/themes" ,
2726 distDir : "dist/themes" ,
2827 } ,
2928} ) ;
3029
3130const platformConfig = {
32- mweb : {
31+ mweb3 : {
3332 filter : ( filename ) => / ^ m w e b - / . test ( filename ) , // 过滤需要编译的 scss 文件
3433 namer : ( filename ) => `${ filename } .css` , // scss 编译后写入的文件名
35- writer : writerForMWeb
34+ themeConfig : require ( "../src/themes/mweb-config" ) ,
35+ getThemeName : ( filePath ) => filePath . match ( / m w e b - ( .* ) \. s c s s $ / ) [ 1 ] , // ayu, lark, etc.
3636 } ,
3737 mweb4 : {
3838 filter : ( filename ) => / ^ m w e b - / . test ( filename ) ,
3939 namer : ( filename ) => `${ filename } .mwebtheme` ,
40+ themeConfig : require ( "../src/themes/mweb-config" ) ,
41+ getThemeName : ( filePath ) => filePath . match ( / m w e b - ( .* ) \. s c s s $ / ) [ 1 ] , // ayu, lark, etc.
4042 compiler : compilerForMWeb4 ,
41- writer : writerForMWeb
4243 } ,
4344 typora : {
4445 filter : ( filename ) => / ^ t y p o r a - / . test ( filename ) ,
4546 namer : ( filename ) => `${ filename } .css` ,
47+ themeConfig : require ( "../src/themes/typora-config" ) ,
48+ getThemeName : ( filePath ) => filePath . match ( / t y p o r a - ( .* ) \. s c s s $ / ) [ 1 ] , // ayu, lark, etc.
4649 } ,
4750} ;
4851
@@ -293,14 +296,15 @@ function compilerForMWeb4({ filePath }) {
293296 color : "555555" ,
294297 } ,
295298 } ;
299+ const { themeConfig } = platformConfig [ args . platform ] ;
300+ const themeName = platformConfig [ args . platform ] . getThemeName ( filePath ) ;
296301 const css = sass . renderSync ( { file : filePath } ) . css ;
297- const themeName = filePath . match ( / m w e b - ( .* ) \. s c s s $ / ) [ 1 ] ; // eg: ayu, lark, etc.
298302 const isMWeb4EditorThemeCompatible =
299303 themeConfig [ themeName ] . isMWeb4EditorThemeCompatible ;
300304
301305 if ( isMWeb4EditorThemeCompatible ) {
302306 // sass-extract 有 bug,import 优先级不正确,算出来的 final value 不正确,都是 bear-default.scss 里定义的。
303- // 所以这里不解析入口文件,只计算 bear-palettes 里的变量值 。
307+ // 所以这里不直接解析入口文件 mweb-xxx.scss,而是只解析 bear-palettes/xxx.scss 。
304308 const rendered = ( ( ) => {
305309 let variablePath = `bear-palettes/${ themeName } ` ;
306310 // 简单的适配几个特殊主题
@@ -312,8 +316,6 @@ function compilerForMWeb4({ filePath }) {
312316 case "lark" :
313317 variablePath = `mweb-lark` ; // 这个可以直接解析入口文件,因为没有 import bear-default.scss
314318 break ;
315- default :
316- break ;
317319 }
318320 return sassExtract . renderSync ( {
319321 file : `${ args . themeDir } /${ variablePath } .scss` ,
@@ -399,26 +401,14 @@ ${css}`;
399401
400402function write ( { filePath, css } ) {
401403 fs . ensureDirSync ( args . distDir ) ;
402- const { namer } = platformConfig [ args . platform ] ;
403- const filename = path . basename ( filePath ) ; // with extension
404- const outFile = `${ args . distDir } /${ namer ( path . parse ( filename ) . name ) } ` ;
405- fs . writeFile ( outFile , css , ( error ) => {
406- if ( error ) {
407- console . log ( `写入文件失败:${ outFile } ` , error ) ;
408- process . exit ( 1 ) ;
409- } else console . log ( `输出:${ outFile } ` ) ;
410- } ) ;
411- }
412-
413- function writerForMWeb ( { filePath, css } ) {
414- fs . ensureDirSync ( args . distDir ) ;
415- fs . ensureDirSync ( args . distDir + '/dark' ) ;
416- fs . ensureDirSync ( args . distDir + '/light' ) ;
417- const { namer } = platformConfig [ args . platform ] ;
418- const themeName = filePath . match ( / m w e b - ( .* ) \. s c s s $ / ) [ 1 ] ; // eg: ayu, lark, etc.
419- const filename = filePath . match ( / ( m w e b - .* ) \. s c s s $ / ) [ 1 ] ;
404+ fs . ensureDirSync ( args . distDir + "/dark" ) ;
405+ fs . ensureDirSync ( args . distDir + "/light" ) ;
406+ const { namer, themeConfig } = platformConfig [ args . platform ] ;
407+ const themeName = platformConfig [ args . platform ] . getThemeName ( filePath ) ;
420408 const isDark = themeConfig [ themeName ] . mode == "dark" ;
421- const outFile = `${ args . distDir } /${ isDark ? 'dark' : 'light' } /${ namer ( filename ) } ` ;
409+ const outFile = `${ args . distDir } /${ isDark ? "dark" : "light" } /${ namer (
410+ getFileNameWithoutExtension ( filePath )
411+ ) } `;
422412 fs . writeFile ( outFile , css , ( error ) => {
423413 if ( error ) {
424414 console . log ( `写入文件失败:${ outFile } ` , error ) ;
@@ -441,11 +431,24 @@ function main() {
441431
442432 files . forEach ( async ( filePath ) => {
443433 try {
434+ if ( cfg . themeConfig ) {
435+ const themeName = platformConfig [ args . platform ] . getThemeName ( filePath ) ;
436+ console . log ( `检查主题配置是否存在:${ themeName } ` ) ;
437+ if ( ! cfg . themeConfig [ themeName ] ) {
438+ console . log (
439+ `主题配置不存在,需要在主题配置文件中添加配置项:${ themeName } `
440+ ) ;
441+ process . exit ( 1 ) ;
442+ }
443+ }
444+
444445 console . log ( `编译中:${ filePath } ` ) ;
445446 let css = cfg . compiler
446447 ? await cfg . compiler ( { filePath } )
447448 : await compile ( { filePath } ) ;
448- cfg . writer
449+
450+ console . log ( `写入文件中:${ filePath } ` ) ;
451+ cfg . writer
449452 ? await cfg . writer ( { filePath, css } )
450453 : await write ( { filePath, css } ) ;
451454 } catch ( err ) {
0 commit comments