feat: add defaut txt match

This commit is contained in:
Jian Lu 2025-04-06 13:27:52 +08:00
parent 4c46c71ae3
commit a93eecbbb8
2 changed files with 21 additions and 10 deletions

View File

@ -377,12 +377,12 @@ export async function buildProPluginServer(cwd: string, userConfig: UserConfig,
'.json': 'copy', '.json': 'copy',
}, },
esbuildPlugins: [pluginEsbuildCommercialInject], esbuildPlugins: [pluginEsbuildCommercialInject],
onSuccess: async () => { // onSuccess: async () => {
const serverFiles = [path.join(cwd, target_dir, 'server', 'index.js')]; // const serverFiles = [path.join(cwd, target_dir, 'server', 'index.js')];
serverFiles.forEach((file) => { // serverFiles.forEach((file) => {
obfuscate(file); // obfuscate(file);
}); // });
}, // },
}), }),
); );
fs.removeSync(tsconfig.path); fs.removeSync(tsconfig.path);

View File

@ -12,20 +12,31 @@ import fs from 'node:fs'
const pluginEsbuildCommercialInject = { const pluginEsbuildCommercialInject = {
name: 'plugin-esbuild-commercial-inject', name: 'plugin-esbuild-commercial-inject',
setup(build) { setup(build) {
build.onLoad({ filter: /src\/server\/index\.ts$/ }, async (args) => {
build.onLoad({ filter: /src\/index\.ts$/ }, async (args) => {
let source = fs.readFileSync(args.path, 'utf8'); let source = fs.readFileSync(args.path, 'utf8');
const regex = /export\s*\{\s*default\s*\}\s*from\s*(?:'([^']*)'|"([^"]*)");?/; // match: export { default } from './plugin'; 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 match = source.match(regex);
const match2 = source.match(regex2);
if (match) { if (match) {
source = source.replace(regex, ``); source = source.replace(regex, ``);
const moduleName = match[1] || match[2]; const moduleName = match[1] || match[2];
source = source =
` `
import { withCommercial } from '@nocobase/plugin-commercial/server'; import { withCommercial } from '@nocobase/plugin-commercial/server';
import _plugin from '${moduleName}'; import _plugin from '${moduleName}';
export default withCommercial(_plugin); export default withCommercial(_plugin);
${source} ${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`); console.log(`Insert commercial server code success`);
} else { } else {