Skip to content

Commit

Permalink
feat: merge configuration policies (#1264)
Browse files Browse the repository at this point in the history
* feat: merge configuration policies

* feat: merge more fields from params

* chore: update types

* chore: remove unless code

* chore: add api example

* chore: update config resolve

* chore: remove example

---------

Co-authored-by: ADNY <66500121+ErKeLost@users.noreply.github.com>
Co-authored-by: erkelost <1256029807@qq.com>
  • Loading branch information
3 people committed Apr 30, 2024
1 parent 0ecfeef commit 9754d37
Show file tree
Hide file tree
Showing 34 changed files with 345 additions and 1,035 deletions.
6 changes: 6 additions & 0 deletions .changeset/famous-llamas-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@farmfe/core': patch
'@farmfe/cli': patch
---

merge configuration policies
40 changes: 40 additions & 0 deletions packages/cli/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { FarmCLIOptions, UserConfig } from '@farmfe/core';
import { FarmCLIBuildOptions, GlobalFarmCLIOptions } from './types.js';

export function getOptionFromBuildOption(
options: FarmCLIBuildOptions & GlobalFarmCLIOptions
): FarmCLIOptions & UserConfig {
const {
input,
outDir,
target,
format,
watch,
minify,
sourcemap,
treeShaking,
mode
} = options;

const output: UserConfig['compilation']['output'] = {
...(outDir && { path: outDir }),
...(target && { targetEnv: target }),
...(format && { format })
};

const compilation: UserConfig['compilation'] = {
input: { ...(input && { index: input }) },
output,
...(watch && { watch }),
...(minify && { minify }),
...(sourcemap && { sourcemap }),
...(treeShaking && { treeShaking })
};

const defaultOptions: FarmCLIOptions & UserConfig = {
compilation,
...(mode && { mode })
};

return defaultOptions;
}
57 changes: 5 additions & 52 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ import { readFileSync } from 'node:fs';
import { cac } from 'cac';
import {
resolveCore,
getConfigPath,
resolveCommandOptions,
handleAsyncOperationErrors,
preventExperimentalWarning,
resolveRootPath,
resolveCliConfig
} from './utils.js';
import { COMMANDS } from './plugin/index.js';
import { getOptionFromBuildOption } from './config.js';

import type {
FarmCLIBuildOptions,
Expand Down Expand Up @@ -97,22 +95,8 @@ cli

const defaultOptions = {
root,
compilation: {
watch: options.watch,
output: {
path: options?.outDir,
targetEnv: options?.target,
format: options?.format
},
input: {
index: options?.input
},
sourcemap: options.sourcemap,
minify: options.minify,
treeShaking: options.treeShaking
},
mode: options.mode,
configPath
configPath,
...getOptionFromBuildOption(options)
};

const { build } = await resolveCore();
Expand All @@ -138,21 +122,8 @@ cli

const defaultOptions = {
root,
compilation: {
output: {
path: options?.outDir,
targetEnv: options?.target,
format: options?.format
},
input: {
index: options?.input
},
sourcemap: options.sourcemap,
minify: options.minify,
treeShaking: options.treeShaking
},
mode: options.mode,
configPath
configPath,
...getOptionFromBuildOption(options)
};

const { watch } = await resolveCore();
Expand Down Expand Up @@ -211,24 +182,6 @@ cli
}
});

// create plugins command
cli
.command('plugin [command]', 'Commands for manage plugins', {
allowUnknownOptions: true
})
.action(async (command: keyof typeof COMMANDS, args: unknown) => {
try {
COMMANDS[command](args);
} catch (e) {
const { Logger } = await import('@farmfe/core');
const logger = new Logger();
logger.error(
`The command arg parameter is incorrect. If you want to create a plugin in farm. such as "farm plugin create"\n${e.stack}`
);
process.exit(1);
}
});

// Listening for unknown command
cli.on('command:*', async () => {
const { Logger } = await import('@farmfe/core');
Expand Down
92 changes: 0 additions & 92 deletions packages/cli/src/plugin/build.ts

This file was deleted.

124 changes: 0 additions & 124 deletions packages/cli/src/plugin/create.ts

This file was deleted.

31 changes: 0 additions & 31 deletions packages/cli/src/plugin/index.ts

This file was deleted.

0 comments on commit 9754d37

Please sign in to comment.