Skip to content

Commit

Permalink
Merge pull request #36 from farm-fe/fix/customInputCannotFindOutputEntry
Browse files Browse the repository at this point in the history
fix: custom input & upgrade @farmfe/core
  • Loading branch information
ErKeLost authored Aug 9, 2024
2 parents 22aa214 + 4bf2b20 commit 7453cfc
Show file tree
Hide file tree
Showing 10 changed files with 247 additions and 53 deletions.
5 changes: 5 additions & 0 deletions .changeset/silver-jobs-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'farmup': patch
---

fix custom input & upgrade @farm/core
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"biome.enabled": true,
"[javascript]": {
"editor.defaultFormatter": "biomejs.biome"
},
"[typescript]": {
"editor.defaultFormatter": "biomejs.biome"
}
}
6 changes: 6 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@
"formatter": {
"quoteStyle": "single"
}
},
"files": {
"include": [
"*.ts",
"*.json"
]
}
}
1 change: 1 addition & 0 deletions packages/core/farm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default defineConfig({
targetEnv: 'node',
format: 'esm',
},
lazyCompilation: false,
persistentCache: false,
external: ['^@farmfe/core$'],
minify: false,
Expand Down
19 changes: 13 additions & 6 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@
"build": "farm build",
"preview": "farm preview",
"clean": "farm clean",
"farmup": "node ./bin/farmup.js --no-exec -w"
"farmup": "node ./bin/farmup.js --no-exec -w",
"prepublish": "mv ../../README.md ./"
},
"bin": "./bin/farmup.js",
"devDependencies": {
"@farmfe/cli": "^1.0.2",
"@farmfe/cli": "^1.0.3",
"@types/fs-extra": "^11.0.4",
"@types/lodash-es": "^4.17.12",
"@types/node": "^20.12.7",
"typescript": "^5.4.5",
"cac": "^6.7.14",
"execa": "^8.0.1",
"fs-extra": "^11.2.0",
"glob": "^10.3.15",
"lodash-es": "^4.17.21"
"lodash-es": "^4.17.21",
"typescript": "^5.4.5"
},
"exports": {
".": {
Expand All @@ -39,6 +40,12 @@
"types": "./dist/plugin.d.ts"
}
},
"files": [
"dist",
"README.md",
"package.json",
"bin"
],
"keywords": [
"farm",
"cli",
Expand All @@ -56,6 +63,6 @@
"email": "[email protected]"
},
"dependencies": {
"@farmfe/core": "^1.3.6"
"@farmfe/core": "^1.3.12"
}
}
}
47 changes: 32 additions & 15 deletions packages/core/src/config/normalize/find-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ import path from 'node:path';
import { ExecuteMode, type CommonOptions, type Format, type ResolvedCommonOptions } from '../../types/options';
import { isExists } from '../../util/file';

const findEntryKeyFromObject = (input: Record<string, string>) => {
return Object.keys(input)[0];
const findEntryKeyFromObject = (input: Record<string, string | undefined | null>) => {
let entry = null;
for (const key in input) {
if (input[key]) {
entry = key;
break;
}
}
return entry;
};

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

const defaultCompilationInputKeys = Object.keys(config.compilation?.input ?? {}).reduce(
(res, key) => Object.assign(res, { [key]: null }),
{} as Record<string, null | undefined | string>,
);

const clearDefault = (obj: Record<string, null | string | undefined>) => ({
...defaultCompilationInputKeys,
...obj,
});

// cli option > config
if (entriesFromOption) {
return entriesFromOption;
return clearDefault(entriesFromOption);
}

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

if (findEntryKey) return config.compilation?.input!;
if (findEntryKey) return clearDefault({ [findEntryKey]: config.compilation?.input?.[findEntryKey] });

let findEntry: string | null = null;

Expand All @@ -116,9 +133,7 @@ export async function tryFindEntryFromUserConfig(logger: Logger, config: UserCon
} else {
logger.info(`automatic find and use this entry: "${findEntry}"`);
}
return {
[findEntryKey]: findEntry,
};
return clearDefault({ [findEntryKey]: findEntry });
}

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

const executeMode = options.execute.type;


if(options.target?.startsWith('browser')) {
if (options.target?.startsWith('browser')) {
return;
}

if ((executeMode === ExecuteMode.Custom || executeMode === ExecuteMode.Node) && !options.noExecute) {
options.entry = Object.entries(options.entry).reduce((res, [key, val]) => {
res[`${key}.${formatMapExt[options.format ?? 'cjs']}`] = val;
return res;
}, {} as Record<string, string>);
if (executeMode === ExecuteMode.Node && !options.noExecute) {
options.entry = Object.entries(options.entry).reduce(
(res, [key, val]) => {
if (val) res[`${key}.${formatMapExt[options.format ?? 'cjs']}`] = val;
return res;
},
{} as Record<string, string>,
);

options.outputEntry = {
name: '[entryName]'
name: '[entryName]',
};
}
}
5 changes: 3 additions & 2 deletions packages/core/src/config/normalize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function normalizedExecuted(commonOption: CommonOptions, options: ResolvedCommon
}
} else {
// from entry file ext
const entryFiles = Object.values(options.entry);
const entryFiles = Object.values(options.entry).filter(Boolean) as string[];
let res: ExecuteMode | undefined;
for (const item of entryFiles) {
const targetFromExt = extensionMapExecutedMode[path.extname(item).slice(1)];
Expand Down Expand Up @@ -128,7 +128,8 @@ export function normalizedTargetEnv(
} else {
let targetFromInput: TargetEnv | undefined;

for (const entryFile of Object.values(options.entry)) {
const entryFiles = Object.values(options.entry).filter(Boolean) as string[];
for (const entryFile of entryFiles) {
const ext = path.extname(entryFile).slice(1);
if (extMapTargetEnv[ext]) {
targetFromInput = extMapTargetEnv[ext];
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/plugins/auto-execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default function autoExecute(options: CommonOptions = {}, logger = defaul

return {
name: `${name}:execute`,
priority: Number.NEGATIVE_INFINITY,
priority: Number.MIN_SAFE_INTEGER,
async config(config) {
return await normalizeOption.config(config);
},
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export interface CommonOptions {
}

export interface ResolvedCommonOptions {
entry: Record<string, string>;
entry: Record<string, string | null | undefined>;
args: string[];
execute: ExecuteOption;
external: [];
Expand Down
Loading

0 comments on commit 7453cfc

Please sign in to comment.