Closed RandyZ closed 5 months ago
感谢。 在 4.8.1 的代码中(目前 main 已经合并),nextjs 已经升级到 14 了。可以合并下。
worker 异常问题主要来自于 tiktoken 包。目前已经用 wasm 替代,观测了一段时间,内存正常了。
你的修改是正确的。目前配置应该改成 serverComponentsExternalPackages 。麻烦从 main 分支进行 rebase 和修改。
你的修改是正确的。目前配置应该改成 serverComponentsExternalPackages 。麻烦从 main 分支进行 rebase 和修改。
好的,我会基于main再提交一个pr
例行检查
你的版本
问题描述, 日志截图 我是基于4.6.9做的定制性开发,由于后续想接入我们自己的模块项目,所以需要升级nextjs版本,从13升级到了14。改用了turbopack来作为打包工具。
近期准备更新一下FastGPT的代码到4.8.x,新的节点调试和节点之间交互是我们需要的。所以想做一下升级。
“Error: The packages specified in the 'transpilePackages' conflict with the 'serverComponentsExternalPackages': mongoose, pg”
复现步骤
原因和分析 产生的原因是在
v4.8-preview
的这个tag中增加了一段配置,导致用nextjs 14运行出现异常。我对于Nextjs中这几个选项的了解是这样的:
所以从意图上说希望'mongoose', 'pg'独立打包并且优化加载方式为require是没问题的,但是详细看了下13和14的实现细节:
在13版本中的作用是这样的
从下面的代码可以看出serverComponentsExternalPackages的优先级会高于transpilePackages生效,优先隔离掉服务端运行的包。不再做打包优化和兼容性处理
// Alias these modules to be resolved with "module" if possible. function getBarrelOptimizationAliases(packages) { const aliases = {}; const mainFields = [ "module", "main" ]; for (const pkg of packages){ try { const descriptionFileData = require(
${pkg}/package.json
); const descriptionFilePath = require.resolve(${pkg}/package.json
); for (const field of mainFields){ if (descriptionFileData.hasOwnProperty(field)) { aliases[pkg + "$"] = _path.default.join(_path.default.dirname(descriptionFilePath), descriptionFileData[field]); break; } } } catch {} } return aliases; } ........... // If a package is included intranspilePackages
, we don't want to make it external. // And also, if that resource is an ES module, we bundle it too because we can't // rely on the require hook to aliasreact
to our precompiled version. const shouldBeBundled = isResourceInPackages(res, config.transpilePackages, resolvedExternalPackageDirs) || isEsm && isAppLayer; if (/node_modules[/\].*.[mc]?js$/.test(res)) { if ((0, _utils.isWebpackServerLayer)(layer)) { // All packages should be bundled for the server layer if they're not opted out. // This option takes priority over the transpilePackages option. if (optOutBundlingPackageRegex.test(res)) { return${externalType} ${request}
; } return; } if (shouldBeBundled) return; // Anything else that is standard JavaScript withinnode_modules
// can be externalized. return${externalType} ${request}
; }// since
pages
doesn't always bundle by default we need to // auto-include optimizePackageImports in transpilePackages const finalTranspilePackages = config.transpilePackages || []; for (const pkg of config.experimental.optimizePackageImports || []){ if (!finalTranspilePackages.includes(pkg)) { finalTranspilePackages.push(pkg); } }// The
serverComponentsExternalPackages
should not conflict with // thetranspilePackages
. if (config.experimental.serverComponentsExternalPackages && finalTranspilePackages) { const externalPackageConflicts = finalTranspilePackages.filter((pkg)=>{ var _config_experimental_serverComponentsExternalPackages; return (_config_experimental_serverComponentsExternalPackages = config.experimental.serverComponentsExternalPackages) == null ? void 0 : _config_experimental_serverComponentsExternalPackages.includes(pkg); }); if (externalPackageConflicts.length > 0) { throw new Error(The packages specified in the 'transpilePackages' conflict with the 'serverComponentsExternalPackages': ${externalPackageConflicts.join(", ")}
); } } // 后面会创建handleExternals
来处理按需导入transpilePackages: ['@fastgpt/', 'ahooks', '@chakra-ui/', 'react'], experimental: { // 外部包独立打包 serverComponentsExternalPackages: ['mongoose', 'pg'], }