@@ -16,6 +16,41 @@ Rust port of [enhanced-resolve].
1616
1717## Usage
1818
19+ ### Basic npm Usage
20+
21+ Use the opinionated ** synchronous** resolver with default options:
22+
23+ ``` js
24+ import * as resolver from " @rspack/resolver" ;
25+
26+ // Use the opinionated sync resolver with default options
27+ const { path: resolvedPath } = resolver .sync (contextPath, ' ./index.js' );
28+
29+ // When resolution fails
30+ const result = resolver .sync (contextPath, ' ./noExist.js' );
31+ // result => { error: "Cannot find module './noExist.js'" }
32+ ```
33+
34+ ### Custom Resolver with Options
35+
36+ You can customize the resolver using ` ResolverFactory ` :
37+
38+ ``` javascript
39+ import { ResolverFactory } from " @rspack/resolver" ;
40+
41+ const resolver = new ResolverFactory (resolveOptions);
42+
43+ // Sync API
44+ const result = resolver .sync (contextPath, ' ./request.js' );
45+ // result => { path: "/the/resolved/path/index.js" }
46+ // or { error: "Cannot find module './request.js'" }
47+
48+ // Async API
49+ const result = await resolver .async (contextPath, ' ./request.js' );
50+ // result => { path: "/the/resolved/path/index.js" }
51+ // or { error: "Cannot find module './request.js'" }
52+ ```
53+
1954The following usages apply to both Rust and Node.js; the code snippets are written in JavaScript.
2055
2156To handle the ` exports ` field in ` package.json ` , ESM and CJS need to be differentiated.
@@ -129,8 +164,47 @@ The options are aligned with [enhanced-resolve].
129164| Field | Default | Description |
130165| ---------------------| ---------| -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
131166| tsconfig | None | TypeScript related config for resolver |
132- | tsconfig.tsconfig | | A relative path to the tsconfig file based on ` cwd ` , or an absolute path of tsconfig file. |
133- | tsconfig.references | ` [] ` | - 'auto': inherits from TypeScript config <br /> - ` string [] ` : relative path (based on directory of the referencing tsconfig file) or absolute path of referenced project's tsconfig |
167+ | tsconfig.configFile | | A relative path to the tsconfig file based on ` cwd ` , or an absolute path of tsconfig file. |
168+ | tsconfig.references | ` [] ` | - 'auto': inherits from TypeScript config <br /> - ` string [] ` : relative path (based on directory of the referencing tsconfig file) or absolute path of referenced project's tsconfig |
169+
170+
171+ In the context of ` @rspack/resolver ` , the ` tsconfig.references ` option helps isolate the ` paths ` configurations of different TypeScript projects.
172+ This ensures that path aliases defined in one TypeScript project do not unintentionally affect the resolving behavior of another.
173+
174+ Given the following [ project] ( https://github.com/web-infra-dev/rspack-resolver/blob/main/examples/tsconfig_references ) structure:
175+
176+ ``` txt
177+ ├── app
178+ │ ├── mock_foo
179+ │ │ ├── index.js
180+ │ │ └── package.json
181+ │ ├── package.json
182+ │ ├── src
183+ │ │ └── index.ts
184+ │ ├── tsconfig.json
185+ │ └── webpack.config.js
186+ └── component
187+ ├── index.js
188+ ├── mock_foo
189+ │ ├── index.js
190+ │ └── package.json
191+ ├── package.json
192+ ├── src
193+ │ └── index.ts
194+ └── tsconfig.json
195+ ```
196+
197+ - Both ` app ` and ` component ` have their own tsconfig.json.
198+ - Each defines a path alias ` foo ` pointing to their respective ` mock_foo ` directory.
199+ - ` app/tsconfig.json ` includes ` component ` as a referenced project.
200+
201+ When configuring ` @rspack/resolver ` with ` app/tsconfig.json ` ,
202+ the resolving result for ` import foo ` in ` component/src/index.ts ` differs based on whether ` tsconfig.references ` is enabled:
203+
204+ | ` tsconfig.references ` | Resolve Result | Behavior |
205+ | -----------------------| -------------------------------| ------------------------------------------------------------------------------------------------------------------------------------------------------------------|
206+ | Disabled | ` app/mock_foo/index.js ` | Uses the root ` tsconfig.json ` ’s path alias for all modules; <br />Same as [ tsconfig-paths-webpack-plugin] ( https://www.npmjs.com/package/tsconfig-paths-webpack-plugin ) |
207+ | Enabled | ` component/mock_foo/index.js ` | Using the referenced project's own ` paths ` config |
134208
135209### Unimplemented Options
136210
0 commit comments