Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failed builds when using thread-loader #1207

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 37 additions & 21 deletions src/instances.ts
Expand Up @@ -33,22 +33,44 @@ import {
} from './utils';
import { makeWatchRun } from './watch-run';

// Each TypeScript instance is based on the webpack instance (key of the WeakMap)
// Each TypeScript instance is cached based on the webpack instance (key of the WeakMap)
// and also the name that was generated or passed via the options (string key of the
// internal Map)
const instanceCache = new WeakMap<webpack.Compiler, Map<string, TSInstance>>();
const instancesBySolutionBuilderConfigs = new Map<FilePathKey, TSInstance>();
class InstanceCache {
// Some loaders (e.g. thread-loader) will set the _compiler property to undefined.
// We can't use undefined as a WeakMap key as it will throw an error at runtime,
// thus we keep a dummy "marker" object to use as key in those situations.
private marker: webpack.Compiler = {} as webpack.Compiler;
private map: WeakMap<webpack.Compiler, Map<string, TSInstance>>;

constructor() {
this.map = new WeakMap();
}

function addTSInstanceToCache(
key: webpack.Compiler,
instanceName: string,
instance: TSInstance
) {
const instances = instanceCache.get(key) ?? new Map<string, TSInstance>();
instances.set(instanceName, instance);
instanceCache.set(key, instances);
get(key: webpack.Compiler, name: string): TSInstance | undefined {
const compiler = key ?? this.marker;

let instances = this.map.get(compiler);
if (!instances) {
instances = new Map();
this.map.set(compiler, instances);
}

return instances.get(name);
}

set(key: webpack.Compiler, name: string, instance: TSInstance) {
const compiler = key ?? this.marker;

const instances = this.map.get(compiler) ?? new Map<string, TSInstance>();
instances.set(name, instance);
this.map.set(compiler, instances);
}
}

const instanceCache = new InstanceCache();
const instancesBySolutionBuilderConfigs = new Map<FilePathKey, TSInstance>();

/**
* The loader is executed once for each file seen by webpack. However, we need to keep
* a persistent instance of TypeScript that contains all of the files in the program
Expand All @@ -60,13 +82,7 @@ export function getTypeScriptInstance(
loaderOptions: LoaderOptions,
loader: webpack.loader.LoaderContext
): { instance?: TSInstance; error?: WebpackError } {
let instances = instanceCache.get(loader._compiler);
if (!instances) {
instances = new Map();
instanceCache.set(loader._compiler, instances);
}

const existing = instances.get(loaderOptions.instance);
const existing = instanceCache.get(loader._compiler, loaderOptions.instance);
if (existing) {
if (!existing.initialSetupPending) {
ensureProgram(existing);
Expand Down Expand Up @@ -160,7 +176,7 @@ function successfulTypeScriptInstance(
const existing = getExistingSolutionBuilderHost(configFileKey);
if (existing) {
// Reuse the instance if config file for project references is shared.
addTSInstanceToCache(loader._compiler, loaderOptions.instance, existing);
instanceCache.set(loader._compiler, loaderOptions.instance, existing);
return { instance: existing };
}
}
Expand Down Expand Up @@ -246,7 +262,7 @@ function successfulTypeScriptInstance(
filePathKeyMapper,
};

addTSInstanceToCache(
instanceCache.set(
loader._compiler,
loaderOptions.instance,
transpileInstance
Expand Down Expand Up @@ -303,7 +319,7 @@ function successfulTypeScriptInstance(
filePathKeyMapper,
};

addTSInstanceToCache(loader._compiler, loaderOptions.instance, instance);
instanceCache.set(loader._compiler, loaderOptions.instance, instance);
return { instance };
}

Expand Down