Skip to content

Commit

Permalink
feat: content 支持函数式入参
Browse files Browse the repository at this point in the history
  • Loading branch information
baranwang committed Nov 10, 2024
1 parent c4872bd commit 9674b0f
Show file tree
Hide file tree
Showing 18 changed files with 74 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/famous-swans-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@iringo/modkit": minor
---

content 支持函数式入参
5 changes: 5 additions & 0 deletions .changeset/witty-ants-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@iringo/modkit": minor
---

开放部分 dev 配置
10 changes: 9 additions & 1 deletion packages/modkit/core/src/rsbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'node:path';
import {
type IAppContext,
type ModkitConfig,
type ModkitPlugin,
type PluginType,
address,
getPluginContext,
Expand Down Expand Up @@ -77,6 +78,11 @@ const generateEnvironment = async ({

// 处理 source
const sourceBackup = lodash.cloneDeep(config.source);
if (typeof sourceBackup.content === 'function') {
sourceBackup.content = await runMaybeAsync(sourceBackup.content, {
pluginName: plugin.name as ModkitPlugin['name'],
});
}
const source = (await runMaybeAsync(pluginCtx.modifySource, { source: sourceBackup })) ?? sourceBackup;

// 设置输出模块名
Expand Down Expand Up @@ -139,6 +145,7 @@ export const useRsbuild = async ({
}));

const { rsbuild: rsbuildPartialConfig, ...tools } = config.tools ?? {};
const { port = 3000, ...devConfig } = config.dev ?? {};
const rsbuildConfig: RsbuildConfig = {
...rsbuildPartialConfig,
source: {
Expand All @@ -156,7 +163,8 @@ export const useRsbuild = async ({
copy: assetsCopy,
},
dev: {
assetPrefix: `http://${address.ip()}:${config.dev?.port ?? 3000}`,
...devConfig,
assetPrefix: `http://${address.ip()}:${port}`,
hmr: false,
liveReload: false,
},
Expand Down
4 changes: 3 additions & 1 deletion packages/modkit/plugins/boxjs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import '@iringo/modkit-shared';
import type { BoxJsType } from '../dist';

declare module '@iringo/modkit-shared' {
interface PluginModuleContent {}
interface ModkitPluginName {
boxjs: 'boxjs';
}

interface PluginArgumentType {
boxJs?: BoxJsType | 'exclude';
Expand Down
2 changes: 1 addition & 1 deletion packages/modkit/plugins/egern/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"files": ["dist", "types", "CHANGELOG.md"],
"dependencies": {
"@iringo/modkit-shared": "workspace:^",
"@iringo/surge2egern": "^1.1.0"
"@iringo/surge2egern": "workspace:^"
},
"devDependencies": {
"@iringo/modkit-config": "workspace:^",
Expand Down
5 changes: 4 additions & 1 deletion packages/modkit/plugins/egern/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Surge2Egern } from '@iringo/surge2egern';
export const pluginEgern = (): ModkitPlugin => {
return {
name: 'egern',

setup() {
return {
onAfterBuild({ distPath }) {
Expand All @@ -26,7 +27,9 @@ export const pluginEgern = (): ModkitPlugin => {
const targetPath = surgeModule.replace('.sgmodule', '.yaml');
return fs.promises.writeFile(targetPath, result);
}),
);
).finally(() => {
surge2egern.destroy();
});
},
};
},
Expand Down
7 changes: 7 additions & 0 deletions packages/modkit/plugins/egern/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import '@iringo/modkit-shared';

declare module '@iringo/modkit-shared' {
interface ModkitPluginName {
egern: 'egern';
}
}
4 changes: 4 additions & 0 deletions packages/modkit/plugins/loon/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import '@iringo/modkit-shared';
import type { LoonArgumentType } from '../dist/index';

declare module '@iringo/modkit-shared' {
interface ModkitPluginName {
loon: 'loon';
}

interface PluginModuleContent {
loonGeneral?: Record<string, string>;
}
Expand Down
7 changes: 7 additions & 0 deletions packages/modkit/plugins/quantumultx/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import '@iringo/modkit-shared';

declare module '@iringo/modkit-shared' {
interface ModkitPluginName {
quantumultx: 'quantumultx';
}
}
4 changes: 2 additions & 2 deletions packages/modkit/plugins/stash/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const pluginStash = (): ModkitPlugin => {
configurePlatform() {
return {
extension: '.stoverride',
template: '<%= stashTemplate.output %>',
template: '<%= output %>',
};
},
modifySource({ source }) {
Expand All @@ -25,7 +25,7 @@ export const pluginStash = (): ModkitPlugin => {
templateParameters(params) {
const stashTemplate = new StashTemplate(params);
return {
stashTemplate,
output: stashTemplate.output,
};
},
};
Expand Down
4 changes: 3 additions & 1 deletion packages/modkit/plugins/stash/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ export class StashTemplate extends Template {
}

get #override(): StashOverride {
const { name, description, ...rest } = this.metadata;
const { name, description, version, system, ...rest } = this.metadata;
const result: StashOverride = {};
result.name = name;
result.desc = description;
result.version = version;
result.system = system;
Object.entries(rest).forEach(([key, value]) => {
result[key] = Array.isArray(value) ? value.join('\n') : value;
});
Expand Down
3 changes: 3 additions & 0 deletions packages/modkit/plugins/stash/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import '@iringo/modkit-shared';

declare module '@iringo/modkit-shared' {
interface ModkitPluginName {
stash: 'stash';
}
interface PluginArgumentType {
stash?: 'exclude';
}
Expand Down
4 changes: 4 additions & 0 deletions packages/modkit/plugins/surge/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import '@iringo/modkit-shared';

declare module '@iringo/modkit-shared' {
interface ModkitPluginName {
surge: 'surge';
}

interface PluginModuleContent {
surgeGeneral?: Record<string, string>;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/modkit/shared/src/plugin/template.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ModuleContent } from '../types';
import type { TemplateParametersParams } from '../types/plugin';
import { handleArgumentsDefaultValue } from '../utils';

Expand All @@ -12,7 +13,7 @@ export class Template {
return this.source?.metadata || {};
}

get content() {
get content(): ModuleContent {
return this.source?.content || {};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/modkit/shared/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Output } from './output';
import type { ModkitPlugin } from './plugin';
import type { SourceConfig } from './source';

export interface DevConfig {
export interface DevConfig extends Pick<NonNullable<RsbuildConfig['dev']>, 'progressBar' | 'writeToDisk'> {
/**
* @default 3000
*/
Expand Down
6 changes: 5 additions & 1 deletion packages/modkit/shared/src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,15 @@ export interface PluginHooks {
commands?: AsyncWorker<CommandsParams, void>;
}

export interface ModkitPluginName {
dts: 'dts';
}

export interface ModkitPlugin {
/**
* 插件名称
*/
name: string;
name: keyof ModkitPluginName & string;

setup: (api: PluginAPI) => PluginHooks;
}
9 changes: 8 additions & 1 deletion packages/modkit/shared/src/types/source/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ModkitPlugin } from '../plugin';
import type { ArgumentItem } from './argument';
import type { ModuleMetadata } from './metadata';
import type { ModuleMITM } from './mitm';
Expand All @@ -19,6 +20,12 @@ export interface ModuleContent extends PluginModuleContent {
mitm?: ModuleMITM;
}

export type ModuleContentType =
| ModuleContent
| ((options: {
pluginName: ModkitPlugin['name'];
}) => ModuleContent | PromiseLike<ModuleContent>);

export interface SourceConfig {
/**
* 模块名称
Expand All @@ -35,7 +42,7 @@ export interface SourceConfig {
/**
* 模块内容
*/
content?: ModuleContent;
content?: ModuleContentType;
/**
* 待编译的脚本
*/
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

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

0 comments on commit 9674b0f

Please sign in to comment.