Skip to content

Commit

Permalink
linked and installed module dependencies resolve paths
Browse files Browse the repository at this point in the history
  • Loading branch information
cczw2010 committed Jan 19, 2024
1 parent 4cedae5 commit 225339f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
29 changes: 14 additions & 15 deletions src/moduleLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,23 @@ export async function initModules(options){
moduleConfigs.linkModuleRoots.push(process.env.__VITESCVROOT)
moduleConfigs.linkModulePaths.push(resolve(process.env.__VITESCVROOT,'node_modules'))
}

const resolvePaths = [
join(process.env.__PROJECTROOT,'node_modules'),
join(process.env.__PROJECTROOT,'node_modules/.pnpm/node_modules')
]
for (let moduleName in moduleOptions) {
let isPackage = !existsSync(resolve(process.env.__PROJECTROOT,moduleName)) //是否安装的包,而不是内部文件
let moduleIndex = normalizePath(require.resolve(moduleName,{
paths:resolvePaths
paths:[process.env.__PROJECTROOT]
}))
if(!moduleIndex){
console.error(`[vitescv] [${moduleName}] not exit`)
continue
}
try{
let dir = getModuleRootPathByIndex(moduleIndex)
let dir = getModuleRootPathByIndex(moduleIndex,moduleName,isPackage,!moduleIndex.startsWith(process.env.__PROJECTROOT))
let moduleInfo = {
idx:moduleMap.size,
origin:moduleName,
option: moduleOptions[moduleName]||{},
source:moduleIndex,
dir,
// dst:normalizePath(join(dir,'runtime.js')),
isPackage,
}
moduleConfigs.linkModuleRoots.push(dir)
Expand All @@ -85,8 +79,6 @@ export async function initModules(options){
if(moduleInfo.isPackage && !moduleInfo.source.startsWith(process.env.__PROJECTROOT)){
moduleConfigs.linkModulePaths.push(join(moduleInfo.dir,'node_modules'))
}
// const = checkModuleLink(moduleInfo,resolvePaths)
// Object.assign(moduleConfigs.alias,deps)

if(isPackage){
moduleConfigs.optimizeInclude.push(moduleName)
Expand Down Expand Up @@ -246,12 +238,19 @@ function transformedCode(id,code){
}
}

// 根据入口文件地址获取node_module的位置
function getModuleRootPathByIndex(fpath){
let dir = dirname(fpath)
// 获取模块的根目录
function getModuleRootPathByIndex(moduleIndex,moduleName,isPackage,isLink){
let dir = dirname(moduleIndex)
if(!isPackage){
return dir
}
if(dir){
if(!existsSync(join(dir,'node_modules'))){
return getModuleRootPathByIndex(dir)
if(isLink){
if(!existsSync(resolve(dir,'package.json'))){
return getModuleRootPathByIndex(dir,moduleName,isPackage,isLink)
}
}else if(!dir.endsWith(moduleName)){
return getModuleRootPathByIndex(dir,moduleName,isPackage,isLink)
}
return dir
}
Expand Down
2 changes: 1 addition & 1 deletion vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export default function(userConfig){
//💡 除了input(index.html)文件来检测需要预构建的依赖项外,指定其他入口文件检索
// entries:[],
//💡 默认情况下,不在 node_modules 中的,链接的包不会被预构建。使用此选项可强制预构建链接的包。
include:Config.optimizeInclude,
// include:Config.optimizeInclude,
// include:['element-ui/lib/*.js'],
// include:['@vitescv/elementui > element-ui'],
// 💡 排除的预构建,vitescv/app包含虚拟模块,预构建的时候并不存在,会报错
Expand Down

0 comments on commit 225339f

Please sign in to comment.