Skip to content

Commit

Permalink
feat(plugin-compiler): 优化幽灵依赖和自动注入功能兼容性
Browse files Browse the repository at this point in the history
  • Loading branch information
lyfeyaj committed Mar 31, 2023
1 parent c795fc2 commit e5f97e3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
37 changes: 24 additions & 13 deletions packages/plugin-compiler/src/parsers/scriptParserPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
asArray,
CompileTypes,
EntryBuilderHelpers,
FileParserOptions,
Expand Down Expand Up @@ -244,21 +245,28 @@ export class ScriptParserPlugin implements Plugin {
])
)

const { consumes, externals } = (runner?.userConfig ||
{}) as CompilerUserConfig

// 需要消费的 npm 包
// 通常用于 主/子 共享及消费依赖的场景
const consumingPackages = (
(runner?.userConfig as CompilerUserConfig)?.consumes || []
).reduce((res, value) => {
if (typeof value === 'string') {
res.add(value)
} else if (_.isPlainObject(value)) {
_.forEach(value, function (v) {
res.add(v)
})
}
const consumesOrExternalsPackages = []
.concat(asArray(consumes))
.concat(
// externals 支持的类型比较多,这里只处理 string 和 object
asArray(externals)
)
.reduce((res, value) => {
if (typeof value === 'string') {
res.add(value)
} else if (_.isPlainObject(value)) {
_.forEach(value, function (v) {
res.add(v)
})
}

return res
}, new Set<string>())
return res
}, new Set<string>())

const result: {
api: false | string
Expand All @@ -272,8 +280,9 @@ export class ScriptParserPlugin implements Plugin {
if (result[name]) return

// 如果子包已通过 consumes 配置 mor 的运行时, 则标记当前运行时为存在
if (consumingPackages.has(packageName)) {
if (consumesOrExternalsPackages.has(packageName)) {
result[name] = packageName
logger.debug(`在 consumes 或者 externals 中找到 ${packageName} 依赖`)
return
}

Expand All @@ -296,6 +305,8 @@ export class ScriptParserPlugin implements Plugin {
}
})

logger.debug(`运行时自动注入依赖为:`, result)

return result
}

Expand Down
31 changes: 21 additions & 10 deletions packages/plugin-compiler/src/plugins/phantomDependencyPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
asArray,
fsExtra as fs,
lodash as _,
logger,
Plugin,
Runner,
Expand All @@ -8,7 +10,7 @@ import {
WebpackWrapper
} from '@morjs/utils'
import path from 'path'
import { NODE_MODULE_REGEXP } from '../constants'
import { CompilerUserConfig, NODE_MODULE_REGEXP } from '../constants'

/**
* 检测项目中的幽灵依赖
Expand Down Expand Up @@ -72,7 +74,7 @@ export class PhantomDependencyPlugin implements Plugin {
externals = [],
consumes = [],
watch
} = runner.userConfig || {}
} = (runner.userConfig || {}) as CompilerUserConfig
if (!phantomDependency) return

let allDependencies = { ...this.getPkgDepend(runner.getCwd()) }
Expand All @@ -94,8 +96,11 @@ export class PhantomDependencyPlugin implements Plugin {
}

// 跳过在 externals 或 consumes 中配置的包
const otherDepends = [...externals, ...consumes].map((item) =>
this.getExternalsPkgName(item)
const otherDepends = [...asArray(externals), ...consumes].reduce(
(res, item) => {
return res.concat(this.getExternalsPkgName(item))
},
[]
)

const table = {
Expand All @@ -106,7 +111,7 @@ export class PhantomDependencyPlugin implements Plugin {
// 跳过已在 package.json 和 phantomDependency.exclude 中配置的依赖
for (const depKey in usedDependencies) {
if (
!(phantomDependency.exclude || []).includes(depKey) &&
!(phantomDependency['exclude'] || []).includes(depKey) &&
!allDependencies[depKey] &&
!aliasAll[depKey] &&
!aliasAll[depKey.split('/')[0]] &&
Expand All @@ -117,7 +122,7 @@ export class PhantomDependencyPlugin implements Plugin {
}

if (table.rows.length > 0) {
if (phantomDependency.mode === 'error') {
if (phantomDependency['mode'] === 'error') {
logger.error('检测到幽灵依赖,请添加到 package.json')
logger.table(table, 'error')

Expand All @@ -137,7 +142,11 @@ export class PhantomDependencyPlugin implements Plugin {
}

// 检查 script 和 sjs 文件 获取 import 和 require 的依赖进行检测
handlePhantomTransformer(transformers, fileInfoPath, usedDependencies) {
handlePhantomTransformer(
transformers: ts.CustomTransformers,
fileInfoPath: string,
usedDependencies: Record<string, string>
) {
transformers.before.push(
tsTransformerFactory((node) => {
if (!node) return node
Expand Down Expand Up @@ -200,12 +209,14 @@ export class PhantomDependencyPlugin implements Plugin {

/**
* 获取 externals 或 consumes 的依赖名
* NOTE: 有限支持,externals 可能为函数,这种情况下暂时忽略兼容性
* @param item 配置子项
*/
getExternalsPkgName(item) {
getExternalsPkgName(item: string | Record<string, any>) {
if (typeof item === 'string') return item
if (Object.prototype.toString.call(item) === '[object Object]') {
return Object.keys(item)[0]
if (_.isPlainObject(item)) {
return Object.keys(item)
}
return []
}
}

0 comments on commit e5f97e3

Please sign in to comment.