Skip to content

Commit

Permalink
Merge branch 'v2-dev' into feat/add-tauri-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
ErKeLost authored Dec 17, 2024
2 parents 689ed27 + d197a5b commit 85cda4e
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 63 deletions.
1 change: 1 addition & 0 deletions examples/refactor-react/.env.staging
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FARM_BASE_AOV=2000000
6 changes: 0 additions & 6 deletions examples/refactor-react/farm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ export default defineConfig({
// compilerPlugin(),
custom()
],
server: {
port: 4855,
appType: "mpa",
https: true
},
compilation: {
input: {
index: path.resolve(__dirname, "index.html"),
Expand All @@ -71,7 +66,6 @@ export default defineConfig({
server: {
port: 4855,
appType: "mpa",
https: true
},
});

Expand Down
3 changes: 3 additions & 0 deletions examples/refactor-react/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import FarmLogo from "./assets/logo.png";
// import { a } from './a.js'
// import { Button } from 'antd'
// import { HappyProvider } from '@ant-design/happy-work-theme';
console.log(import.meta.env);

export function Main() {
const [count, setCount] = useState(0);
// console.log(a);

return (
<>
<div>
Expand Down
67 changes: 22 additions & 45 deletions packages/core/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ import type {
UserConfig,
UserConfigExport,
UserConfigFnObject,
UserServerConfig,
commandType
} from './types.js';

Expand Down Expand Up @@ -107,8 +106,6 @@ export async function resolveConfig(
defaultNodeEnv: CompilationMode = 'development',
isPreview = false
): Promise<ResolvedUserConfig> {
const compileMode = defaultMode;

const mode = inlineOptions.mode || defaultMode;
const isNodeEnvSet = !!process.env.NODE_ENV;
inlineOptions.mode = mode;
Expand All @@ -134,7 +131,7 @@ export async function resolveConfig(
let userConfig: UserConfig = mergeFarmCliConfig(
inlineOptions,
{},
compileMode
defaultMode
);

const transformInlineConfig = userConfig;
Expand All @@ -146,7 +143,7 @@ export async function resolveConfig(

const { jsPlugins, vitePluginAdapters } = await resolvePlugins(
userConfig,
compileMode
defaultMode
);
const sortFarmJsPlugins = getSortedPlugins([
...jsPlugins,
Expand All @@ -155,16 +152,14 @@ export async function resolveConfig(

const config = await resolveConfigHook(userConfig, sortFarmJsPlugins);
// may be user push plugin when config hooks
const allPlugins = await resolvePlugins(config, compileMode);
const allPlugins = await resolvePlugins(config, defaultMode);
const farmJsPlugins = getSortedPlugins([
...allPlugins.jsPlugins,
...vitePluginAdapters,
externalAdapter()
]);

const resolvedUserConfig = await handleResolveConfig(
mode,
compileMode,
configFilePath,
loadedUserConfig,
config,
Expand All @@ -180,8 +175,6 @@ export async function resolveConfig(
}

async function handleResolveConfig(
mode: string,
compileMode: CompilationMode,
configFilePath: string,
loadedUserConfig:
| {
Expand All @@ -201,26 +194,17 @@ async function handleResolveConfig(
allowClearScreen: loadedUserConfig.config?.clearScreen
});

const resolvedUserConfig = await resolveUserConfig(
config,
configFilePath,
compileMode
);
const resolvedUserConfig = await resolveUserConfig(config, configFilePath);

resolvedUserConfig.logger = logger;

// farm handles server attributes in resolveConfig.
// On the one hand, farm can be used in node and server needs
// to be enabled. Lazy loading mode is enabled in node environment.
resolvedUserConfig.server = normalizeDevServerConfig(
resolvedUserConfig.server,
compileMode
);
resolvedUserConfig.server = normalizeDevServerConfig(resolvedUserConfig);

resolvedUserConfig.compilation = await normalizeUserCompilationConfig(
resolvedUserConfig,
mode as CompilationMode
);
resolvedUserConfig.compilation =
await normalizeUserCompilationConfig(resolvedUserConfig);

Object.assign(resolvedUserConfig, {
root: resolvedUserConfig.compilation.root,
Expand Down Expand Up @@ -648,13 +632,12 @@ function tryHttpsAsFileRead(value: unknown): string | Buffer | unknown {
}

export function normalizeDevServerConfig(
options: UserServerConfig | undefined,
mode: string
userConfig: UserConfig | undefined
): NormalizedServerConfig {
const { host, port, hmr: hmrConfig, https } = options || {};
const isProductionMode = mode === 'production';
const serverOptions = userConfig?.server;
const { host, port, hmr: hmrConfig, https } = serverOptions || {};
const hmr =
isProductionMode || hmrConfig === false
hmrConfig === false
? false
: merge(
{},
Expand All @@ -666,15 +649,15 @@ export function normalizeDevServerConfig(
hmrConfig === true ? {} : hmrConfig
);

return merge({}, DEFAULT_DEV_SERVER_OPTIONS, options, {
return merge({}, DEFAULT_DEV_SERVER_OPTIONS, serverOptions, {
hmr,
https: https
? {
...https,
ca: tryHttpsAsFileRead(options.https.ca),
cert: tryHttpsAsFileRead(options.https.cert),
key: tryHttpsAsFileRead(options.https.key),
pfx: tryHttpsAsFileRead(options.https.pfx)
ca: tryHttpsAsFileRead(serverOptions.https.ca),
cert: tryHttpsAsFileRead(serverOptions.https.cert),
key: tryHttpsAsFileRead(serverOptions.https.key),
pfx: tryHttpsAsFileRead(serverOptions.https.pfx)
}
: undefined
}) as NormalizedServerConfig;
Expand Down Expand Up @@ -963,8 +946,7 @@ export async function resolveDefaultUserConfig(options: any) {

const resolvedUserConfig: ResolvedUserConfig = await resolveUserConfig(
defaultConfig,
undefined,
defaultConfig.compilation.mode
undefined
);

const normalizedConfig = await normalizeUserCompilationConfig(
Expand All @@ -977,12 +959,11 @@ export async function resolveDefaultUserConfig(options: any) {

export async function resolveUserConfig(
userConfig: UserConfig,
configFilePath?: string | undefined,
mode: 'development' | 'production' | string = 'development'
configFilePath?: string | undefined
): Promise<ResolvedUserConfig> {
const resolvedUserConfig = {
...userConfig,
envMode: mode
envMode: userConfig.mode
} as ResolvedUserConfig;

// set internal config
Expand Down Expand Up @@ -1017,20 +998,16 @@ export async function resolveUserConfig(
NODE_ENV: userConfig.compilation.mode,
// TODO publicPath rewrite to BASE_URL
BASE_URL: userConfig.compilation.output.publicPath ?? '/',
mode,
DEV: mode === 'development',
PROD: mode === 'production'
mode: userConfig.mode,
DEV: userConfig.compilation.mode === 'development',
PROD: userConfig.compilation.mode === 'production'
};

resolvedUserConfig.publicDir = normalizePublicDir(
resolvedRootPath,
userConfig.publicDir
);

// TODO type error
// @ts-ignore
// resolveUserConfig.logger = logger;

return resolvedUserConfig;
}

Expand Down
16 changes: 9 additions & 7 deletions packages/core/src/config/mergeConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path, { isAbsolute } from 'node:path';
import { isString } from '../plugin/js/utils.js';
import { isArray, isObject } from '../utils/share.js';
import { CompilationMode } from './env.js';
import { FarmCliOptions, UserConfig } from './types.js';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -48,14 +49,15 @@ export function mergeConfig<T extends Record<string, any>>(
export function mergeFarmCliConfig(
cliOption: FarmCliOptions & UserConfig,
target: UserConfig,
mode?: 'development' | 'production'
mode: CompilationMode
): UserConfig {
let left: UserConfig = {};
const options = initialCliOptions(cliOption);

(
[
'clearScreen',
'mode',
'compilation',
'envDir',
'envPrefix',
Expand Down Expand Up @@ -115,18 +117,18 @@ export function mergeFarmCliConfig(
});
}

if (options.server?.port) {
if (options.mode) {
left = mergeConfig(left, {
server: {
port: options.port
compilation: {
mode: mode ?? (options.mode as UserConfig['compilation']['mode'])
}
});
}

if (options.mode) {
if (options.server?.port) {
left = mergeConfig(left, {
compilation: {
mode: mode ?? (options.mode as UserConfig['compilation']['mode'])
server: {
port: options.port
}
});
}
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export interface UserConfig {
/** current root of this project, default to current working directory */
root?: string;
clearScreen?: boolean;
mode?: string;
envDir?: string;
watch?: boolean | WatchOptions;
envPrefix?: string | string[];
Expand Down Expand Up @@ -218,7 +219,7 @@ export interface GlobalCliOptions {
c?: boolean | string;
config?: string;
m?: string;
mode?: 'development' | 'production';
mode?: string;
}

export interface FarmCLIServerOptions {
Expand Down
5 changes: 1 addition & 4 deletions packages/core/src/plugin/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ export async function handleVitePlugins(
if (vitePlugins.length) {
userConfig = merge({}, userConfig, {
compilation: userConfig.compilation,
server: normalizeDevServerConfig(
userConfig.server,
userConfig.compilation?.mode ?? mode
)
server: normalizeDevServerConfig(userConfig)
});
}
const flatVitePlugins = await resolveAsyncPlugins(vitePlugins);
Expand Down

0 comments on commit 85cda4e

Please sign in to comment.