Skip to content

Commit 7453cfc

Browse files
authored
Merge pull request #36 from farm-fe/fix/customInputCannotFindOutputEntry
fix: custom input & upgrade @farmfe/core
2 parents 22aa214 + 4bf2b20 commit 7453cfc

File tree

10 files changed

+247
-53
lines changed

10 files changed

+247
-53
lines changed

.changeset/silver-jobs-fix.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'farmup': patch
3+
---
4+
5+
fix custom input & upgrade @farm/core

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
"biome.enabled": true,
33
"[javascript]": {
44
"editor.defaultFormatter": "biomejs.biome"
5+
},
6+
"[typescript]": {
7+
"editor.defaultFormatter": "biomejs.biome"
58
}
69
}

biome.json

+6
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,11 @@
2424
"formatter": {
2525
"quoteStyle": "single"
2626
}
27+
},
28+
"files": {
29+
"include": [
30+
"*.ts",
31+
"*.json"
32+
]
2733
}
2834
}

packages/core/farm.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default defineConfig({
1111
targetEnv: 'node',
1212
format: 'esm',
1313
},
14+
lazyCompilation: false,
1415
persistentCache: false,
1516
external: ['^@farmfe/core$'],
1617
minify: false,

packages/core/package.json

+13-6
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,21 @@
1313
"build": "farm build",
1414
"preview": "farm preview",
1515
"clean": "farm clean",
16-
"farmup": "node ./bin/farmup.js --no-exec -w"
16+
"farmup": "node ./bin/farmup.js --no-exec -w",
17+
"prepublish": "mv ../../README.md ./"
1718
},
1819
"bin": "./bin/farmup.js",
1920
"devDependencies": {
20-
"@farmfe/cli": "^1.0.2",
21+
"@farmfe/cli": "^1.0.3",
2122
"@types/fs-extra": "^11.0.4",
2223
"@types/lodash-es": "^4.17.12",
2324
"@types/node": "^20.12.7",
24-
"typescript": "^5.4.5",
2525
"cac": "^6.7.14",
2626
"execa": "^8.0.1",
2727
"fs-extra": "^11.2.0",
2828
"glob": "^10.3.15",
29-
"lodash-es": "^4.17.21"
29+
"lodash-es": "^4.17.21",
30+
"typescript": "^5.4.5"
3031
},
3132
"exports": {
3233
".": {
@@ -39,6 +40,12 @@
3940
"types": "./dist/plugin.d.ts"
4041
}
4142
},
43+
"files": [
44+
"dist",
45+
"README.md",
46+
"package.json",
47+
"bin"
48+
],
4249
"keywords": [
4350
"farm",
4451
"cli",
@@ -56,6 +63,6 @@
5663
"email": "[email protected]"
5764
},
5865
"dependencies": {
59-
"@farmfe/core": "^1.3.6"
66+
"@farmfe/core": "^1.3.12"
6067
}
61-
}
68+
}

packages/core/src/config/normalize/find-entry.ts

+32-15
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@ import path from 'node:path';
44
import { ExecuteMode, type CommonOptions, type Format, type ResolvedCommonOptions } from '../../types/options';
55
import { isExists } from '../../util/file';
66

7-
const findEntryKeyFromObject = (input: Record<string, string>) => {
8-
return Object.keys(input)[0];
7+
const findEntryKeyFromObject = (input: Record<string, string | undefined | null>) => {
8+
let entry = null;
9+
for (const key in input) {
10+
if (input[key]) {
11+
entry = key;
12+
break;
13+
}
14+
}
15+
return entry;
916
};
1017

1118
const maybeEntryPrefix = ['src'];
@@ -97,14 +104,24 @@ function normalizeCommonEntry(entries: CommonOptions['entry']): ResolvedCommonOp
97104
export async function tryFindEntryFromUserConfig(logger: Logger, config: UserConfig, options: CommonOptions) {
98105
const entriesFromOption = normalizeCommonEntry(options.entry);
99106

107+
const defaultCompilationInputKeys = Object.keys(config.compilation?.input ?? {}).reduce(
108+
(res, key) => Object.assign(res, { [key]: null }),
109+
{} as Record<string, null | undefined | string>,
110+
);
111+
112+
const clearDefault = (obj: Record<string, null | string | undefined>) => ({
113+
...defaultCompilationInputKeys,
114+
...obj,
115+
});
116+
100117
// cli option > config
101118
if (entriesFromOption) {
102-
return entriesFromOption;
119+
return clearDefault(entriesFromOption);
103120
}
104121

105122
let findEntryKey = findEntryKeyFromObject(config.compilation?.input ?? {});
106123

107-
if (findEntryKey) return config.compilation?.input!;
124+
if (findEntryKey) return clearDefault({ [findEntryKey]: config.compilation?.input?.[findEntryKey] });
108125

109126
let findEntry: string | null = null;
110127

@@ -116,9 +133,7 @@ export async function tryFindEntryFromUserConfig(logger: Logger, config: UserCon
116133
} else {
117134
logger.info(`automatic find and use this entry: "${findEntry}"`);
118135
}
119-
return {
120-
[findEntryKey]: findEntry,
121-
};
136+
return clearDefault({ [findEntryKey]: findEntry });
122137
}
123138

124139
const packageModuleValueMapFormat: Record<string, Format> = {
@@ -151,19 +166,21 @@ export function pinOutputEntryFilename(options: ResolvedCommonOptions) {
151166

152167
const executeMode = options.execute.type;
153168

154-
155-
if(options.target?.startsWith('browser')) {
169+
if (options.target?.startsWith('browser')) {
156170
return;
157171
}
158172

159-
if ((executeMode === ExecuteMode.Custom || executeMode === ExecuteMode.Node) && !options.noExecute) {
160-
options.entry = Object.entries(options.entry).reduce((res, [key, val]) => {
161-
res[`${key}.${formatMapExt[options.format ?? 'cjs']}`] = val;
162-
return res;
163-
}, {} as Record<string, string>);
173+
if (executeMode === ExecuteMode.Node && !options.noExecute) {
174+
options.entry = Object.entries(options.entry).reduce(
175+
(res, [key, val]) => {
176+
if (val) res[`${key}.${formatMapExt[options.format ?? 'cjs']}`] = val;
177+
return res;
178+
},
179+
{} as Record<string, string>,
180+
);
164181

165182
options.outputEntry = {
166-
name: '[entryName]'
183+
name: '[entryName]',
167184
};
168185
}
169186
}

packages/core/src/config/normalize/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function normalizedExecuted(commonOption: CommonOptions, options: ResolvedCommon
5555
}
5656
} else {
5757
// from entry file ext
58-
const entryFiles = Object.values(options.entry);
58+
const entryFiles = Object.values(options.entry).filter(Boolean) as string[];
5959
let res: ExecuteMode | undefined;
6060
for (const item of entryFiles) {
6161
const targetFromExt = extensionMapExecutedMode[path.extname(item).slice(1)];
@@ -128,7 +128,8 @@ export function normalizedTargetEnv(
128128
} else {
129129
let targetFromInput: TargetEnv | undefined;
130130

131-
for (const entryFile of Object.values(options.entry)) {
131+
const entryFiles = Object.values(options.entry).filter(Boolean) as string[];
132+
for (const entryFile of entryFiles) {
132133
const ext = path.extname(entryFile).slice(1);
133134
if (extMapTargetEnv[ext]) {
134135
targetFromInput = extMapTargetEnv[ext];

packages/core/src/plugins/auto-execute.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export default function autoExecute(options: CommonOptions = {}, logger = defaul
5757

5858
return {
5959
name: `${name}:execute`,
60-
priority: Number.NEGATIVE_INFINITY,
60+
priority: Number.MIN_SAFE_INTEGER,
6161
async config(config) {
6262
return await normalizeOption.config(config);
6363
},

packages/core/src/types/options.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export interface CommonOptions {
7272
}
7373

7474
export interface ResolvedCommonOptions {
75-
entry: Record<string, string>;
75+
entry: Record<string, string | null | undefined>;
7676
args: string[];
7777
execute: ExecuteOption;
7878
external: [];

0 commit comments

Comments
 (0)