From a8734bff9349861fb3ffa309a75df30581f3f8f4 Mon Sep 17 00:00:00 2001 From: Mingo <391549618@qq.com> Date: Wed, 10 Jul 2024 10:56:18 +0800 Subject: [PATCH 1/4] feat: add package files support --- packages/ice/src/service/preBundleDeps.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/ice/src/service/preBundleDeps.ts b/packages/ice/src/service/preBundleDeps.ts index fe1d8c9f21..6ba2454766 100644 --- a/packages/ice/src/service/preBundleDeps.ts +++ b/packages/ice/src/service/preBundleDeps.ts @@ -185,7 +185,28 @@ 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 - const resolvedEntryPoint = (resolveExports(pkgJSON, entry) || resolveLegacy(pkgJSON) || 'index.js') as string; + // 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 entryPointPath = path.join(pkgDir, resolvedEntryPoint); return entryPointPath; } From 150163ed233cff64e4fb4bd406b68d1a179a9d26 Mon Sep 17 00:00:00 2001 From: Mingo <391549618@qq.com> Date: Wed, 10 Jul 2024 15:20:37 +0800 Subject: [PATCH 2/4] =?UTF-8?q?fix:=20=E6=94=AF=E6=8C=81=E5=85=A8=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E7=9A=84=E6=96=87=E4=BB=B6=E5=BC=95=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/ice/src/service/preBundleDeps.ts | 30 ++++++----------------- 1 file changed, 8 insertions(+), 22 deletions(-) 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; } From 1843ed6426dd7a297db08b1487b3be0d74e3c5a1 Mon Sep 17 00:00:00 2001 From: Mingo <391549618@qq.com> Date: Thu, 11 Jul 2024 15:36:43 +0800 Subject: [PATCH 3/4] fix: compatible legacy function types --- packages/ice/src/service/preBundleDeps.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/ice/src/service/preBundleDeps.ts b/packages/ice/src/service/preBundleDeps.ts index a17fa3b6b8..c7950c6355 100644 --- a/packages/ice/src/service/preBundleDeps.ts +++ b/packages/ice/src/service/preBundleDeps.ts @@ -177,7 +177,7 @@ export async function bundleDeps(options: }); } -function resolveAbsoluteImport (entry: string, pkgDir: string, pkgJSON) { +function resolveAbsoluteImport(entry: string, pkgDir: string, pkgJSON) { const relativePath = entry.replace(`${pkgJSON.name}/`, ''); const absolutePath = path.join(pkgDir, relativePath); return fs.existsSync(absolutePath) ? relativePath : ''; @@ -192,8 +192,8 @@ 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 - const resolvedEntryPoint = (resolveExports(pkgJSON, entry) || resolveAbsoluteImport(entry, pkgDir, pkgJSON) || resolveLegacy(pkgJSON) || 'index.js'); - const entryPointPath = path.join(pkgDir, resolvedEntryPoint); + const resolvedEntryPoint = resolveExports(pkgJSON, entry) || resolveAbsoluteImport(entry, pkgDir, pkgJSON) || resolveLegacy(pkgJSON); + const entryPointPath = path.join(pkgDir, typeof resolvedEntryPoint === 'string' ? resolvedEntryPoint : 'index.js'); return entryPointPath; } From 882f0d1b1e6bc519986fe15638d02b871d112200 Mon Sep 17 00:00:00 2001 From: Mingo <391549618@qq.com> Date: Mon, 15 Jul 2024 21:11:29 +0800 Subject: [PATCH 4/4] =?UTF-8?q?feat:=20=E4=BD=BF=E7=94=A8fse.pathExistsSyn?= =?UTF-8?q?c=E4=BB=A3=E6=9B=BFfs.existsSync?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/ice/src/service/preBundleDeps.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/ice/src/service/preBundleDeps.ts b/packages/ice/src/service/preBundleDeps.ts index c7950c6355..2c7a20cc36 100644 --- a/packages/ice/src/service/preBundleDeps.ts +++ b/packages/ice/src/service/preBundleDeps.ts @@ -1,5 +1,4 @@ import path from 'path'; -import fs from 'fs'; import { createHash } from 'crypto'; import fse from 'fs-extra'; import { esbuild } from '@ice/bundles'; @@ -180,7 +179,7 @@ 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 : ''; + return fse.pathExistsSync(absolutePath) ? relativePath : ''; } export function resolvePackageESEntry(depId: string, pkgPath: string, alias: TaskConfig['config']['alias']) {