Skip to content

Commit

Permalink
feat: upgrade dependence & option sort & add output option (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
shulandmimi committed May 29, 2024
1 parent fe54ea3 commit 8e76286
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 65 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-keys-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'farmup': patch
---

upgrade dependence & option sort & add output option
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ feature

- cross import CommonJs and EsModule
- watch mode
- support html

## Setup

Expand Down Expand Up @@ -54,9 +55,7 @@ farmup build index.ts --no-exec
- more cli options
- sourcemap
- ignore some watch file
- command output
- start to tmp file
- build to local file
- execute without output file

## options

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
"dependencies": {
"@changesets/cli": "^2.27.3",
"@farmfe/core": "^1.1.9",
"@farmfe/core": "^1.1.12",
"cac": "^6.7.14",
"fs-extra": "^11.2.0",
"glob": "^10.3.15",
Expand Down
82 changes: 41 additions & 41 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions src/config/normalize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export function normalizedTargetEnv(
config: UserConfig,
commonOptions: CommonOptions,
options: ResolvedCommonOptions,
logger: Logger,
logger: Logger
) {
config.compilation?.output?.targetEnv;
if (commonOptions.target) {
Expand Down Expand Up @@ -164,7 +164,7 @@ async function normalizedSimpleConfig(
config: UserConfig,
commonOptions: CommonOptions,
options: ResolvedCommonOptions,
logger: Logger,
logger: Logger
) {
const inputs = await tryFindEntryFromUserConfig(logger, config, commonOptions);

Expand All @@ -186,6 +186,7 @@ async function normalizedSimpleConfig(
? { target: commonOptions.target || config.compilation?.output?.targetEnv }
: {}),
...(commonOptions.autoExternal ? { autoExternal: !!commonOptions.autoExternal } : {}),
outputDir: commonOptions.outputDir ?? config.compilation.output?.path ?? './dist',
noExecute: commonOptions.noExecute ?? false,
noWatch: commonOptions.noWatch ?? true,
watchFiles: await normalizeWatchFiles(commonOptions),
Expand Down Expand Up @@ -231,12 +232,10 @@ export class NormalizeOption {
autoExternal: false,
noExecute: false,
watchFiles: [],
outputDir: './dist',
};

constructor(
private commonOption: CommonOptions,
private logger: Logger,
) {}
constructor(private commonOption: CommonOptions, private logger: Logger) {}

async config(config: UserConfig): Promise<UserConfig> {
await normalizedSimpleConfig(config, this.commonOption, this.options, this.logger);
Expand All @@ -248,11 +247,12 @@ export class NormalizeOption {
...pick(this.options, ['format', 'mode']),
...(this.options.target ? { targetEnv: this.options.target } : {}),
...(this.options.outputEntry ? { entryFilename: this.options.outputEntry.name } : {}),
path: this.options.outputDir,
},
...pick(this.options, 'minify'),
},
},
this.options,
this.options
);
}

Expand Down
22 changes: 13 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ExecuteMode, type CommonOptions } from './types/options';
import autoExternal from './plugins/auto-external';
import path from 'node:path';
import { isBoolean, isString } from 'lodash-es';
import { logger } from './config/constant';

const { version } = JSON.parse(readFileSync(new URL('../package.json', import.meta.url)).toString());

Expand All @@ -38,6 +39,7 @@ function createInlineConfig(options: CommonOptions): InlineConfig {
root: options.root,
configPath: options.config,
plugins: buildPluginsByCommonOption(options),
logger: logger,
};
}

Expand Down Expand Up @@ -75,19 +77,20 @@ async function build(options: CommonOptions) {

const cli = cac('farmup');

cli.option(
'--target [target]',
"target for output, default is node, support 'browser'、'node'、'node16'、'node-legacy'、'node-next'、'browser-legacy'、'browser-es2015'、'browser-es2017'、'browser-esnext'",
)
cli.option('-w, --watch [...files]', 'watch files', { default: false })
.option('-e, --exec [file]', 'custom execute command')
.option('-o, --output [dir]', 'output directory, default "./dist" if not set in config')
.option('--mode [mode]', 'mode for build, default is development, choose one from "development" or "production"')
.option('--minify', 'minify for output')
.option('--config [config]', 'config path, if not path, it will be auto find')
.option('-c, --config [config]', 'config path, if not path, it will be auto find')
.option('--no-config', 'if farm.config.[ext] exists, it will be ignore')
.option('--format [format]', 'choose one from "cjs" or "esm"')
.option('--external [...external]', 'external')
.option('-w, --watch [...files]', 'watch files', { default: false })
.option('--no-auto-external', 'if not found module, auto as external', { default: true })
.option('-e, --exec [file]', 'custom execute command');
.option(
'--target [target]',
"target for output, default is node, support 'browser'、'node'、'node16'、'node-legacy'、'node-next'、'browser-legacy'、'browser-es2015'、'browser-es2017'、'browser-esnext'"
);

// biome-ignore lint/suspicious/noExplicitAny: <explanation>
async function commonOptionsFromArgs(args: Record<string, any>): Promise<Partial<CommonOptions>> {
Expand All @@ -98,8 +101,8 @@ async function commonOptionsFromArgs(args: Record<string, any>): Promise<Partial
? args.config
: path.resolve(root, args.config)
: args.config
? await getConfigFilePath(root)
: undefined;
? await getConfigFilePath(root)
: undefined;
const execute = isString(args.exec) && !isBoolean(args.exec) ? args.exec : undefined;

return {
Expand All @@ -118,6 +121,7 @@ async function commonOptionsFromArgs(args: Record<string, any>): Promise<Partial
.flat()
.map((item) => (item === true ? undefined : item))
.filter(Boolean),
outputDir: args.output || './dist',
};
}

Expand Down
19 changes: 15 additions & 4 deletions src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export type TargetEnv = Exclude<Required<Required<UserConfig>['compilation']>['o
type Get<T extends Record<keyof any, any>, K extends keyof any> = K extends `${infer PREFIX}.${infer LAST}`
? Get<Exclude<T[PREFIX], undefined>, LAST>
: K extends keyof T
? T[K]
: never;
? T[K]
: never;

export type Format = Get<UserConfig, 'compilation.output.format'>;

Expand Down Expand Up @@ -54,9 +54,15 @@ export interface CommonOptions {

root?: string;

/** watch files, support glob pattern */
watchFiles?: string[];

name?: string,
/** name for plugin or logger prefix */
name?: string;
/**
* output directory for build
* @default './dist'
*/
outputDir?: string;
}

export interface ResolvedCommonOptions {
Expand All @@ -81,6 +87,11 @@ export interface ResolvedCommonOptions {
matchEntryName: (name: string, inputs: Record<string, string>) => string | undefined;
name: string;
};

/**
* @default './dist'
*/
outputDir: string;
}

export enum ExecuteMode {
Expand Down

0 comments on commit 8e76286

Please sign in to comment.