Skip to content

Commit

Permalink
clear the resolution caches whenever a compilation starts
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Jun 16, 2020
1 parent 40ad252 commit dc3df6b
Showing 1 changed file with 33 additions and 35 deletions.
68 changes: 33 additions & 35 deletions packages/kbn-optimizer/src/worker/bundle_refs_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,48 +40,46 @@ interface RequestData {
type Callback<T> = (error?: any, result?: T) => void;
type ModuleFactory = (data: RequestData, callback: Callback<BundleRefModule>) => void;

/**
* Isolate the weired type juggling we have to do to add a hook to the webpack compiler
*/
function hookIntoCompiler(
compiler: webpack.Compiler,
handler: (context: string, request: string) => Promise<BundleRefModule | undefined>
) {
compiler.hooks.compile.tap('BundleRefsPlugin', (compilationParams: any) => {
compilationParams.normalModuleFactory.hooks.factory.tap(
'BundleRefsPlugin/normalModuleFactory/factory',
(wrappedFactory: ModuleFactory): ModuleFactory => (data, callback) => {
const context = data.context;
const dep = data.dependencies[0];

handler(context, dep.request).then(
(result) => {
if (!result) {
wrappedFactory(data, callback);
} else {
callback(undefined, result);
}
},
(error) => callback(error)
);
}
);
});
}

export class BundleRefsPlugin {
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, private readonly bundleRefs: BundleRefs) {}

apply(compiler: webpack.Compiler) {
hookIntoCompiler(compiler, async (context, request) => {
const ref = await this.resolveRef(context, request);
if (ref) {
return new BundleRefModule(ref.exportId);
}
/**
* Called by webpack when the plugin is passed in the webpack config
*/
public apply(compiler: webpack.Compiler) {
// called whenever the compiler starts to compile, passed the params
// that will be used to create the compilation
compiler.hooks.compile.tap('BundleRefsPlugin', (compilationParams: any) => {
// clear caches because a new compilation is starting, meaning that files have
// changed and we should re-run resolutions
this.resolvedRefEntryCache.clear();
this.resolvedRequestCache.clear();

// hook into the creation of NormalModule instances in webpack, if the import
// statement leading to the creation of the module is pointing to a bundleRef
// entry then create a BundleRefModule instead of a NormalModule.
compilationParams.normalModuleFactory.hooks.factory.tap(
'BundleRefsPlugin/normalModuleFactory/factory',
(wrappedFactory: ModuleFactory): ModuleFactory => (data, callback) => {
const context = data.context;
const dep = data.dependencies[0];

this.resolveRef(context, dep.request).then(
(ref) => {
if (!ref) {
wrappedFactory(data, callback);
} else {
callback(undefined, new BundleRefModule(ref.exportId));
}
},
(error) => callback(error)
);
}
);
});
}

Expand Down

0 comments on commit dc3df6b

Please sign in to comment.