Skip to content

Commit

Permalink
Fix options caching when ts-loader is used in multiple rules (#782)
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 authored and johnnyreilly committed Jun 2, 2018
1 parent 0e31030 commit caebb05
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,21 @@ function getLoaderOptions(loader: Webpack) {
const instanceName =
webpackIndex + '_' + (loaderOptions.instance || 'default');

if (loaderOptionsCache.hasOwnProperty(instanceName)) {
return loaderOptionsCache[instanceName];
if (!loaderOptionsCache.hasOwnProperty(instanceName)) {
loaderOptionsCache[instanceName] = new WeakMap();
}

const cache = loaderOptionsCache[instanceName];

if (cache.has(loaderOptions)) {
return cache.get(loaderOptions) as LoaderOptions;
}

validateLoaderOptions(loaderOptions);

const options = makeLoaderOptions(instanceName, loaderOptions);

loaderOptionsCache[instanceName] = options;
cache.set(loaderOptions, options);

return options;
}
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export interface TSInstance {
}

export interface LoaderOptionsCache {
[name: string]: LoaderOptions;
[name: string]: WeakMap<LoaderOptions, LoaderOptions>;
}

export interface TSInstances {
Expand Down
1 change: 1 addition & 0 deletions test/execution-tests/optionsCaching/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './foo.vue'
7 changes: 7 additions & 0 deletions test/execution-tests/optionsCaching/foo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const React = {}

export default {
render () {
return <div>hello</div>
}
}
6 changes: 6 additions & 0 deletions test/execution-tests/optionsCaching/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"target": "esnext",
"jsx": "react"
}
}
25 changes: 25 additions & 0 deletions test/execution-tests/optionsCaching/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var path = require('path')

module.exports = {
mode: 'development',
entry: path.resolve(__dirname, 'foo.ts'),
output: {
path: __dirname,
filename: 'bundle.js'
},
module: {
rules: [
{ test: /\.ts$/, loader: 'ts-loader', options: {} },
{
test: /\.vue$/,
loader: 'ts-loader',
options: {
appendTsxSuffixTo: [/\.vue$/]
}
}
]
}
}

// for test harness purposes only, you would not need this in a normal project
module.exports.resolveLoader = { alias: { 'ts-loader': require('path').join(__dirname, "../../../index.js") } }

0 comments on commit caebb05

Please sign in to comment.