Skip to content

Commit 7ef1d45

Browse files
authored
docs(blog): add details on tsconfig.json alias resolution and support for import.defer() (#7777)
1 parent 8a7d191 commit 7ef1d45

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/content/blog/webpack-5-105.mdx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ contributors:
77
Webpack 5.105 brings several improvements, both in code generation and bug fixes.
88

99
- [Two Low-Severity CVEs Fixed in Webpack](#two-low-severity-cves-fixed-in-webpack)
10+
- [`tsconfig.json` Alias Resolution Without Requiring plugins](#tsconfigjson-alias-resolution-without-requiring-plugins)
1011
- [Automatic Module Resolution for Web Workers](#automatic-module-resolution-for-web-workers)
1112
- [Support Import Specifier Guard](#support-import-specifier-guard)
13+
- [Support `import.defer()` for Context Module](#support-importdefer-for-context-module)
1214
- [Preserving Custom `import.meta` Properties](#preserving-custom-importmeta-properties)
1315
- [Better Control of Source Maps in `devtool`](#better-control-of-source-maps-in-devtool)
1416
- [Cleaner ESM output for Node.js and modern builds](#cleaner-esm-output-for-nodejs-and-modern-builds)
@@ -41,6 +43,42 @@ For more information on reporting security vulnerabilities,
4143
see the [Webpack Security Policy](https://github.com/webpack/webpack/security/policy)
4244
.
4345

46+
## `tsconfig.json` Alias Resolution Without Requiring plugins
47+
48+
Module resolution can be configured to read `compilerOptions.baseUrl` and `compilerOptions.paths` from `tsconfig.json` and apply those aliases when resolving imports. As a result, it is no longer necessary to install and configure the external `tsconfig-paths-webpack-plugin` just to make TypeScript path aliases work during bundling.
49+
50+
In the webpack configuration, the `resolve.tsconfig` option is used to specify which tsconfig file should be used as the source (which is also useful in monorepos or setups with multiple tsconfig files):
51+
52+
```js
53+
// webpack.config.js
54+
module.exports = {
55+
resolve: {
56+
tsconfig: "./tsconfig.json", // Can be `false` (disabled), `true` (use the default tsconfig.json), a path to a `tsconfig.json` file, or an object with configFile and references options.
57+
},
58+
};
59+
```
60+
61+
Example of aliases in tsconfig.json:
62+
63+
```json
64+
{
65+
"compilerOptions": {
66+
"baseUrl": ".",
67+
"paths": {
68+
"@/*": ["src/*"]
69+
}
70+
}
71+
}
72+
```
73+
74+
And then in the code:
75+
76+
```ts
77+
import Button from "@/components/Button";
78+
```
79+
80+
This way, `@/components/Button` can be resolved according to the rules defined in `tsconfig.json`, avoiding the need to duplicate aliases in `resolve.alias` and reducing discrepancies between what TypeScript understands and what the bundler resolves.
81+
4482
## Automatic Module Resolution for Web Workers
4583

4684
When importing a package specifically to be used in a Web Worker in Webpack, module resolution can prioritize files like `index.worker.js` over `index.js` when using [conditional exports](https://nodejs.org/api/packages.html#conditional-exports) defined in the `exports` field of `package.json`.
@@ -90,6 +128,19 @@ if (React.useId) {
90128
}
91129
```
92130
131+
## Support `import.defer()` for Context Module
132+
133+
Webpack now supports `import.defer()` even when the import is a context module (that is, when the import path is built dynamically and webpack needs to include a set of possible files). In practice, this makes it much easier to defer the loading of a module selected at runtime, even when that module comes from a dynamic expression pointing to a directory. This enables patterns like “choose between a.js or b.js and load it only when needed”, with webpack generating the appropriate context so that import.defer() works correctly.
134+
135+
{/* eslint-disable */}
136+
137+
```text
138+
const file = Math.random() > 0.5 ? "a.js" : "b.js";
139+
import.defer("./dir/" + file);
140+
```
141+
142+
{/* eslint-enabled */}
143+
93144
## Preserving Custom `import.meta` Properties
94145
95146
Webpack now preserves custom `import.meta` properties during bundling when [`output.module`](/configuration/output/#outputmodule) is enabled. Previously, Webpack would remove any unknown `import.meta` properties (such as `import.meta.customProp`), causing the loss of contextual or tool-specific information in the build.

0 commit comments

Comments
 (0)