ts-loader icon indicating copy to clipboard operation
ts-loader copied to clipboard

Webpack 2.0 - Cannot read property 'unsafeCache' of undefined

Open donaldpipowitch opened this issue 10 years ago • 16 comments

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

Using [email protected]:

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`
};

donaldpipowitch avatar Sep 20 '16 07:09 donaldpipowitch

+1 heeeelp

josiah1888 avatar Sep 20 '16 19:09 josiah1888

I switched to https://github.com/s-panferov/awesome-typescript-loader. Works fine.

donaldpipowitch avatar Sep 20 '16 19:09 donaldpipowitch

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)

johnnyreilly avatar Sep 20 '16 20:09 johnnyreilly

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: {}
})

r-park avatar Sep 21 '16 16:09 r-park

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.

jeffijoe avatar Sep 25 '16 10:09 jeffijoe

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

johnnyreilly avatar Oct 09 '16 04:10 johnnyreilly

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

nitimmerman avatar Nov 10 '16 14:11 nitimmerman

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)?

schickling avatar Nov 15 '16 15:11 schickling

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.

clydin avatar Nov 15 '16 15:11 clydin

@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 avatar Dec 09 '16 10:12 donaldpipowitch

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

In the ghettoooo

jeffijoe avatar Dec 09 '16 11:12 jeffijoe

Would someone like to submit a PR to resolve this?

johnnyreilly avatar Dec 09 '16 12:12 johnnyreilly

Related docs: https://webpack.js.org/guides/migrating/#loaderoptionsplugin-context

johnnyreilly avatar Dec 09 '16 22:12 johnnyreilly

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

johnnyreilly avatar Dec 10 '16 06:12 johnnyreilly

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.

johnnyreilly avatar Dec 20 '16 05:12 johnnyreilly

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

johnnyreilly avatar Feb 06 '17 07:02 johnnyreilly