From e11e2d8cd71202534e6f46f22817699c52a7e666 Mon Sep 17 00:00:00 2001 From: "Xc@centOs" Date: Thu, 29 Feb 2024 22:04:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E4=BA=86i18n=E7=9A=84?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=9C=A8node=5Fmodules/.../node=5Fmodules?= =?UTF-8?q?=E4=B8=AD=E5=A4=9A=E5=B1=82=E7=BA=A7=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/babel-plugin/oakI18n.js | 55 +++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/config/babel-plugin/oakI18n.js b/config/babel-plugin/oakI18n.js index 0f398c9..b972e65 100644 --- a/config/babel-plugin/oakI18n.js +++ b/config/babel-plugin/oakI18n.js @@ -1,5 +1,5 @@ const fs = require('fs'); -const { relative, resolve, join } = require('path'); +const { relative, resolve, join, dirname } = require('path'); const t = require('@babel/types'); const assert = require('assert'); const { fork } = require('child_process'); @@ -35,34 +35,47 @@ function parseFileModuleAndNs(cwd, filename) { } } const relativePath = relative(cwd2, filename); - if (relativePath.startsWith('node_modules') || relativePath.startsWith('..')) { // 在测试环境下是相对路径 - const moduleRelativePath = relativePath - .replace(/\\/g, '/') - .split('/') - .slice(0, 2); - const modulePath = join(cwd2, ...moduleRelativePath); - const moduleDir = moduleRelativePath[1]; + // 寻找离filename最近层的package.json,看作项目名称 + // 用relativePath来递进查找在操作系统层面更安全 + const fileDirPaths = relativePath.replace(/\\/g, '/').split('/'); - let moduleName = ModuleDict[moduleDir]; - if (!moduleName) { - const { name } = require(join(modulePath, 'package.json')); - ModuleDict[moduleDir] = name; - moduleName = name; - } - const rel2paths = relative(modulePath, filename) - .replace(/\\/g, '/') - .split('/'); + let iter = fileDirPaths.length; + let filePrjRootPath = ''; + let moduleName = ''; + do { + filePrjRootPath = join(cwd2, ...fileDirPaths.slice(0, iter)); + const pkgJsonPath = join(filePrjRootPath, 'package.json'); + if (fs.existsSync(pkgJsonPath)) { + moduleName = ModuleDict[filePrjRootPath]; + if (!moduleName) { + const { name } = require(pkgJsonPath); + moduleName = name; + ModuleDict[filePrjRootPath] = name; + } + break; + } + else { + assert(iter > 0); + iter--; + } + } while (true); + + // 引用的第三方项目目前合法的只有components下的组件,pages是容一些较早的遗留代码 + const fileRelPathInPrj = relative(filePrjRootPath, filename).replace(/\\/g, '/').split('/');; let ns; - switch (rel2paths[1]) { + switch (fileRelPathInPrj[1]) { case 'pages': { - ns = `${moduleName}-p-${rel2paths.slice(2, rel2paths.length - 1).join('-')}`; + ns = `${moduleName}-p-${fileRelPathInPrj.slice(2, fileRelPathInPrj.length - 1).join('-')}`; break; } default: { - assert(rel2paths[1] === 'components', rel2paths.join('//')); - ns = `${moduleName}-c-${rel2paths.slice(2, rel2paths.length - 1).join('-')}`; + if (fileRelPathInPrj[1] !== 'components') { + assert(false); + } + assert(fileRelPathInPrj[1] === 'components', fileRelPathInPrj.join('//')); + ns = `${moduleName}-c-${fileRelPathInPrj.slice(2, fileRelPathInPrj.length - 1).join('-')}`; break; } }