Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Commit

Permalink
[BundleRefPlugin] resolve imports to files too
Browse files Browse the repository at this point in the history
(cherry picked from commit 2bb6ac1a796c6b8661d9ca6c33bef9e23d7a0764)
  • Loading branch information
spalger committed Jun 16, 2020
1 parent dd442ca commit 1495503
Showing 1 changed file with 47 additions and 18 deletions.
65 changes: 47 additions & 18 deletions packages/kbn-optimizer/src/worker/bundle_refs_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ function hookIntoCompiler(
}

export class BundleRefsPlugin {
private resolvedRequestCache = new Map<string, Promise<string | undefined>>();
private readonly resolvedRefEntryCache = new Map<BundleRef, Promise<string>>();
private readonly resolvedRequestCache = new Map<string, Promise<string | undefined>>();
private readonly ignorePrefix = Path.resolve(this.bundle.contextDir) + Path.sep;

constructor(private readonly bundle: Bundle, public readonly bundleRefs: BundleRefs) {}
constructor(private readonly bundle: Bundle, private readonly bundleRefs: BundleRefs) {}

apply(compiler: webpack.Compiler) {
hookIntoCompiler(compiler, async (context, request) => {
Expand All @@ -83,8 +85,26 @@ export class BundleRefsPlugin {
});
}

private cachedResolveRequest(context: string, request: string) {
const absoluteRequest = Path.resolve(context, request);
private cachedResolveRefEntry(ref: BundleRef) {
const cached = this.resolvedRefEntryCache.get(ref);

if (cached) {
return cached;
}

const absoluteRequest = Path.resolve(ref.contextDir, ref.entry);
const promise = this.cachedResolveRequest(absoluteRequest).then((resolved) => {
if (!resolved) {
throw new Error(`Unable to resolve request [${ref.entry}] relative to [${ref.contextDir}]`);
}

return resolved;
});
this.resolvedRefEntryCache.set(ref, promise);
return promise;
}

private cachedResolveRequest(absoluteRequest: string) {
const cached = this.resolvedRequestCache.get(absoluteRequest);

if (cached) {
Expand All @@ -102,6 +122,7 @@ export class BundleRefsPlugin {
return absoluteRequest;
}

// look for an index file in directories
if (stats?.isDirectory()) {
for (const ext of RESOLVE_EXTENSIONS) {
const indexPath = Path.resolve(absoluteRequest, `index${ext}`);
Expand All @@ -112,6 +133,15 @@ export class BundleRefsPlugin {
}
}

// look for a file with one of the supported extensions
for (const ext of RESOLVE_EXTENSIONS) {
const filePath = `${absoluteRequest}${ext}`;
const fileStats = await safeStat(filePath);
if (fileStats?.isFile()) {
return filePath;
}
}

return;
}

Expand All @@ -132,7 +162,12 @@ export class BundleRefsPlugin {
return;
}

const resolved = await this.cachedResolveRequest(context, request);
const absoluteRequest = Path.resolve(context, request);
if (absoluteRequest.startsWith(this.ignorePrefix)) {
return;
}

const resolved = await this.cachedResolveRequest(absoluteRequest);
if (!resolved) {
return;
}
Expand All @@ -143,23 +178,17 @@ export class BundleRefsPlugin {
return;
}

let matchingRef: BundleRef | undefined;
for (const ref of eligibleRefs) {
const resolvedEntry = await this.cachedResolveRequest(ref.contextDir, ref.entry);
const resolvedEntry = await this.cachedResolveRefEntry(ref);
if (resolved === resolvedEntry) {
matchingRef = ref;
break;
return ref;
}
}

if (!matchingRef) {
const bundleId = Array.from(new Set(eligibleRefs.map((r) => r.bundleId))).join(', ');
const publicDir = eligibleRefs.map((r) => r.entry).join(', ');
throw new Error(
`import [${request}] references a non-public export of the [${bundleId}] bundle and must point to one of the public directories: [${publicDir}]`
);
}

return matchingRef;
const bundleId = Array.from(new Set(eligibleRefs.map((r) => r.bundleId))).join(', ');
const publicDir = eligibleRefs.map((r) => r.entry).join(', ');
throw new Error(
`import [${request}] references a non-public export of the [${bundleId}] bundle and must point to one of the public directories: [${publicDir}]`
);
}
}

0 comments on commit 1495503

Please sign in to comment.