From a93eecbbb88d1caa0f0a167ef1e7cff1ca6be599 Mon Sep 17 00:00:00 2001 From: Jian Lu Date: Sun, 6 Apr 2025 13:27:52 +0800 Subject: [PATCH] feat: add defaut txt match --- packages/core/build/src/buildPlugin.ts | 12 ++++++------ .../plugins/pluginEsbuildCommercialInject.ts | 19 +++++++++++++++---- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/packages/core/build/src/buildPlugin.ts b/packages/core/build/src/buildPlugin.ts index 9bf07f52f0..48a1e1f9f6 100644 --- a/packages/core/build/src/buildPlugin.ts +++ b/packages/core/build/src/buildPlugin.ts @@ -377,12 +377,12 @@ export async function buildProPluginServer(cwd: string, userConfig: UserConfig, '.json': 'copy', }, esbuildPlugins: [pluginEsbuildCommercialInject], - onSuccess: async () => { - const serverFiles = [path.join(cwd, target_dir, 'server', 'index.js')]; - serverFiles.forEach((file) => { - obfuscate(file); - }); - }, + // onSuccess: async () => { + // const serverFiles = [path.join(cwd, target_dir, 'server', 'index.js')]; + // serverFiles.forEach((file) => { + // obfuscate(file); + // }); + // }, }), ); fs.removeSync(tsconfig.path); diff --git a/packages/core/build/src/plugins/pluginEsbuildCommercialInject.ts b/packages/core/build/src/plugins/pluginEsbuildCommercialInject.ts index 09a1594520..bfdb45f3c3 100644 --- a/packages/core/build/src/plugins/pluginEsbuildCommercialInject.ts +++ b/packages/core/build/src/plugins/pluginEsbuildCommercialInject.ts @@ -12,20 +12,31 @@ import fs from 'node:fs' const pluginEsbuildCommercialInject = { name: 'plugin-esbuild-commercial-inject', setup(build) { - - build.onLoad({ filter: /src\/index\.ts$/ }, async (args) => { + build.onLoad({ filter: /src\/server\/index\.ts$/ }, async (args) => { let source = fs.readFileSync(args.path, 'utf8'); const regex = /export\s*\{\s*default\s*\}\s*from\s*(?:'([^']*)'|"([^"]*)");?/; // match: export { default } from './plugin'; + const regex2 = /export\s+default\s+([a-zA-Z_0-9]+)\s*;?/; // match: export default xxx; const match = source.match(regex); + const match2 = source.match(regex2); if (match) { source = source.replace(regex, ``); const moduleName = match[1] || match[2]; - source = -` + source = + ` import { withCommercial } from '@nocobase/plugin-commercial/server'; import _plugin from '${moduleName}'; export default withCommercial(_plugin); ${source} +`; + console.log(`Insert commercial server code success`); + } else if (match2) { + source = source.replace(regex2, ``); + const moduleName = match2[1] || match2[2]; + source = + ` +import { withCommercial } from '@nocobase/plugin-commercial/server'; +${source} +export default withCommercial(${moduleName}); `; console.log(`Insert commercial server code success`); } else {