mirror of https://github.com/alibaba/ice.git
				
				
				
			feat: enhance dropLogLevel build config (#6376)
* feat: 增强droplog功能 * fix: 修改warn说明 * fix: 补充md文件 --------- Co-authored-by: ClarkXia <xiawenwu41@gmail.com>
This commit is contained in:
		
							parent
							
								
									ae705c03e8
								
							
						
					
					
						commit
						f58e0c16d5
					
				|  | @ -185,7 +185,7 @@ const userConfig = [ | |||
|   }, | ||||
|   { | ||||
|     name: 'dropLogLevel', | ||||
|     validation: 'string', | ||||
|     validation: 'boolean|array|string', | ||||
|     setConfig: (config: Config, dropLogLevel: UserConfig['dropLogLevel']) => { | ||||
|       const levels = { | ||||
|         trace: 0, | ||||
|  | @ -195,17 +195,38 @@ const userConfig = [ | |||
|         warn: 3, | ||||
|         error: 4, | ||||
|       }; | ||||
|       if (typeof dropLogLevel === 'string') { | ||||
|         const level = levels[dropLogLevel]; | ||||
|         if (typeof level === 'number') { | ||||
|           return mergeDefaultValue(config, 'minimizerOptions', { | ||||
|             compress: { | ||||
|               pure_funcs: Object.keys(levels) | ||||
|                 .filter((methodName) => levels[methodName] <= level) | ||||
|               .map(methodName => `console.${methodName}`), | ||||
|                 .map((methodName) => `console.${methodName}`), | ||||
|             }, | ||||
|           }); | ||||
|         } else { | ||||
|         logger.warn(`dropLogLevel only support [${Object.keys(levels).join(',')}]`); | ||||
|           logger.warn( | ||||
|             `If you use a string as the attribute value of dropLogLevel you should enter one of the following strings [${Object.keys( | ||||
|               levels, | ||||
|             ).join(',')}]`,
 | ||||
|           ); | ||||
|         } | ||||
|       } else if (dropLogLevel === true) { | ||||
|         return mergeDefaultValue(config, 'minimizerOptions', { | ||||
|           compress: { | ||||
|             drop_console: true, | ||||
|           }, | ||||
|         }); | ||||
|       } else if (Array.isArray(dropLogLevel)) { | ||||
|         const pureFuncs = dropLogLevel.map((method) => `console.${method}`); | ||||
|         return mergeDefaultValue(config, 'minimizerOptions', { | ||||
|           compress: { | ||||
|             pure_funcs: pureFuncs, | ||||
|           }, | ||||
|         }); | ||||
|       } else if (dropLogLevel !== false) { | ||||
|         logger.warn('dropLogLevel support boolean, array and string'); | ||||
|       } | ||||
|     }, | ||||
|   }, | ||||
|  | @ -218,7 +239,9 @@ const userConfig = [ | |||
|       if (customValue === true) { | ||||
|         compileRegex = /node_modules\/*/; | ||||
|       } else if (customValue && customValue.length > 0) { | ||||
|         compileRegex = new RegExp(customValue.map((dep: string | RegExp) => { | ||||
|         compileRegex = new RegExp( | ||||
|           customValue | ||||
|             .map((dep: string | RegExp) => { | ||||
|               if (dep instanceof RegExp) { | ||||
|                 return dep.source; | ||||
|               } else if (typeof dep === 'string') { | ||||
|  | @ -227,7 +250,10 @@ const userConfig = [ | |||
|                 return matchStr; | ||||
|               } | ||||
|               return false; | ||||
|         }).filter(Boolean).join('|')); | ||||
|             }) | ||||
|             .filter(Boolean) | ||||
|             .join('|'), | ||||
|         ); | ||||
|       } | ||||
|       if (compileRegex) { | ||||
|         config.compileIncludes = [compileRegex]; | ||||
|  | @ -363,7 +389,9 @@ const userConfig = [ | |||
|     setConfig: (config: Config, codeSplitting: UserConfig['codeSplitting'], context: UserConfigContext) => { | ||||
|       const { originalUserConfig } = context; | ||||
|       if ('splitChunks' in originalUserConfig) { | ||||
|         logger.warn('splitChunks is deprecated, please use codeSplitting instead.https://ice.work/docs/guide/basic/config#codesplitting'); | ||||
|         logger.warn( | ||||
|           'splitChunks is deprecated, please use codeSplitting instead.https://ice.work/docs/guide/basic/config#codesplitting', | ||||
|         ); | ||||
|       } else { | ||||
|         // When codeSplitting is set to false / router, do not config splitChunks.
 | ||||
|         if (codeSplitting === false || codeSplitting === 'page') { | ||||
|  | @ -466,8 +494,4 @@ function defineConfig(config: UserConfig | (() => UserConfig)) { | |||
|   return config || defaultUserConfig; | ||||
| } | ||||
| 
 | ||||
| export { | ||||
|   defineConfig, | ||||
|   userConfig, | ||||
|   cliOption, | ||||
| }; | ||||
| export { defineConfig, userConfig, cliOption }; | ||||
|  |  | |||
|  | @ -30,6 +30,8 @@ interface IgnorePattern { | |||
| 
 | ||||
| type DistType = 'javascript' | 'html'; | ||||
| 
 | ||||
| type DropType = 'trace' | 'debug' | 'log' | 'info' | 'warn' | 'error'; | ||||
| 
 | ||||
| interface Fetcher { | ||||
|   packageName: string; | ||||
|   method?: string; | ||||
|  | @ -140,7 +142,7 @@ export interface UserConfig { | |||
|    * `console.*` will be dropped when build. | ||||
|    * @see https://v3.ice.work/docs/guide/basic/config#droploglevel
 | ||||
|    */ | ||||
|   dropLogLevel?: 'trace' | 'debug' | 'log' | 'info' | 'warn' | 'error'; | ||||
|   dropLogLevel?: DropType[] | DropType | boolean; | ||||
|   /** | ||||
|    * Minify build output, it only works in prod mode by default. | ||||
|    * @see https://v3.ice.work/docs/guide/basic/config#minify
 | ||||
|  | @ -248,9 +250,11 @@ export interface UserConfig { | |||
|    * generate additional assets for request data, default is true | ||||
|    * @see https://v3.ice.work/docs/guide/basic/config#dataloader
 | ||||
|    */ | ||||
|   dataLoader?: { | ||||
|   dataLoader?: | ||||
|     | { | ||||
|         fetcher?: Fetcher; | ||||
|   } | Boolean; | ||||
|       } | ||||
|     | Boolean; | ||||
|   /** | ||||
|    * Enable cross-origin loading of chunks. | ||||
|    * @see https://v3.ice.work/docs/guide/basic/config#crossoriginloading
 | ||||
|  |  | |||
|  | @ -240,11 +240,27 @@ export default defineConfig(() => ({ | |||
| 
 | ||||
| ### dropLogLevel | ||||
| 
 | ||||
| - 类型:`'trace' | 'debug' | 'log' | 'warn' | 'error'` | ||||
| - 默认值:`null`,不移除任何 console 代码 | ||||
| - 类型:`boolean | DropType[] | DropType` | ||||
| - 默认值:`false`,不移除任何 console 代码 | ||||
| 
 | ||||
| 压缩代码时移除 console.* 相关代码,比如配置了 log 则会移除 console.trace | ||||
| 、console.debug、console.log 代码。 | ||||
| 压缩代码时移除 console.* 相关代码,配置为true时,移除所有console.*相关代码。当想移除部分console代码,例如想要移除console.log和console.error时,可以配置为 | ||||
| ```js | ||||
| import { defineConfig } from '@ice/app'; | ||||
| 
 | ||||
| export default defineConfig(() => ({ | ||||
|    dropLog: ['error', 'log'], | ||||
| })); | ||||
| ``` | ||||
| 也可以根据console等级来进行移除 | ||||
| ```js | ||||
| // console 等级为 trace < debug < log < info < warn < error | ||||
| // 例如想要移除trace、debug、log时可以像下面这样配置 | ||||
| import { defineConfig } from '@ice/app'; | ||||
| 
 | ||||
| export default defineConfig(() => ({ | ||||
|    dropLog: 'log', | ||||
| })); | ||||
| ``` | ||||
| 
 | ||||
| ### compileDependencies | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue