Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

tsconfig-paths-webpack-plugin

dividab19.3mMIT4.2.0TypeScript support: included

Load modules according to tsconfig paths in webpack.

readme

tsconfig-paths-webpack-plugin

npm version build Coverage Status code style: prettier MIT license

Use this to load modules whose location is specified in the paths section of tsconfig.json when using webpack. This package provides the functionality of the tsconfig-paths package but as a webpack plug-in.

Using this plugin means that you should no longer need to add alias entries in your webpack.config.js which correspond to the paths entries in your tsconfig.json. This plugin creates those alias entries for you, so you don't have to!

How to install

NOTE: If you are using webpack 4 you need to use version >= 3.0.0 (which is aso backwards compatible with webpack 3).

yarn add --dev tsconfig-paths-webpack-plugin

or

npm install --save-dev tsconfig-paths-webpack-plugin

How to use

In your webpack config add this:

const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');

module.exports = {
  ...
  resolve: {
    plugins: [new TsconfigPathsPlugin({/* options: see below */})]
  }
  ...
}

Notice that the plugin is placed in the resolve.plugins section of the configuration. tsconfig-paths-webpack-plugin is a resolve plugin and should only be placed in this part of the configuration. Don't confuse this with the plugins array at the root of the webpack configuration object.

If you're using allowJs in tsconfig.json, or allow other non-TS extensions in webpack, make sure you set extensions option in sync with your webpack config.

Options

configFile (string) (default='tsconfig.json')

Allows you to specify where to find the TypeScript configuration file.

You may provide

  • just a file name. The plugin will search for the filename using the built-in logic in the tsconfig-paths package. The search will start at cwd.
  • a relative path to the configuration file. It will be resolved relative to cwd.
  • an absolute path to the configuration file.

The use of cwd as default above is not optimal but we've found no better solution yet. If you have a suggestion please file an issue.

extensions (string[]) (default=[".ts", ".tsx"])

An array of the extensions that will be tried during resolve. Ideally this would be the same as the extensions from the webpack config but it seems resolver plug-ins does not have access to this information so you need to specify it again for the plugin.

baseUrl (string) (default=undefined)

This allows you to override the baseUrl found in tsconfig.json. The baseUrl specifies from which directory paths should be resolved. So this option enabled you to resolve from another directory than the one where tsconfig.json is located. This can be useful if you want to use webpack with tsc --watch instead of a typescript loader. If this option is undefined then the baseUrl from tsconfig.json will be used.

mainFields ((string | string[])[]) (default=["main"])

An array of the field names that should be considered when resolving packages. Ideally this would be the same as the mainFields from the webpack config but it seems resolver plug-ins does not have access to this infomration so you need to specify it again for the plugin.

silent (boolean) (default=false)

If true, no console.log messages will be emitted. Note that most error messages are emitted via webpack which is not affected by this flag.

logLevel (string) (default=warn)

Can be info, warn or error which limits the log output to the specified log level. Beware of the fact that errors are written to stderr and everything else is written to stderr (or stdout if logInfoToStdOut is true).

colors (boolean) (default=true)

If false, disables built-in colors in logger messages.

logInfoToStdOut (boolean) (default=false)

This is important if you read from stdout or stderr and for proper error handling. The default value ensures that you can read from stdout e.g. via pipes or you use webpack -j to generate json output.

references _(string[]) (default=undefined)

Support for Typescript Project References.

Typescript support

This package has typescript typings included. If your webpack config is using typescript, you can use this syntax to import the default export:

import TsconfigPathsPlugin from "tsconfig-paths-webpack-plugin";

Or you can use this syntax to import the named export:

import { TsconfigPathsPlugin } from "tsconfig-paths-webpack-plugin";

How to test

To run the provided example:

yarn example

How to publish

yarn version --patch
yarn version --minor
yarn version --major

Prior work

This project uses work done in the awesome-typescript-loader.

changelog

Change Log

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

Unreleased

4.2.0 - 2024-11-15

Added

Fixed

4.1.0 - 2023-07-11

Added

  • Remove warning on missing baseUrl, which tsconfig-paths ^4.0.0 doesn't require anymore #105. Thanks to @sverweij for this PR!

  • Add support for multiple tsconfig paths #79. Thanks to @lynch16 for this PR!

Fixed

  • Update tsconfig file name in .npmignore #100. Thanks to @tcd for this PR!

4.0.1 - 2023-03-09

4.0.0 - 2022-07-29

  • chore: update tsconfig-paths dependency #90. Thanks to @MichaelDeBoey for this PR!

  • Update webpack and remove temp types #92. Thanks to @hungtcs for this PR!

  • Eslint (#95)

  • Upgrade ci and jest (#96)

3.5.2 - 2021-11-13

Fixed

  • Fix incorrect resolution with relative imports on project dependencies #85. Thanks to @voliva for this fix!

  • Remove console.log from plugin.ts #72. Thanks to @visualfanatic for this fix!

3.5.1 - 2021-03-16

Fixed

3.5.0 - 2021-03-15

Added

  • Copied webpack 5 types into plugins.temp.types.ts #51. Thanks to @Brian-McBride for this addition!

3.4.1 - 2021-03-13

Fixed

  • Use prepare instead of postinstall for husky.

3.4.0 - 2021-03-13

Added

3.3.0 - 2020-07-29

Added

  • Add readJson fallback to support custom file systems, see #51. Fixes #50. Thanks to @JakeSidSmith for this addition!

3.2.0 - 2018-06-12

Added

3.1.4 - 2018-06-04

Fixed

  • Add undeclared dependency on enhanced-resolve to dependencies, see #19. Thanks to @jbboehr for this fix!

3.1.3 - 2018-05-14

Fixed

  • Fix exports so all types of imports works (commonjs require and typescript import).

3.1.2 - 2018-05-14

Fixed

  • Add types field to package.json.

3.1.1 - 2018-05-14

Fixed

  • Add named typescript export.

3.1.0 - 2018-05-14

Added

  • Typescript typings, see #16.

3.0.4 - 2018-04-17

Fixed

3.0.3 - 2018-03-31

Fixed

3.0.2 - 2018-03-14

Fixed

3.0.1 - 2018-03-13

Fixed

3.0.0 - 2018-03-13

Added

  • Proper support for webpack 4. See #9. Thanks to @Nayni for adding this! (See PR #10)

2.0.0 - 2018-01-13

Changed

  • Performance enhancements:
    • Using async versions of tsconfig-paths package matchPath functions.
    • Using webpack's cached filesystem. Should give better performance.

1.4.0

Changed

  • Only log startup message when logLevel is set to info. See #6.

1.3.1

Added

  • Upgraded to tsconfig-paths 2.7.2.

1.3.0

Added

  • Upgraded to tsconfig-paths 2.7.1.

1.2.0

Added

  • Added option for baseUrl.

1.1.0

Added

  • Added option for extensions.

1.0.0

Changed

  • Log path to tsconfig.json at warn log level so it is logged as default.

0.4.0

Changed

  • Rename plugin class to TsconfigPathsPlugin (lower case s, breaking change)
  • Load config using tsconfig-paths.

Removed

  • Remove compiler option (it was not used)

0.3.0

Changed

  • Use package tsconfig-paths to resolve paths.

0.2.0

Changed

  • Internal refactor

0.1.1

Added

  • Include files.

0.1.0

Added

  • First release.