diff --git a/packages/ice/src/service/preBundleDeps.ts b/packages/ice/src/service/preBundleDeps.ts index 6ba2454766..a17fa3b6b8 100644 --- a/packages/ice/src/service/preBundleDeps.ts +++ b/packages/ice/src/service/preBundleDeps.ts @@ -1,4 +1,5 @@ import path from 'path'; +import fs from 'fs'; import { createHash } from 'crypto'; import fse from 'fs-extra'; import { esbuild } from '@ice/bundles'; @@ -176,6 +177,12 @@ export async function bundleDeps(options: }); } +function resolveAbsoluteImport (entry: string, pkgDir: string, pkgJSON) { + const relativePath = entry.replace(`${pkgJSON.name}/`, ''); + const absolutePath = path.join(pkgDir, relativePath); + return fs.existsSync(absolutePath) ? relativePath : ''; +} + export function resolvePackageESEntry(depId: string, pkgPath: string, alias: TaskConfig['config']['alias']) { const pkgJSON = fse.readJSONSync(pkgPath); const pkgDir = path.dirname(pkgPath); @@ -185,28 +192,7 @@ export function resolvePackageESEntry(depId: string, pkgPath: string, alias: Tas // rax/element -> ./element const entry = aliasKey ? depId.replace(new RegExp(`^${aliasKey}`), '.') : depId; // resolve "exports.import" field or "module" field - // 1.resolve exports - let resolvedEntryPoint = resolveExports(pkgJSON, entry); - // 2.resolve files - if (!resolvedEntryPoint && pkgJSON.files && Array.isArray(pkgJSON.files)) { - const relativeEntry = entry.replace(`${pkgJSON.name}/`, ''); - for (const file of pkgJSON.files) { - const normalizedFile = path.normalize(file); - if (normalizedFile.startsWith(relativeEntry)) { - const extension = path.extname(normalizedFile); - resolvedEntryPoint = `${relativeEntry}${extension}`; - break; - } - } - } - // 3.resolve module - if (!resolvedEntryPoint) { - resolvedEntryPoint = resolveLegacy(pkgJSON); - } - // 4.downgrade index.js - if (!resolvedEntryPoint) { - resolvedEntryPoint = 'index.js' - } + const resolvedEntryPoint = (resolveExports(pkgJSON, entry) || resolveAbsoluteImport(entry, pkgDir, pkgJSON) || resolveLegacy(pkgJSON) || 'index.js'); const entryPointPath = path.join(pkgDir, resolvedEntryPoint); return entryPointPath; }