Skip to content

Commit ff7854c

Browse files
authored
Merge pull request #2 from vite-plugin/v1.4.0
V1.4.0
2 parents 7626265 + 4a9915c commit ff7854c

10 files changed

Lines changed: 202 additions & 128 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11

2+
## [2022-07-15] v1.4.0
3+
4+
- dbaff13 docs: v1.4.0
5+
- cfc0da2 feat: add test
6+
- 8baa83b test: v1.4.0
7+
- df034e2 refactor: better logic
8+
- 2c3a23f chore: `files`
9+
210
## [2022-07-02] v1.3.4
311

412
- f16697a docs: v1.3.4

README.md

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,40 +72,57 @@ You can see 👉 [vite-plugin-esmodule](https://github.com/vite-plugin/vite-plug
7272

7373
Optimizer(entries[, options])
7474

75-
##### entries
75+
`entries: Entries`
7676

7777
```ts
78-
export interface Entries {
79-
[moduleId: string]:
80-
| string
81-
| ResultDescription
82-
| ((args: OptimizerArgs) => string | ResultDescription | Promise<string | ResultDescription | void> | void)
83-
| void;
84-
}
85-
8678
export interface OptimizerArgs {
8779
/** Generated file cache directory */
8880
dir: string;
8981
}
9082

9183
export interface ResultDescription {
92-
alias?: { find: string | RegExp; replacement: string };
84+
/**
85+
* This is consistent with the `alias` behavior.
86+
*
87+
* e.g.
88+
* `import fs from 'fs'`
89+
* or
90+
* `import fs from 'node:fs'`
91+
*
92+
* @example
93+
* {
94+
* // This means that both 'fs' and 'node:fs' are supported.
95+
* find: /^(node:)?fs$/,
96+
* replacement: '/project/node_modules/.vite-plugin-optimizer/fs.js',
97+
* }
98+
*/
99+
alias?: {
100+
find: string | RegExp;
101+
/**
102+
* If not explicitly specified, will use the path to the generated file as the default.
103+
*/
104+
replacement?: string;
105+
};
93106
code?: string;
94107
}
108+
109+
export interface Entries {
110+
[moduleId: string]:
111+
| string
112+
| ResultDescription
113+
| ((args: OptimizerArgs) => string | ResultDescription | Promise<string | ResultDescription | void> | void);
114+
}
95115
```
96116

97-
##### options
117+
`options: OptimizerOptions`
98118

99119
```ts
100120
export interface OptimizerOptions {
101121
/**
102122
* @default ".vite-plugin-optimizer"
103123
*/
104124
dir?: string;
105-
/**
106-
* @default ".js"
107-
*/
108-
ext?: string;
125+
resolveId?: ((id: string) => string | Promise<string | void> | void);
109126
}
110127
```
111128

README.zh-CN.md

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,40 +72,57 @@ optimizer({
7272

7373
Optimizer(entries[, options])
7474

75-
##### entries
75+
`entries: Entries`
7676

7777
```ts
78-
export interface Entries {
79-
[moduleId: string]:
80-
| string
81-
| ResultDescription
82-
| ((args: OptimizerArgs) => string | ResultDescription | Promise<string | ResultDescription | void> | void)
83-
| void;
84-
}
85-
8678
export interface OptimizerArgs {
87-
/** 生成缓存文件夹 */
79+
/** 生成缓存文件路径 */
8880
dir: string;
8981
}
9082

9183
export interface ResultDescription {
92-
alias?: { find: string | RegExp; replacement: string };
93-
code: string;
84+
/**
85+
* 这与 `alias` 行为一致。
86+
*
87+
* e.g.
88+
* `import fs from 'fs'`
89+
* or
90+
* `import fs from 'node:fs'`
91+
*
92+
* @example
93+
* {
94+
* // 这种写法表示同时支持 'fs' 和 'node:fs'。
95+
* find: /^(node:)?fs$/,
96+
* replacement: '/project/node_modules/.vite-plugin-optimizer/fs.js',
97+
* }
98+
*/
99+
alias?: {
100+
find: string | RegExp;
101+
/**
102+
* 如果没有显式的指定,将会使用生成文件的路径作为默认值。
103+
*/
104+
replacement?: string;
105+
};
106+
code?: string;
107+
}
108+
109+
export interface Entries {
110+
[moduleId: string]:
111+
| string
112+
| ResultDescription
113+
| ((args: OptimizerArgs) => string | ResultDescription | Promise<string | ResultDescription | void> | void);
94114
}
95115
```
96116

97-
##### options
117+
`options: OptimizerOptions`
98118

99119
```ts
100120
export interface OptimizerOptions {
101121
/**
102122
* @default ".vite-plugin-optimizer"
103123
*/
104124
dir?: string;
105-
/**
106-
* @default ".js"
107-
*/
108-
ext?: string;
125+
resolveId?: ((id: string) => string | Promise<string | void> | void);
109126
}
110127
```
111128

index.d.ts

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Plugin, UserConfig } from 'vite';
1+
import { Plugin } from 'vite';
22

33
declare const optimizer: VitePluginOptimizer;
44
export default optimizer;
@@ -10,20 +10,24 @@ export interface OptimizerArgs {
1010

1111
export interface ResultDescription {
1212
/**
13-
* this option is designed to fully support `alias`
14-
* this is useful if users want to customize alias
15-
* ```js
13+
* This is consistent with the `alias` behavior.
14+
*
15+
* e.g.
16+
* `import fs from 'fs'`
17+
* or
18+
* `import fs from 'node:fs'`
19+
*
20+
* @example
1621
* {
17-
* // e.g. 👉 `/^(node:)?fs$/` from user customization
22+
* // This means that both 'fs' and 'node:fs' are supported.
1823
* find: /^(node:)?fs$/,
1924
* replacement: '/project/node_modules/.vite-plugin-optimizer/fs.js',
2025
* }
21-
* ```
2226
*/
2327
alias?: {
2428
find: string | RegExp;
2529
/**
26-
* If not specified, the path of the generated file is used.
30+
* If not explicitly specified, will use the path to the generated file as the default.
2731
*/
2832
replacement?: string;
2933
};
@@ -34,19 +38,15 @@ export interface Entries {
3438
[moduleId: string]:
3539
| string
3640
| ResultDescription
37-
| ((args: OptimizerArgs) => string | ResultDescription | Promise<string | ResultDescription | void> | void)
38-
| void;
41+
| ((args: OptimizerArgs) => string | ResultDescription | Promise<string | ResultDescription | void> | void);
3942
}
4043

4144
export interface OptimizerOptions {
4245
/**
4346
* @default ".vite-plugin-optimizer"
4447
*/
4548
dir?: string;
46-
/**
47-
* @default ".js"
48-
*/
49-
ext?: string;
49+
resolveId?: ((id: string) => string | Promise<string | void> | void);
5050
}
5151

5252
export interface VitePluginOptimizer {
@@ -58,20 +58,9 @@ export interface VitePluginOptimizer {
5858
export type GenerateRecord = {
5959
alias?: ResultDescription['alias'];
6060
module: string;
61-
// absolute path of file
62-
filepath: string;
61+
// Absolute path of file
62+
filename: string;
6363
};
6464
export interface GenerateModule {
65-
(dir: string, entries: Entries, ext: string): Promise<GenerateRecord[]>;
66-
}
67-
68-
export interface RegisterAlias {
69-
(
70-
config: UserConfig,
71-
records: GenerateRecord[],
72-
): void;
73-
}
74-
75-
export interface RegisterOptimizeDepsExclude {
76-
(config: UserConfig, exclude: string[]): void;
65+
(...args: Parameters<VitePluginOptimizer>): Promise<GenerateRecord[]>;
7766
}

0 commit comments

Comments
 (0)