Skip to content

Commit

Permalink
chore: resolve server options
Browse files Browse the repository at this point in the history
  • Loading branch information
ErKeLost committed Aug 14, 2024
1 parent 902c5c8 commit 56c02ce
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 62 deletions.
17 changes: 6 additions & 11 deletions examples/refactor-react/farm.config.ts
Original file line number Diff line number Diff line change
@@ -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,
},
});
20 changes: 15 additions & 5 deletions examples/vite-adapter-vue/svg-component.d.ts
Original file line number Diff line number Diff line change
@@ -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<import("vue").ExtractPropTypes<{
name: {
type: import("vue").PropType<"icon-vue">;
default: string;
required: true;
};
}>>, {
name: "icon-vue";
}>;
export const svgNames: ["icon-vue"];
export type SvgName = "icon-vue";
export default MySvgIcon;
Expand Down
79 changes: 52 additions & 27 deletions packages/core/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -98,7 +115,7 @@ export async function resolveConfig(
defaultMode: CompilationMode = 'development',
defaultNodeEnv: CompilationMode = 'development',
isPreview = false,
logger?: Logger
logger = new Logger()
): Promise<ResolvedUserConfig> {
logger = logger ?? new Logger();
// TODO mode 这块还是不对 要区分 mode 和 build 还是 dev 环境
Expand Down Expand Up @@ -130,28 +147,24 @@ 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,
...vitePluginAdapters,
externalAdapter()
]);

const config = await resolveConfigHook(userConfig, sortFarmJsPlugins);
const config = await resolveConfigHook(rawConfig, sortFarmJsPlugins);

const resolvedUserConfig = await resolveUserConfig(
config,
Expand All @@ -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) {
Expand All @@ -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);
}

/**
Expand Down
38 changes: 19 additions & 19 deletions packages/core/src/config/mergeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -127,15 +130,15 @@ export function mergeFarmCliConfig(
});
}

if (options.https) {
if (options.server.https) {
left = mergeConfig(left, {
server: {
https: options.https
}
});
}

if (options.sourcemap) {
if (options.compilation.sourcemap) {
left = mergeConfig(left, {
compilation: { sourcemap: options.sourcemap }
});
Expand All @@ -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 }),
Expand All @@ -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 }),
Expand All @@ -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 })
};
Expand Down

0 comments on commit 56c02ce

Please sign in to comment.