tsconfig-paths-webpack-plugin icon indicating copy to clipboard operation
tsconfig-paths-webpack-plugin copied to clipboard

Support TypeScript 4.1

Open talohana opened this issue 4 years ago • 10 comments

TypeScript 4.1 adds an option to use path mapping without specifying baseUrl, more about it in TypeScript 4.1 Release Notes

My project is using TypeScript 4.1.2, trying to remove the baseUrl I get the following error

Failed to load tsconfig.json: Missing baseUrl in compilerOptions tsconfig-paths-webpack-plugin: Found no baseUrl in tsconfig.json, not applying tsconfig-paths-webpack-plugin

Would like to submit a PR with a little help :)

talohana avatar Dec 12 '20 12:12 talohana

See also: dividab/tsconfig-paths#143, https://github.com/dividab/tsconfig-paths-webpack-plugin/issues/32#issuecomment-695134747

jakebailey avatar Dec 14 '20 20:12 jakebailey

Since 4.1, I've worked around this with secondary configs that extend and add a baseUrl; after hitting more bugs like #60, I've found that it's easier to drop the plugin in favor of a little code that transforms the paths config into webpack resolve aliases; the output is identical to the tsconfig-paths plugin (when it's working).

jakebailey avatar Jul 15 '21 00:07 jakebailey

@jakebailey

Hi, share please your solution / example repo? I have similar issues

7iomka avatar Dec 30 '21 02:12 7iomka

Sure, the function I wrote to generate aliases is here: https://github.com/microsoft/pyright/blob/169c6089b9914870c43622743c75844657cd9d70/build/lib/webpack.js#L90

resolve.alias gets set to the return value of this function.

As far as I can tell, this is all you need to support tsconfig paths. It verifies enough to make the file ts-check (hence the asserts), but otherwise is all that's really needed to make paths work, no plugin needed.

jakebailey avatar Dec 30 '21 02:12 jakebailey

https://github.com/microsoft/pyright/blob/169c6089b9914870c43622743c75844657cd9d70/build/lib/webpack.js#L90

resolve: {
            extensions: ['.ts', '.js'],
            alias: tsconfigResolveAliases('tsconfig.json'),
        },

That is enough?

7iomka avatar Dec 30 '21 03:12 7iomka

Correct.

jakebailey avatar Dec 30 '21 03:12 jakebailey

Correct.

With storybook it is correct usage?

  webpackFinal: async (config) => {
    // resolve problem with typescript aliases
    config.resolve.alias = {
      ...config.resolve?.alias,
      ...tsconfigResolveAliases('../tsconfig.json'),
    };
  ....
}

7iomka avatar Dec 30 '21 03:12 7iomka

I have no idea what storybook is, but I don't see why not. Can't hurt to try.

jakebailey avatar Dec 30 '21 03:12 jakebailey

I have no idea what storybook is, but I don't see why not. Can't hurt to try.

I have that config

{
  "extends": "./tsconfig/nextjs.json",
  "include": ["."],
  "exclude": ["dist", "build", "node_modules"]
}

With JSONC you parse content of file and compilerOptions required inside. Any workaround for that kind of configs? filepath on the field extends may have similar extends (That is how monorepo configs structured)

7iomka avatar Dec 30 '21 04:12 7iomka

Last I looked, paths is not a config that is carried over from the extended config, so if your project doesn't copy/paste paths around, they aren't working correctly in the first place, and my snippet is still doing the right thing by not following extended paths. The project that uses my little helper (pyright and Pylance) are forced to duplicate them in every tsconfig.

jakebailey avatar Dec 30 '21 04:12 jakebailey