From e5f97e38787a10c3f780e1b0b3a2931d5a0ef4fe Mon Sep 17 00:00:00 2001 From: lyfeyaj Date: Fri, 31 Mar 2023 15:00:42 +0800 Subject: [PATCH] =?UTF-8?q?feat(plugin-compiler):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=B9=BD=E7=81=B5=E4=BE=9D=E8=B5=96=E5=92=8C=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=B3=A8=E5=85=A5=E5=8A=9F=E8=83=BD=E5=85=BC=E5=AE=B9=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/parsers/scriptParserPlugin.ts | 37 ++++++++++++------- .../src/plugins/phantomDependencyPlugin.ts | 31 +++++++++++----- 2 files changed, 45 insertions(+), 23 deletions(-) diff --git a/packages/plugin-compiler/src/parsers/scriptParserPlugin.ts b/packages/plugin-compiler/src/parsers/scriptParserPlugin.ts index 704ecfff..0b223287 100644 --- a/packages/plugin-compiler/src/parsers/scriptParserPlugin.ts +++ b/packages/plugin-compiler/src/parsers/scriptParserPlugin.ts @@ -1,4 +1,5 @@ import { + asArray, CompileTypes, EntryBuilderHelpers, FileParserOptions, @@ -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()) + return res + }, new Set()) const result: { api: false | string @@ -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 } @@ -296,6 +305,8 @@ export class ScriptParserPlugin implements Plugin { } }) + logger.debug(`运行时自动注入依赖为:`, result) + return result } diff --git a/packages/plugin-compiler/src/plugins/phantomDependencyPlugin.ts b/packages/plugin-compiler/src/plugins/phantomDependencyPlugin.ts index 11304098..e5dbabc3 100644 --- a/packages/plugin-compiler/src/plugins/phantomDependencyPlugin.ts +++ b/packages/plugin-compiler/src/plugins/phantomDependencyPlugin.ts @@ -1,5 +1,7 @@ import { + asArray, fsExtra as fs, + lodash as _, logger, Plugin, Runner, @@ -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' /** * 检测项目中的幽灵依赖 @@ -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()) } @@ -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 = { @@ -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]] && @@ -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') @@ -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 + ) { transformers.before.push( tsTransformerFactory((node) => { if (!node) return node @@ -200,12 +209,14 @@ export class PhantomDependencyPlugin implements Plugin { /** * 获取 externals 或 consumes 的依赖名 + * NOTE: 有限支持,externals 可能为函数,这种情况下暂时忽略兼容性 * @param item 配置子项 */ - getExternalsPkgName(item) { + getExternalsPkgName(item: string | Record) { 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 [] } }