mirror of
				https://gitee.com/vant-contrib/vant.git
				synced 2025-10-31 03:22:08 +08:00 
			
		
		
		
	feat(cli): add css.removeSourceFile config (#10750)
This commit is contained in:
		
							parent
							
								
									e61d85a890
								
							
						
					
					
						commit
						bb23e1b700
					
				| @ -103,7 +103,7 @@ module.exports = { | |||||||
| - Type: `string` | - Type: `string` | ||||||
| - Default: `'less'` | - Default: `'less'` | ||||||
| 
 | 
 | ||||||
| CSS preprocess Config, support `less` and `sass`. Use `less` by default. | CSS preprocessor config, support `less` and `sass`. Use `less` by default. | ||||||
| 
 | 
 | ||||||
| ```js | ```js | ||||||
| module.exports = { | module.exports = { | ||||||
| @ -115,6 +115,23 @@ module.exports = { | |||||||
| }; | }; | ||||||
| ``` | ``` | ||||||
| 
 | 
 | ||||||
|  | ### build.css.removeSourceFile | ||||||
|  | 
 | ||||||
|  | - Type: `boolean` | ||||||
|  | - Default: `'false'` | ||||||
|  | 
 | ||||||
|  | Whether to remove the source style files after build. | ||||||
|  | 
 | ||||||
|  | ```js | ||||||
|  | module.exports = { | ||||||
|  |   build: { | ||||||
|  |     css: { | ||||||
|  |       removeSourceFile: true, | ||||||
|  |     }, | ||||||
|  |   }, | ||||||
|  | }; | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
| ### build.site.publicPath | ### build.site.publicPath | ||||||
| 
 | 
 | ||||||
| - Type: `string` | - Type: `string` | ||||||
|  | |||||||
| @ -115,6 +115,23 @@ module.exports = { | |||||||
| }; | }; | ||||||
| ``` | ``` | ||||||
| 
 | 
 | ||||||
|  | ### build.css.removeSourceFile | ||||||
|  | 
 | ||||||
|  | - Type: `boolean` | ||||||
|  | - Default: `'false'` | ||||||
|  | 
 | ||||||
|  | 是否在构建后移除样式文件的源代码。 | ||||||
|  | 
 | ||||||
|  | ```js | ||||||
|  | module.exports = { | ||||||
|  |   build: { | ||||||
|  |     css: { | ||||||
|  |       removeSourceFile: true, | ||||||
|  |     }, | ||||||
|  |   }, | ||||||
|  | }; | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
| ### build.site.publicPath | ### build.site.publicPath | ||||||
| 
 | 
 | ||||||
| - Type: `string` | - Type: `string` | ||||||
|  | |||||||
| @ -1,11 +1,13 @@ | |||||||
| import { parse } from 'path'; | import { parse } from 'path'; | ||||||
| import { readFileSync, writeFileSync } from 'fs'; | import fse from 'fs-extra'; | ||||||
| import { replaceExt } from '../common/index.js'; | import { getVantConfig, replaceExt } from '../common/index.js'; | ||||||
| import { compileCss } from './compile-css.js'; | import { compileCss } from './compile-css.js'; | ||||||
| import { compileLess } from './compile-less.js'; | import { compileLess } from './compile-less.js'; | ||||||
| import { compileSass } from './compile-sass.js'; | import { compileSass } from './compile-sass.js'; | ||||||
| import { consola } from '../common/logger.js'; | import { consola } from '../common/logger.js'; | ||||||
| 
 | 
 | ||||||
|  | const { readFileSync, writeFileSync, removeSync } = fse; | ||||||
|  | 
 | ||||||
| async function compileFile(filePath: string) { | async function compileFile(filePath: string) { | ||||||
|   const parsedPath = parse(filePath); |   const parsedPath = parse(filePath); | ||||||
| 
 | 
 | ||||||
| @ -30,6 +32,11 @@ async function compileFile(filePath: string) { | |||||||
| 
 | 
 | ||||||
| export async function compileStyle(filePath: string) { | export async function compileStyle(filePath: string) { | ||||||
|   const css = await compileFile(filePath); |   const css = await compileFile(filePath); | ||||||
|  |   const vantConfig = getVantConfig(); | ||||||
|  | 
 | ||||||
|  |   if (vantConfig.build?.css?.removeSourceFile) { | ||||||
|  |     removeSync(filePath); | ||||||
|  |   } | ||||||
| 
 | 
 | ||||||
|   writeFileSync(replaceExt(filePath, '.css'), css); |   writeFileSync(replaceExt(filePath, '.css'), css); | ||||||
| } | } | ||||||
|  | |||||||
| @ -12,6 +12,7 @@ import { | |||||||
|   ES_DIR, |   ES_DIR, | ||||||
|   SRC_DIR, |   SRC_DIR, | ||||||
|   LIB_DIR, |   LIB_DIR, | ||||||
|  |   getVantConfig, | ||||||
|   STYLE_DEPS_JSON_FILE, |   STYLE_DEPS_JSON_FILE, | ||||||
| } from '../common/constant.js'; | } from '../common/constant.js'; | ||||||
| 
 | 
 | ||||||
| @ -87,8 +88,10 @@ export function genComponentStyle( | |||||||
|     delete require.cache[STYLE_DEPS_JSON_FILE]; |     delete require.cache[STYLE_DEPS_JSON_FILE]; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   const vantConfig = getVantConfig(); | ||||||
|   const components = getComponents(); |   const components = getComponents(); | ||||||
|   const baseFile = getCssBaseFile(); |   const baseFile = getCssBaseFile(); | ||||||
|  |   const hasSourceFile = vantConfig.build?.css?.removeSourceFile !== true; | ||||||
| 
 | 
 | ||||||
|   components.forEach((component) => { |   components.forEach((component) => { | ||||||
|     genEntry({ |     genEntry({ | ||||||
| @ -98,7 +101,7 @@ export function genComponentStyle( | |||||||
|       ext: '.css', |       ext: '.css', | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     if (CSS_LANG !== 'css') { |     if (CSS_LANG !== 'css' && hasSourceFile) { | ||||||
|       genEntry({ |       genEntry({ | ||||||
|         baseFile, |         baseFile, | ||||||
|         component, |         component, | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user