UNPKG

5.53 kBMarkdownView Raw
1# tsconfig-paths-webpack-plugin
2
3[![npm version][version-image]][version-url]
4[![build][build-image]][build-url]
5[![code style: prettier][prettier-image]][prettier-url]
6[![MIT license][license-image]][license-url]
7
8Use this to load modules whose location is specified in the `paths` section of
9`tsconfig.json` when using webpack. This package provides the functionality of
10the [tsconfig-paths](https://www.npmjs.com/package/tsconfig-paths) package but
11as a webpack plug-in.
12
13Using this plugin means that you should no longer need to add `alias` entries in
14your `webpack.config.js` which correspond to the `paths` entries in your
15`tsconfig.json`. This plugin creates those `alias` entries for you, so you don't
16have to!
17
18## How to install
19
20> NOTE: If you are using webpack 4 you need to use version >= 3.0.0 (which is aso backwards compatible with webpack 3).
21
22```
23yarn add --dev tsconfig-paths-webpack-plugin
24```
25
26or
27
28```
29npm install --save-dev tsconfig-paths-webpack-plugin
30```
31
32## How to use
33
34In your webpack config add this:
35
36```js
37const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
38
39module.exports = {
40 ...
41 resolve: {
42 plugins: [new TsconfigPathsPlugin({/* options: see below */})]
43 }
44 ...
45}
46```
47
48**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.
49
50If 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.**
51
52## Options
53
54#### configFile _(string) (default='tsconfig.json')_
55
56Allows you to specify where to find the TypeScript configuration file.
57
58You may provide
59
60- just a file name. The plugin will search for the filename using the built-in
61 logic in the `tsconfig-paths` package. The search will start at `cwd`.
62- a relative path to the configuration file. It will be resolved relative to
63 `cwd`.
64- an absolute path to the configuration file.
65
66> The use of `cwd` as default above is not optimal but we've found no better
67> solution yet. If you have a suggestion please file an issue.
68
69#### extensions _(string[]) (default=[".ts", ".tsx"])_
70
71An 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.
72
73#### baseUrl _(string) (default=undefined)_
74
75This 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.
76
77#### mainFields _(string[]) (default=["main"])_
78
79An 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.
80
81#### silent _(boolean) (default=false)_
82
83If true, no console.log messages will be emitted. Note that most error messages
84are emitted via webpack which is not affected by this flag.
85
86#### logLevel _(string) (default=warn)_
87
88Can be `info`, `warn` or `error` which limits the log output to the specified
89log level. Beware of the fact that errors are written to stderr and everything
90else is written to stderr (or stdout if logInfoToStdOut is true).
91
92#### colors _(boolean) (default=true)_
93
94If `false`, disables built-in colors in logger messages.
95
96#### logInfoToStdOut _(boolean) (default=false)_
97
98This is important if you read from stdout or stderr and for proper error
99handling. The default value ensures that you can read from stdout e.g. via pipes
100or you use webpack -j to generate json output.
101
102## Typescript support
103
104This package has typescript typings included. If your webpack config is using typescript, you can use this syntax to import the default export:
105
106```ts
107import TsconfigPathsPlugin from "tsconfig-paths-webpack-plugin";
108```
109
110Or you can use this syntax to import the named export:
111
112```ts
113import { TsconfigPathsPlugin } from "tsconfig-paths-webpack-plugin";
114```
115
116## How to test
117
118To run the provided example:
119
120```
121yarn example
122```
123
124## How to publish
125
126```
127yarn version --patch
128yarn version --minor
129yarn version --major
130```
131
132## Prior work
133
134This project uses work done in the
135[awesome-typescript-loader](https://github.com/s-panferov/awesome-typescript-loader).
136
137[version-image]: https://img.shields.io/npm/v/tsconfig-paths-webpack-plugin.svg?style=flat
138[version-url]: https://www.npmjs.com/package/tsconfig-paths-webpack-plugin
139[build-image]: https://github.com/dividab/tsconfig-paths-webpack-plugin/workflows/Build/badge.svg
140[build-url]: https://github.com/dividab/tsconfig-paths-webpack-plugin/actions?query=workflow%3ABuild+branch%3Amaster
141[prettier-image]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat
142[prettier-url]: https://github.com/prettier/prettier
143[license-image]: https://img.shields.io/github/license/jonaskello/tsconfig-paths-webpack-plugin.svg?style=flat
144[license-url]: https://opensource.org/licenses/MIT