diff --git a/.changeset/lemon-queens-smash.md b/.changeset/lemon-queens-smash.md new file mode 100644 index 0000000..9eb22c3 --- /dev/null +++ b/.changeset/lemon-queens-smash.md @@ -0,0 +1,6 @@ +--- +"@iringo/modkit-shared": minor +"@iringo/modkit": minor +--- + +支持 rsbuild 插件 diff --git a/packages/modkit/core/src/cli.ts b/packages/modkit/core/src/cli.ts index c9338b0..c311798 100644 --- a/packages/modkit/core/src/cli.ts +++ b/packages/modkit/core/src/cli.ts @@ -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 }); }); } diff --git a/packages/modkit/core/src/rsbuild.ts b/packages/modkit/core/src/rsbuild.ts index 3d355a9..a0141b2 100644 --- a/packages/modkit/core/src/rsbuild.ts +++ b/packages/modkit/core/src/rsbuild.ts @@ -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, }, @@ -168,7 +170,7 @@ export const useRsbuild = async ({ printUrls: false, }, environments, - tools: config.tools, + tools, }, }); diff --git a/packages/modkit/plugins/egern/modern.config.ts b/packages/modkit/plugins/egern/modern.config.ts new file mode 100644 index 0000000..80bd418 --- /dev/null +++ b/packages/modkit/plugins/egern/modern.config.ts @@ -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, +}); diff --git a/packages/modkit/plugins/egern/package.json b/packages/modkit/plugins/egern/package.json new file mode 100644 index 0000000..230148a --- /dev/null +++ b/packages/modkit/plugins/egern/package.json @@ -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" + } +} diff --git a/packages/modkit/plugins/egern/src/index.ts b/packages/modkit/plugins/egern/src/index.ts new file mode 100644 index 0000000..1d20f73 --- /dev/null +++ b/packages/modkit/plugins/egern/src/index.ts @@ -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((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); + }), + ); + }, + }; + }, + }; +}; diff --git a/packages/modkit/plugins/egern/tsconfig.json b/packages/modkit/plugins/egern/tsconfig.json new file mode 100644 index 0000000..d597abe --- /dev/null +++ b/packages/modkit/plugins/egern/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "@iringo/modkit-config/tsconfig", + "compilerOptions": { + "lib": ["ESNext"], + "outDir": "./dist", + "baseUrl": "./" + }, + "include": ["src", "types"] +} diff --git a/packages/modkit/shared/src/plugin/manager.ts b/packages/modkit/shared/src/plugin/manager.ts index d19a375..2a25191 100644 --- a/packages/modkit/shared/src/plugin/manager.ts +++ b/packages/modkit/shared/src/plugin/manager.ts @@ -5,8 +5,9 @@ import type { CommandsParams, ConfigurePlatformReturn, ModifySourceParams, - OnAfterStartDevServer, - OnBeforeStartDevServer, + OnAfterBuildParams, + OnAfterStartDevServerParams, + OnBeforeStartDevServerParams, SourceConfig, TemplateParametersParams, } from '../types'; @@ -17,8 +18,10 @@ const hooks = { configurePlatform: createWorkflow(), modifySource: createAsyncWorkflow(), templateParameters: createWorkflow>(), - onBeforeStartDevServer: createAsyncWorkflow(), - onAfterStartDevServer: createAsyncWorkflow(), + onBeforeStartDevServer: createAsyncWorkflow(), + onAfterStartDevServer: createAsyncWorkflow(), + onBeforeBuild: createAsyncWorkflow(), + onAfterBuild: createAsyncWorkflow(), commands: createAsyncWorkflow(), }; diff --git a/packages/modkit/shared/src/types/index.ts b/packages/modkit/shared/src/types/index.ts index aba81fd..3798a95 100644 --- a/packages/modkit/shared/src/types/index.ts +++ b/packages/modkit/shared/src/types/index.ts @@ -10,11 +10,18 @@ export interface DevConfig { port?: number; } +export interface ModkitConfigTools + extends Pick, 'bundlerChain' | 'rspack' | 'swc'> { + rsbuild?: { + plugins?: RsbuildConfig['plugins']; + }; +} + export interface ModkitConfig { source?: SourceConfig; dev?: DevConfig; output?: Output; - tools?: Pick, 'bundlerChain' | 'rspack' | 'swc'>; + tools?: ModkitConfigTools; plugins?: ModkitPlugin[]; } diff --git a/packages/modkit/shared/src/types/plugin.ts b/packages/modkit/shared/src/types/plugin.ts index 30b806b..4483c76 100644 --- a/packages/modkit/shared/src/types/plugin.ts +++ b/packages/modkit/shared/src/types/plugin.ts @@ -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; } @@ -63,11 +67,19 @@ export interface PluginHooks { /** * 启动开发服务器前 */ - onBeforeStartDevServer?: AsyncWorker; + onBeforeStartDevServer?: AsyncWorker; /** * 启动开发服务器后 */ - onAfterStartDevServer?: AsyncWorker; + onAfterStartDevServer?: AsyncWorker; + /** + * 构建前 + */ + onBeforeBuild?: AsyncWorker; + /** + * 构建后 + */ + onAfterBuild?: AsyncWorker; /** * 为 commander 添加新的 CLI 命令 */ diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 88371c4..b9a3cb1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -239,6 +239,28 @@ importers: specifier: ^5.6.2 version: 5.6.2 + packages/modkit/plugins/egern: + dependencies: + '@iringo/modkit-shared': + specifier: workspace:^ + version: link:../../shared + '@iringo/surge2egern': + specifier: ^1.1.0 + version: 1.1.0(typescript@5.6.2) + devDependencies: + '@iringo/modkit-config': + specifier: workspace:^ + version: link:../../config + '@modern-js/module-tools': + specifier: ^2.60.2 + version: 2.60.2(typescript@5.6.2) + '@types/node': + specifier: ^20.0.0 + version: 20.16.10 + typescript: + specifier: ^5.6.2 + version: 5.6.2 + packages/modkit/plugins/loon: dependencies: '@iringo/modkit-shared': @@ -1645,6 +1667,9 @@ packages: cpu: [x64] os: [win32] + '@iringo/surge2egern@1.1.0': + resolution: {integrity: sha512-8DEu6sIeWCTC0B6TOzXaGyI4IBnhuKKXE0sgNjyA/dfcwoT4jQDoDlaTQUHh2MVqB8OfD2R4gStaogw+o9qqng==} + '@istanbuljs/load-nyc-config@1.1.0': resolution: {integrity: sha512-VjeHSlIzpv/NyD3N0YuHfXOPDIixcA1q2ZV98wsMqcYlPmv2n3Yb2lYP9XMElnaFVXg5A7YLTeLu6V84uQDjmQ==} engines: {node: '>=8'} @@ -7084,6 +7109,16 @@ snapshots: '@esbuild/win32-x64@0.19.2': optional: true + '@iringo/surge2egern@1.1.0(typescript@5.6.2)': + dependencies: + ora: 5.4.1 + puppeteer: 23.6.1(typescript@5.6.2) + transitivePeerDependencies: + - bufferutil + - supports-color + - typescript + - utf-8-validate + '@istanbuljs/load-nyc-config@1.1.0': dependencies: camelcase: 5.3.1