Skip to content

Commit 2f14e44

Browse files
committed
docs (typescript): add guide for webpack config TS
Add a guide on how to use webpack configuration in Typescript `webpack.config.ts`.
1 parent 80e0084 commit 2f14e44

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/content/guides/typescript.mdx

+40
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ contributors:
99
- EugeneHlushko
1010
- chenxsan
1111
- snitin315
12+
- deerawan
1213
---
1314

1415
T> This guide stems from the [_Getting Started_](/guides/getting-started/) guide.
@@ -94,6 +95,45 @@ module.exports = {
9495

9596
This will direct webpack to _enter_ through `./index.ts`, _load_ all `.ts` and `.tsx` files through the `ts-loader`, and _output_ a `bundle.js` file in our current directory.
9697

98+
Regarding webpack configuration, instead of using Javascript for `webpack.config.js`, we could use the Typescript version `webpack.config.ts`. This is possible by installing some packages below.
99+
100+
```bash
101+
npm install --save-dev ts-node @types/webpack
102+
```
103+
104+
`ts-node` is required by webpack to load the configuration in Typescript. `@types/webpack` is an optional to give typing information for webpack config.
105+
106+
Let's see the Typescript version of webpack config file.
107+
108+
**webpack.config.ts**
109+
110+
```ts
111+
import * as path from 'path';
112+
import { Configuration } from 'webpack';
113+
114+
const config: Configuration = {
115+
entry: './src/index.ts',
116+
module: {
117+
rules: [
118+
{
119+
test: /\.tsx?$/,
120+
use: 'ts-loader',
121+
exclude: /node_modules/,
122+
},
123+
],
124+
},
125+
resolve: {
126+
extensions: ['.tsx', '.ts', '.js'],
127+
},
128+
output: {
129+
filename: 'bundle.js',
130+
path: path.resolve(__dirname, 'dist'),
131+
},
132+
};
133+
134+
export default config;
135+
```
136+
97137
Now lets change the import of `lodash` in our `./index.ts` due to the fact that there is no default export present in `lodash` definitions.
98138

99139
**./index.ts**

0 commit comments

Comments
 (0)