TypeStrong/ts-loader

Webpack 2.0 - Cannot read property 'unsafeCache' of undefined

donaldpipowitch opened this issue ยท 16 comments

Please read right to the bottom of this thread for an approach that seems to work.

Using webpack@2.1.0-beta.23:

ERROR in ./src/index.tsx
Module build failed: TypeError: Cannot read property 'unsafeCache' of undefined
    at makeResolver (/Users/foo/node_modules/ts-loader/resolver.js:35:57)
    at ensureTypeScriptInstance (/Users/foo/node_modules/ts-loader/index.js:207:20)
    at Object.loader (/Users/foo/node_modules/ts-loader/index.js:387:14)

Used as:

export const tsLoader = {
  test: /\.ts(x?)$/,
  loader: `ts-loader?silent=true`
};

+1 heeeelp

I'm afraid ts-loader is untested with WebPack 2 to my knowledge. awesome-typescript-loader is probably the best choice if you're after WebPack 2.0 support now. Myself I haven't used 2.0 as it's still in beta (and 1 handles my use cases)

For me, this only happens when I use the LoaderOptionsPlugin with an options key:

new LoaderOptionsPlugin({
  debug: false,
  options: { ... }
})

Remove options and it works fine. Fails even if options is an empty object literal:

new LoaderOptionsPlugin({
  debug: false,
  options: {}
})

I tried awesome-typescript-loader and couldn't get it working so I decided to take a stab at figuring out how to fix this.

@r-park @johnnyreilly @donaldpipowitch @josiah1888 by console.loging in the place the error occured, I found that the options is actually the options from the LoaderOptionsPlugin. By doing this:

new LoaderOptionsPlugin({
  debug: false,
  options: {
    resolve: {}
  }
})

Everything runs again.

Thanks @jeffijoe - we should probably include this in the docs somewhere to improve discovery. Well done for finding out!

@jeffijoe: Thanks! That did the trick for me also!

Also switched to awesome-typescript-loader. Any update when this will work again out-of-the-box without the workaround (which broke SASS for me)?

The workaround isn't really a workaround as the resolver is not properly configured if you pass in an empty resolve object.

The issue is that when LoaderOptionsPlugin is used, the loader doesn't get the full webpack config but instead only what is contained within the object provided to LoaderOptionsPlugin.

@jeffijoe Your workaround seems to work for me now without visible problems. But it would be great if the workaround wouldn't be needed.

@donaldpipowitch Me too, this is how I feel about it:

In the ghettoooo

Would someone like to submit a PR to resolve this?

I've documented the workaround in the readme here: https://github.com/TypeStrong/ts-loader#webpack

We now have an example in our examples section which demonstrates using ts-loader with webpack 2. I'm still not sure if the unsafe cache thing is actually a ts-loader issue; it feels like required configuration. FWIW in our example we supply all the webpack config options to the loader options plugin.

The workaround is now documented on the readme and since this is for the Loader Options Plugin which is due to be short-lived I'm going to close this