mirror of https://github.com/alibaba/ice.git
				
				
				
			Feat: support options for compilationConfig (#6880)
* feat: support options for compilationConfig * fix: update plugin rax compat * chore: lint
This commit is contained in:
		
							parent
							
								
									e858a52280
								
							
						
					
					
						commit
						e4a32686c6
					
				|  | @ -0,0 +1,5 @@ | |||
| --- | ||||
| '@ice/plugin-rax-compat': patch | ||||
| --- | ||||
| 
 | ||||
| fix: support pass options for `compilationConfig` | ||||
|  | @ -0,0 +1,5 @@ | |||
| --- | ||||
| '@ice/shared-config': patch | ||||
| --- | ||||
| 
 | ||||
| feat: support options for compilationConfig | ||||
|  | @ -16,7 +16,7 @@ export class JSXService { | |||
| 
 | ||||
|       // Reset jsc.transform.react.runtime to classic.
 | ||||
|       config.swcOptions = merge(config.swcOptions || {}, { | ||||
|         compilationConfig: (source: string, id: string) => { | ||||
|         compilationConfig: (source: string, id: string, compileOptions) => { | ||||
|           let swcCompilationConfig = {}; | ||||
|           const hasJSXComment = source.indexOf('@jsx createElement') !== -1; | ||||
|           const isRaxComponent = /(from|require\()\s*['"]rax['"]/.test(source); | ||||
|  | @ -43,7 +43,7 @@ export class JSXService { | |||
|             }; | ||||
|           } | ||||
| 
 | ||||
|           return merge({}, originalSwcCompilationConfigFunc(source, id), swcCompilationConfig); | ||||
|           return merge({}, originalSwcCompilationConfigFunc(source, id, compileOptions), swcCompilationConfig); | ||||
|         }, | ||||
|       }); | ||||
|     }); | ||||
|  |  | |||
|  | @ -40,9 +40,21 @@ interface ConfigurationCtx<T = typeof webpack> extends Config { | |||
| } | ||||
| 
 | ||||
| type Experimental = Configuration['experiments']; | ||||
| 
 | ||||
| export type JSXSuffix = 'jsx' | 'tsx'; | ||||
| export interface GetJsxTransformOptions { | ||||
|   rootDir: string; | ||||
|   mode: Options['mode']; | ||||
|   suffix?: JSXSuffix; | ||||
|   fastRefresh: boolean; | ||||
|   polyfill: Config['polyfill']; | ||||
|   enableEnv: boolean; | ||||
| } | ||||
| 
 | ||||
| interface SwcOptions { | ||||
|   removeExportExprs?: string[]; | ||||
|   compilationConfig?: SwcCompilationConfig | ((source: string, id: string) => SwcCompilationConfig); | ||||
|   compilationConfig?: SwcCompilationConfig | | ||||
|     ((source: string, id: string, options: GetJsxTransformOptions) => SwcCompilationConfig); | ||||
|   keepExports?: string[] | { value: string[]; include?: (id: string) => boolean }; | ||||
|   nodeTransform?: boolean; | ||||
| } | ||||
|  |  | |||
|  | @ -5,13 +5,11 @@ import consola from 'consola'; | |||
| import type { SwcConfig, ReactConfig } from '@ice/bundles'; | ||||
| import type { UnpluginOptions } from '@ice/bundles/compiled/unplugin/index.js'; | ||||
| import lodash from '@ice/bundles/compiled/lodash/index.js'; | ||||
| import type { Config } from '../types.js'; | ||||
| import type { Config, JSXSuffix, GetJsxTransformOptions } from '../types.js'; | ||||
| import transformImport from '../utils/transformImport.js'; | ||||
| 
 | ||||
| const { merge } = lodash; | ||||
| 
 | ||||
| type JSXSuffix = 'jsx' | 'tsx'; | ||||
| 
 | ||||
| interface Options { | ||||
|   rootDir: string; | ||||
|   mode: 'development' | 'production' | 'none'; | ||||
|  | @ -84,8 +82,8 @@ const compilationPlugin = (options: Options): UnpluginOptions => { | |||
|         filename: id, | ||||
|         sourceMaps: !!sourceMap, | ||||
|       }; | ||||
| 
 | ||||
|       const commonOptions = getJsxTransformOptions({ rootDir, mode, suffix, fastRefresh, polyfill, enableEnv }); | ||||
|       const compileOptions = { rootDir, mode, suffix, fastRefresh, polyfill, enableEnv }; | ||||
|       const commonOptions = getJsxTransformOptions(compileOptions); | ||||
| 
 | ||||
|       // auto detect development mode
 | ||||
|       if ( | ||||
|  | @ -99,7 +97,7 @@ const compilationPlugin = (options: Options): UnpluginOptions => { | |||
|       merge(programmaticOptions, commonOptions); | ||||
| 
 | ||||
|       if (typeof compilationConfig === 'function') { | ||||
|         merge(programmaticOptions, compilationConfig(source, fileId)); | ||||
|         merge(programmaticOptions, compilationConfig(source, fileId, compileOptions)); | ||||
|       } else if (compilationConfig) { | ||||
|         merge(programmaticOptions, compilationConfig); | ||||
|       } | ||||
|  | @ -180,14 +178,6 @@ const compilationPlugin = (options: Options): UnpluginOptions => { | |||
|   }; | ||||
| }; | ||||
| 
 | ||||
| interface GetJsxTransformOptions { | ||||
|   rootDir: string; | ||||
|   mode: Options['mode']; | ||||
|   suffix?: JSXSuffix; | ||||
|   fastRefresh: boolean; | ||||
|   polyfill: Config['polyfill']; | ||||
|   enableEnv: boolean; | ||||
| } | ||||
| 
 | ||||
| export function getJsxTransformOptions({ | ||||
|   suffix, | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue