Skip to content

cli-plugin-typescript: use tsconfig-paths-webpack-plugin #3123

Open
@IlyaSemenov

Description

@IlyaSemenov

What problem does this feature solve?

Currently, cli-plugin-typescript creates tsconfig.json with aliases setup that repeats vue-cli's webpack aliases, namely:

    "baseUrl": ".",
    "paths": {
      "@/*": [
        "src/*"
      ]
    }

If a developer changes or extends this setup, the code will fail to compile with webpack (because webpack ignores tsconfig.json and uses its own resolve.aliases setup).

Changing the defaults is not the edge case, but rather a probable scenario because the default setup is far from ideal for real use: "baseUrl": "." pollutes the root Node.js module namespace with everything that happens to reside in the project root. For example, in my project I switch it to this:

    "baseUrl": "./src",
    "paths": {
      "@/*": [
        "myproject/*"
      ]
    },

To complete that setup, the developer is forced to put alias updates in two files: tsconfig.json and vue.config.js under chainWebpack and keep them synchronised.

Misleading tsconfig.json setup that can't really be changed without changing its webpack counterpart, and having to support two aliases setups is the problem that I propose to solve.


https://github.com/dividab/tsconfig-paths-webpack-plugin allows webpack to pull aliases setup from tsconfig.json and make it the single source of truth. It can be enabled like this in vue.config.js:

module.exports = {
  chainWebpack(config) {
    config.resolve.alias.delete("@")
    config.resolve
      .plugin("tsconfig-paths")
      .use(require("tsconfig-paths-webpack-plugin"))
  },
}

I propose to make this the default setup when cli-plugin-typescript is used. At the very least, this recipe should be added to the documentation in a new chapter that explains that changing tsconfig paths is not possible without updating webpack setup.

What does the proposed API look like?

From the end-user point of view, nothing will change. The defaults will be the same. However, should user modify tsconfig baseUrl/paths, everything will transparently continue to work in webpack.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions