diff --git a/examples/refactor-react/farm.config.ts b/examples/refactor-react/farm.config.ts index b3c87f351..15606de34 100644 --- a/examples/refactor-react/farm.config.ts +++ b/examples/refactor-react/farm.config.ts @@ -1,14 +1,9 @@ -import { defineConfig } from '@farmfe/core'; +import { defineConfig } from "@farmfe/core"; +import react from "@farmfe/plugin-react"; export default defineConfig({ - plugins: ['@farmfe/plugin-react'], - compilation: { - // presetEnv: false, - // progress: false, - // sourcemap: false, - persistentCache: false, - runtime: { - isolate: true - } - } + plugins: [react()], + server: { + // port: 3005, + }, }); diff --git a/examples/vite-adapter-vue/svg-component.d.ts b/examples/vite-adapter-vue/svg-component.d.ts index a7a37656d..d58a707ca 100644 --- a/examples/vite-adapter-vue/svg-component.d.ts +++ b/examples/vite-adapter-vue/svg-component.d.ts @@ -1,10 +1,20 @@ declare module '~virtual/svg-component' { - const MySvgIcon: (props: { - name: "icon-vue", - className?:string - style?: React.CSSProperties - })=> JSX.Element; + const MySvgIcon: import("vue").DefineComponent<{ + name: { + type: import("vue").PropType<"icon-vue">; + default: string; + required: true; + }; + }, {}, unknown, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, Readonly; + default: string; + required: true; + }; + }>>, { + name: "icon-vue"; + }>; export const svgNames: ["icon-vue"]; export type SvgName = "icon-vue"; export default MySvgIcon; diff --git a/packages/core/src/config/index.ts b/packages/core/src/config/index.ts index dc03ed667..52a824c3d 100644 --- a/packages/core/src/config/index.ts +++ b/packages/core/src/config/index.ts @@ -88,6 +88,23 @@ export function defineFarmConfig(config: UserConfigExport): UserConfigExport { return config; } +// may be use object type +type ResolveConfigOptions = { + inlineOptions: FarmCliOptions & UserConfig; + command: keyof typeof COMMANDS; + defaultMode?: CompilationMode; + defaultNodeEnv?: CompilationMode; + isPreview?: boolean; + logger?: Logger; +}; + +const COMMANDS = { + START: 'start', + BUILD: 'build', + WATCH: 'watch', + PREVIEW: 'preview' +} as const; + /** * Resolve and load user config from the specified path * @param configPath @@ -98,7 +115,7 @@ export async function resolveConfig( defaultMode: CompilationMode = 'development', defaultNodeEnv: CompilationMode = 'development', isPreview = false, - logger?: Logger + logger = new Logger() ): Promise { logger = logger ?? new Logger(); // TODO mode 这块还是不对 要区分 mode 和 build 还是 dev 环境 @@ -130,20 +147,16 @@ export async function resolveConfig( {}, compileMode ); - let configPath = initialConfigPath; + + let configFilePath = initialConfigPath; if (loadedUserConfig) { - configPath = loadedUserConfig.configFilePath; + configFilePath = loadedUserConfig.configFilePath; rawConfig = mergeConfig(rawConfig, loadedUserConfig.config); } - const { config: userConfig, configFilePath } = { - configFilePath: configPath, - config: rawConfig - }; - const { jsPlugins, vitePlugins, rustPlugins, vitePluginAdapters } = - await resolvePlugins(userConfig, compileMode, logger); + await resolvePlugins(rawConfig, compileMode, logger); const sortFarmJsPlugins = getSortedPlugins([ ...jsPlugins, @@ -151,7 +164,7 @@ export async function resolveConfig( externalAdapter() ]); - const config = await resolveConfigHook(userConfig, sortFarmJsPlugins); + const config = await resolveConfigHook(rawConfig, sortFarmJsPlugins); const resolvedUserConfig = await resolveUserConfig( config, @@ -175,9 +188,11 @@ export async function resolveConfig( mode as CompilationMode ); - resolvedUserConfig.root = resolvedUserConfig.compilation.root; - resolvedUserConfig.jsPlugins = sortFarmJsPlugins; - resolvedUserConfig.rustPlugins = rustPlugins; + Object.assign(resolvedUserConfig, { + root: resolvedUserConfig.compilation.root, + jsPlugins: sortFarmJsPlugins, + rustPlugins: rustPlugins + }); // Temporarily dealing with alias objects and arrays in js will be unified in rust in the future.] if (vitePlugins.length) { @@ -195,25 +210,35 @@ export async function resolveConfig( ); } - switch (configEnv.command) { - case 'start': + await handleLazyCompilation( + resolvedUserConfig, + command as keyof typeof COMMANDS + ); + + return resolvedUserConfig; +} + +async function handleLazyCompilation( + config: ResolvedUserConfig, + command: keyof typeof COMMANDS +) { + const commandHandlers = { + [COMMANDS.START]: async (cfg: any) => { if ( - resolvedUserConfig.compilation.lazyCompilation && - typeof resolvedUserConfig.server?.host === 'string' + cfg.compilation.lazyCompilation && + typeof cfg.server?.host === 'string' ) { - await setLazyCompilationDefine(resolvedUserConfig); + await setLazyCompilationDefine(cfg); } - break; - case 'watch': - if (resolvedUserConfig.compilation?.lazyCompilation) { - await setLazyCompilationDefine(resolvedUserConfig); + }, + [COMMANDS.WATCH]: async (cfg: any) => { + if (cfg.compilation?.lazyCompilation) { + await setLazyCompilationDefine(cfg); } - break; - default: - break; - } + } + }; - return resolvedUserConfig; + await commandHandlers[command]?.(config); } /** diff --git a/packages/core/src/config/mergeConfig.ts b/packages/core/src/config/mergeConfig.ts index be05e78f5..119c8f9a0 100644 --- a/packages/core/src/config/mergeConfig.ts +++ b/packages/core/src/config/mergeConfig.ts @@ -92,26 +92,29 @@ export function mergeFarmCliConfig( } if (target.root && !isAbsolute(target.root)) { - const resolvedRoot = path.resolve(cliOption.configPath, target.root); + const resolvedRoot = path.resolve(cliOption.configFile, target.root); target.root = resolvedRoot; } } - if (isString(options.host) || typeof options.host === 'boolean') { + if ( + isString(options.server.host) || + typeof options.server.host === 'boolean' + ) { left = mergeConfig(left, { server: { host: options.host } }); } - if (typeof options.minify === 'boolean') { + if (typeof options.compilation.minify === 'boolean') { left = mergeConfig(left, { compilation: { minify: options.minify } }); } - if (options.outDir) { + if (options.compilation.output.outDir) { left = mergeConfig(left, { compilation: { output: { path: options.outDir } } }); } - if (options.port) { + if (options.server.port) { left = mergeConfig(left, { server: { port: options.port @@ -127,7 +130,7 @@ export function mergeFarmCliConfig( }); } - if (options.https) { + if (options.server.https) { left = mergeConfig(left, { server: { https: options.https @@ -135,7 +138,7 @@ export function mergeFarmCliConfig( }); } - if (options.sourcemap) { + if (options.compilation.sourcemap) { left = mergeConfig(left, { compilation: { sourcemap: options.sourcemap } }); @@ -145,17 +148,13 @@ export function mergeFarmCliConfig( } export function initialCliOptions(options: any): any { - const { - input, - outDir, - target, - format, - watch, - minify, - sourcemap, - treeShaking, - mode - } = options; + const { mode, watch } = options; + const { outDir, target, format, minify, sourcemap, treeShaking } = + options.compilation; + + const input = Object.values(options.compilation.input).filter(Boolean).length + ? options.compilation.input + : {}; const output: UserConfig['compilation']['output'] = { ...(outDir && { path: outDir }), @@ -164,7 +163,7 @@ export function initialCliOptions(options: any): any { }; const compilation: UserConfig['compilation'] = { - input: { ...(input && { index: input }) }, + input: Object.values(input).filter(Boolean).length ? input : {}, output, ...(watch && { watch }), ...(minify && { minify }), @@ -175,6 +174,7 @@ export function initialCliOptions(options: any): any { const defaultOptions: any = { compilation, root: options.root, + server: options.server, configFile: options.configFile, ...(mode && { mode }) };