next-plugins icon indicating copy to clipboard operation
next-plugins copied to clipboard

Suggestion: Add config option to use tsconfig's paths as webpack.resolve.alias

Open vinipace opened this issue 8 years ago • 2 comments

Suggestion

I frequently find myself having to add the same alias to webpack resolver and tsconfig. My ideia is to provide this as an option in @zeit/next-typescript.

// tsconfig.json
{
   "paths": {
      "~/components/*": ["src/components/*"],
    },
}

// next.config.js
const withTypescript = require('@zeit/next-typescript')
module.exports = withTypescript({
  useTsPathsAsAlias: true,
})

The code above should set config.webpack to function that loads tsconfig json, converts tsconfig.paths to webpack.resolve.alias pattern and assign it. The result should be:

// webpack config
{
  ...,
  resolve: {
    ...,
    alias: {
      ...,
      '~/components': path.resolve('src/components'),
    }
  }
}

If it makes sense, I can write PR adding this feature.

vinipace avatar Jun 30 '18 20:06 vinipace

I this it is probably not needed for this to have first class support here; the one change might be re-exporting the TS path plugin out of awesome-typescript-loader.

I have set up the following in my next config:

const TsConfigPathsPlugin = require('awesome-typescript-loader').TsConfigPathsPlugin;

..... in webpack config:
    config.resolve.plugins = [
      ...config.resolve.plugins || [],
      new TsConfigPathsPlugin(),
    ],

That is literally all that is needed.

codinronan avatar Jul 15 '18 07:07 codinronan

This is supported by default: https://nextjs.org/docs/advanced-features/module-path-aliases

timneutkens avatar Dec 18 '20 08:12 timneutkens