Skip to content

Commit

Permalink
fix: 支持全路径的文件引入
Browse files Browse the repository at this point in the history
  • Loading branch information
liu2080019 committed Jul 10, 2024
1 parent a8734bf commit 150163e
Showing 1 changed file with 8 additions and 22 deletions.
30 changes: 8 additions & 22 deletions packages/ice/src/service/preBundleDeps.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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>['config']['alias']) {
const pkgJSON = fse.readJSONSync(pkgPath);
const pkgDir = path.dirname(pkgPath);
Expand All @@ -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;
}
Expand Down

0 comments on commit 150163e

Please sign in to comment.