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 options caching when ts-loader is used in multiple rules #782

Merged
merged 1 commit into from
Jun 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
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") } }