Skip to content

Commit 8ea7b14

Browse files
docs: add basic npm usage and example code (#78)
Co-authored-by: neverland <jait.chen@foxmail.com>
1 parent 7fc3d1b commit 8ea7b14

13 files changed

Lines changed: 225 additions & 2 deletions

File tree

README.md

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
1954
The following usages apply to both Rust and Node.js; the code snippets are written in JavaScript.
2055

2156
To 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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
foo() {
3+
console.log("FOO(App) called");
4+
},
5+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "foo",
3+
"main": "./index.js"
4+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "app",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"build": "webpack -c webpack.config.js"
8+
},
9+
"keywords": [],
10+
"author": "pshu",
11+
"license": "MIT",
12+
"packageManager": "pnpm@10.6.2",
13+
"devDependencies": {
14+
"tsconfig-paths-webpack-plugin": "^4.2.0",
15+
"typescript": "^5.8.3",
16+
"webpack": "^5.99.9",
17+
"webpack-cli": "^6.0.1"
18+
}
19+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { foo } from "foo";
2+
import { callFoo } from "a";
3+
4+
foo();
5+
callFoo();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"paths": {
4+
"foo": ["./mock_foo"],
5+
"a": ["../component/src"]
6+
},
7+
},
8+
"references": [
9+
{
10+
"name": "component",
11+
"path": "../component"
12+
}
13+
]
14+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin");
2+
3+
module.exports = {
4+
entry: {
5+
main: "./src/index.ts",
6+
},
7+
module: {
8+
rules: [
9+
{
10+
test: /\.tsx?$/,
11+
type: "javascript/auto",
12+
},
13+
],
14+
},
15+
optimization: {
16+
minimize: false,
17+
moduleIds: "named",
18+
},
19+
resolve: {
20+
extensions: [".tsx", ".ts", "..."],
21+
plugins: [
22+
new TsconfigPathsPlugin({
23+
logLevel: "info",
24+
extensions: [".tsx", ".ts", ".js"],
25+
baseUrl: "./",
26+
logInfoToStdOut: true,
27+
configFile: "./tsconfig.json",
28+
// uncomment this see different generated files
29+
// references: ["../component/tsconfig.json"],
30+
}),
31+
],
32+
},
33+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
foo() {
3+
console.log("FOO(Component) called");
4+
},
5+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "foo",
3+
"main": "./index.js"
4+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "component",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "src/index.ts",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "pshu",
11+
"license": "MIT",
12+
"packageManager": "pnpm@10.6.2",
13+
"devDependencies": {
14+
"typescript": "^5.8.3"
15+
}
16+
}

0 commit comments

Comments
 (0)