-
-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix failed builds when using thread-loader (#1207)
* Use a dummy compiler reference when the calling webpack compiler is undefined or null * Use a module instead of a class for caching instances * Bump version and update changelog
- Loading branch information
Valerio Pipolo
authored
Nov 4, 2020
1 parent
e90f8ad
commit 3f73e98
Showing
4 changed files
with
51 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import * as webpack from 'webpack'; | ||
import { TSInstance } from './interfaces'; | ||
|
||
// 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. | ||
const marker: webpack.Compiler = {} as webpack.Compiler; | ||
|
||
// 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 cache: WeakMap<webpack.Compiler, Map<string, TSInstance>> = new WeakMap(); | ||
|
||
export function getTSInstanceFromCache( | ||
key: webpack.Compiler, | ||
name: string | ||
): TSInstance | undefined { | ||
const compiler = key ?? marker; | ||
|
||
let instances = cache.get(compiler); | ||
if (!instances) { | ||
instances = new Map(); | ||
cache.set(compiler, instances); | ||
} | ||
|
||
return instances.get(name); | ||
} | ||
|
||
export function setTSInstanceInCache( | ||
key: webpack.Compiler, | ||
name: string, | ||
instance: TSInstance | ||
) { | ||
const compiler = key ?? marker; | ||
|
||
const instances = cache.get(compiler) ?? new Map<string, TSInstance>(); | ||
instances.set(name, instance); | ||
cache.set(compiler, instances); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters