Sync resolver

Hello!

I’m making awesome-typescript-loader and I have a small problem to connect webpack and TypeScript. TypeScript works with sync module resolution model, but webpack only introduces async resolver.

In another loader, ts-loader, problem was resolved by copying some code from webpack. I’ve tried to use deasync for a while, but a lot of people reported, that it’s difficult to build deasync on Windows. So I had to copy the code from ts-loader to make sync resolution possible without deasync. Maybe there is some way to embed sync resolver directly into webpack?

Author: Fantashit

3 thoughts on “Sync resolver

  1. I have looked at the source code. The error is thrown here:
    https://github.com/webpack/enhanced-resolve/blob/master/lib/Resolver.js#L25

    Basically, resolveSync calls resolve like you would and then checks whether the callback was called before it returns or not.

    Two things strike me:

    • It implies resolve may or may not call its callback on the same turn, depending on whether it executed async or managed to complete sync. As shown by the Promise API design, this is a bad idea, that makes harder for the caller to write correct code. A consumer of this API cannot know whether the callback will have been invoked when resolve returned or whether it will happen later. Worse: he can test and observe working code in one configuration, and encounter unexpected bugs when the callback is invoked sync in another config!
    • The code goes through resolve, which is supposed to be async. So in fact, depending on the underlying FS, the impl. will always be sync, or always async, but there’s no possible choice. In particular, Node file system supports both async and sync apis, but the Resolver will always use one and never the other (testing seems to show that async is the chosen path).

    Regardless of detailed design choices, resolving modules inside compiler or parser plugins is desirable. If we need to assume that resolve might be async, then those plugin callbacks would need to be async as well.

    In my case, I could live with the assumption that my plugin is only ever used in conjunction with sync FS, but then I would need a working resolveSync.

  2. This feature is still desirable even though nobody at Webpack cared for half a year.
    Please don’t close.

  3. Can someone do something about the bot? I don’t want to leave a dummy comment every 6 months to keep it open.

    It’s labelled P2-Very Important that ought to be enough to keep the bot away.

Comments are closed.