From 1499caea40f206054146462d57168e9146fd9fed Mon Sep 17 00:00:00 2001 From: gchust Date: Tue, 24 Dec 2024 14:11:23 +0800 Subject: [PATCH] fix: loading error when running nocobase under subpath (#5924) * fix: plugin dynamic import should use relative path * fix: output public path * fix(buildPlugin): update public path handling and add custom public path plugin --- packages/core/build/src/buildPlugin.ts | 38 ++++++++++++++++++++------ 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/packages/core/build/src/buildPlugin.ts b/packages/core/build/src/buildPlugin.ts index 2a224b85f..f851f37ed 100644 --- a/packages/core/build/src/buildPlugin.ts +++ b/packages/core/build/src/buildPlugin.ts @@ -339,7 +339,7 @@ export async function buildPluginClient(cwd: string, userConfig: UserConfig, sou path: outDir, filename: outputFileName, chunkFilename: '[chunkhash].js', - publicPath: `/static/plugins/${packageJson.name}/dist/client/`, + publicPath: `auto`, // will be generated by the custom plugin clean: true, library: { name: packageJson.name, @@ -467,14 +467,36 @@ export async function buildPluginClient(cwd: string, userConfig: UserConfig, sou new rspack.DefinePlugin({ 'process.env.NODE_ENV': JSON.stringify('production'), }), + { + apply(compiler) { + compiler.hooks.compilation.tap('CustomPublicPathPlugin', (compilation) => { + compilation.hooks.runtimeModule.tap('CustomPublicPathPlugin', (module) => { + if (module.name === 'auto_public_path') { + // 处理所有可能的情况 + module.source = { + source: ` +__webpack_require__.p = (function() { + var publicPath = window['__nocobase_public_path__'] || '/'; + // 确保路径以 / 结尾 + if (!publicPath.endsWith('/')) { + publicPath += '/'; + } + return publicPath + 'static/plugins/${packageJson.name}/dist/client/'; +})();` + }; + } + }); + }); + } + }, process.env.BUILD_ANALYZE === 'true' && - new RsdoctorRspackPlugin({ - // plugin options - // supports: { - // generateTileGraph: true, - // }, - mode: 'brief', - }), + new RsdoctorRspackPlugin({ + // plugin options + // supports: { + // generateTileGraph: true, + // }, + mode: 'brief', + }), ].filter(Boolean), node: { global: true,