UNPKG

4.1 kBMarkdownView Raw
1# tsconfig-paths-webpack-plugin
2
3[![npm version][version-image]][version-url]
4[![code style: prettier][prettier-image]][prettier-url]
5[![MIT license][license-image]][license-url]
6
7Use this to load modules whose location is specified in the `paths` section of
8`tsconfig.json` when using webpack. This package provides the functionality of
9the [tsconfig-paths](https://www.npmjs.com/package/tsconfig-paths) package but
10as a webpack plug-in.
11
12## How to install
13
14> NOTE: If you are using webpack 4 you need to use version >= 3.0.0 (which is aso backwards compatible with webpack 3).
15
16```
17yarn add --dev tsconfig-paths-webpack-plugin
18```
19
20or
21
22```
23npm install --save-dev tsconfig-paths-webpack-plugin
24```
25
26## How to use
27
28In your webpack config add this:
29
30```js
31const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
32
33module.exports = {
34 ...
35 resolve: {
36 plugins: [new TsconfigPathsPlugin({ /*configFile: "./path/to/tsconfig.json" */ })]
37 }
38 ...
39}
40```
41
42> 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.
43
44## Options
45
46#### configFile _(string) (default='tsconfig.json')_
47
48Allows you to specify where to find the TypeScript configuration file.
49
50You may provide
51
52* just a file name. The plugin will search for the filename using the built-in
53 logic in the `tsconfig-paths` package. The search will start at `cwd`.
54* a relative path to the configuration file. It will be resolved relative to
55 `cwd`.
56* an absolute path to the configuration file.
57
58> The use of `cwd` as default above is not optimal but we've found no better
59> solution yet. If you have a suggestion please file an issue.
60
61#### extensions _(string[]) (default=[".ts", ".tsx"])_
62
63An array of the extensions that will be tried during resolve. Ideally this would be the same as the extensions form 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.
64
65#### baseUrl _(string) (default=undefined)_
66
67This 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 anhother 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.
68
69#### silent _(boolean) (default=false)_
70
71If true, no console.log messages will be emitted. Note that most error messages
72are emitted via webpack which is not affected by this flag.
73
74#### logLevel _(string) (default=warn)_
75
76Can be `info`, `warn` or `error` which limits the log output to the specified
77log level. Beware of the fact that errors are written to stderr and everything
78else is written to stderr (or stdout if logInfoToStdOut is true).
79
80#### colors _(boolean) (default=true)_
81
82If `false`, disables built-in colors in logger messages.
83
84#### logInfoToStdOut _(boolean) (default=false)_
85
86This is important if you read from stdout or stderr and for proper error
87handling. The default value ensures that you can read from stdout e.g. via pipes
88or you use webpack -j to generate json output.
89
90## How to test
91
92To run the provided example:
93
94```
95yarn example
96```
97
98## Prior work
99
100This project uses work done in the
101[awesome-typescript-loader](https://github.com/s-panferov/awesome-typescript-loader).
102
103[version-image]: https://img.shields.io/npm/v/tsconfig-paths-webpack-plugin.svg?style=flat
104[version-url]: https://www.npmjs.com/package/tsconfig-paths-webpack-plugin
105[prettier-image]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat
106[prettier-url]: https://github.com/prettier/prettier
107[license-image]: https://img.shields.io/github/license/jonaskello/tsconfig-paths-webpack-plugin.svg?style=flat
108[license-url]: https://opensource.org/licenses/MIT