Skip to content

Commit

Permalink
feat: 支持 rsbuild 插件
Browse files Browse the repository at this point in the history
  • Loading branch information
baranwang committed Nov 9, 2024
1 parent 1b65dc9 commit 177ee0c
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changeset/lemon-queens-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@iringo/modkit-shared": minor
"@iringo/modkit": minor
---

支持 rsbuild 插件
2 changes: 2 additions & 0 deletions packages/modkit/core/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ export class ModKitCli {
this.buildCommand.action(async () => {
process.env.NODE_ENV = 'production';

await runMaybeAsync(this.hooksRunner.onBeforeBuild);
await this.rsbuild.build();
await runMaybeAsync(this.hooksRunner.onAfterBuild, { distPath: this.rsbuild.context.distPath });
});
}

Expand Down
4 changes: 3 additions & 1 deletion packages/modkit/core/src/rsbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,10 @@ export const useRsbuild = async ({
to: path.join(assetsOutput, outputName),
}));

const { rsbuild: rsbuildConfig, ...tools } = config.tools ?? {};
const rsbuild = await createRsbuild({
rsbuildConfig: {
...rsbuildConfig,
source: {
entry: config.source?.scripts,
},
Expand Down Expand Up @@ -168,7 +170,7 @@ export const useRsbuild = async ({
printUrls: false,
},
environments,
tools: config.tools,
tools,
},
});

Expand Down
7 changes: 7 additions & 0 deletions packages/modkit/plugins/egern/modern.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { dualBuildConfigs } from '@iringo/modkit-config/modern.config.ts';
import { defineConfig, moduleTools } from '@modern-js/module-tools';

export default defineConfig({
plugins: [moduleTools()],
buildConfig: dualBuildConfigs,
});
28 changes: 28 additions & 0 deletions packages/modkit/plugins/egern/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@iringo/modkit-plugin-egern",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"scripts": {
"dev": "modern build -w",
"build": "modern build"
},
"files": ["dist", "types", "CHANGELOG.md"],
"dependencies": {
"@iringo/modkit-shared": "workspace:^",
"@iringo/surge2egern": "^1.1.0"
},
"devDependencies": {
"@iringo/modkit-config": "workspace:^",
"@modern-js/module-tools": "^2.60.2",
"@types/node": "^20.0.0",
"typescript": "^5.6.2"
},
"repository": {
"type": "git",
"url": "https://github.com/NSRingo/engineering-solutions",
"directory": "packages/modkit/apps/egern"
}
}
34 changes: 34 additions & 0 deletions packages/modkit/plugins/egern/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import fs from 'node:fs';
import path from 'node:path';
import type { ModkitPlugin } from '@iringo/modkit-shared';
import { Surge2Egern } from '@iringo/surge2egern';

export const pluginEgern = (): ModkitPlugin => {
return {
name: 'egern',
setup() {
return {
onAfterBuild({ distPath }) {
if (!distPath || !fs.existsSync(distPath)) {
return;
}

const surgeModules = fs.readdirSync(distPath).reduce<string[]>((acc, file) => {
if (path.extname(file) === '.sgmodule') {
acc.push(path.join(distPath, file));
}
return acc;
}, []);
const surge2egern = new Surge2Egern();
Promise.allSettled(
surgeModules.map(async (surgeModule) => {
const result = await surge2egern.transformModule(surgeModule);
const targetPath = surgeModule.replace('.sgmodule', '.yaml');
return fs.promises.writeFile(targetPath, result);
}),
);
},
};
},
};
};
9 changes: 9 additions & 0 deletions packages/modkit/plugins/egern/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "@iringo/modkit-config/tsconfig",
"compilerOptions": {
"lib": ["ESNext"],
"outDir": "./dist",
"baseUrl": "./"
},
"include": ["src", "types"]
}
11 changes: 7 additions & 4 deletions packages/modkit/shared/src/plugin/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import type {
CommandsParams,
ConfigurePlatformReturn,
ModifySourceParams,
OnAfterStartDevServer,
OnBeforeStartDevServer,
OnAfterBuildParams,
OnAfterStartDevServerParams,
OnBeforeStartDevServerParams,
SourceConfig,
TemplateParametersParams,
} from '../types';
Expand All @@ -17,8 +18,10 @@ const hooks = {
configurePlatform: createWorkflow<void, ConfigurePlatformReturn>(),
modifySource: createAsyncWorkflow<ModifySourceParams, SourceConfig>(),
templateParameters: createWorkflow<TemplateParametersParams, Record<string, any>>(),
onBeforeStartDevServer: createAsyncWorkflow<OnBeforeStartDevServer, void>(),
onAfterStartDevServer: createAsyncWorkflow<OnAfterStartDevServer, void>(),
onBeforeStartDevServer: createAsyncWorkflow<OnBeforeStartDevServerParams, void>(),
onAfterStartDevServer: createAsyncWorkflow<OnAfterStartDevServerParams, void>(),
onBeforeBuild: createAsyncWorkflow<void, void>(),
onAfterBuild: createAsyncWorkflow<OnAfterBuildParams, void>(),
commands: createAsyncWorkflow<CommandsParams, void>(),
};

Expand Down
9 changes: 8 additions & 1 deletion packages/modkit/shared/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ export interface DevConfig {
port?: number;
}

export interface ModkitConfigTools
extends Pick<NonNullable<RsbuildConfig['tools']>, 'bundlerChain' | 'rspack' | 'swc'> {
rsbuild?: {
plugins?: RsbuildConfig['plugins'];
};
}

export interface ModkitConfig {
source?: SourceConfig;
dev?: DevConfig;
output?: Output;
tools?: Pick<NonNullable<RsbuildConfig['tools']>, 'bundlerChain' | 'rspack' | 'swc'>;
tools?: ModkitConfigTools;
plugins?: ModkitPlugin[];
}

Expand Down
20 changes: 16 additions & 4 deletions packages/modkit/shared/src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,22 @@ export interface ConfigurePlatformReturn {
template: string;
}

export interface OnBeforeStartDevServer {
export interface OnBeforeStartDevServerParams {
app: Express;
isFirstCompile: boolean;
}

export interface OnAfterStartDevServer {
export interface OnAfterStartDevServerParams {
app: Express;
isFirstCompile: boolean;
httpServer: Server;
rsbuildServer: RsbuildDevServer;
}

export interface OnAfterBuildParams {
distPath?: string;
}

export interface CommandsParams {
program: commander.Command;
}
Expand All @@ -63,11 +67,19 @@ export interface PluginHooks {
/**
* 启动开发服务器前
*/
onBeforeStartDevServer?: AsyncWorker<OnBeforeStartDevServer, void>;
onBeforeStartDevServer?: AsyncWorker<OnBeforeStartDevServerParams, void>;
/**
* 启动开发服务器后
*/
onAfterStartDevServer?: AsyncWorker<OnAfterStartDevServer, void>;
onAfterStartDevServer?: AsyncWorker<OnAfterStartDevServerParams, void>;
/**
* 构建前
*/
onBeforeBuild?: AsyncWorker<void, void>;
/**
* 构建后
*/
onAfterBuild?: AsyncWorker<OnAfterBuildParams, void>;
/**
* 为 commander 添加新的 CLI 命令
*/
Expand Down
35 changes: 35 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 177ee0c

Please sign in to comment.