Skip to content

Commit

Permalink
fix(doctor): 额外检查配置文件中一些CommonJS专用函数和变量的使用
Browse files Browse the repository at this point in the history
  • Loading branch information
otakustay committed Jan 31, 2022
1 parent 18ec902 commit 341190f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/doctor/src/migration/v4/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from 'node:fs/promises';
import {warn, tip} from '../logger.js';
import {importClientSettings} from '../utils.js';

// eslint-disable-next-line complexity
export default async (cwd: string) => {
const settings = await importClientSettings(cwd);

Expand Down Expand Up @@ -38,6 +39,27 @@ export default async (cwd: string) => {
);
}

if (settingsContent.includes('require(')) {
warn(
'found require() call in you reskript.config.js, which is not allowed by ESM',
'see: https://reskript.vercel.app/docs/migration/v4#项目配置文件'
);
}

if (settingsContent.includes('__dirname' || settingsContent.includes('__filename'))) {
warn(
'found __dirname or __filename in you reskript.config.js, which is not allowed by ESM',
'see: https://reskript.vercel.app/docs/migration/v4#项目配置文件'
);
}

if (settingsContent.includes('require.resolve(')) {
warn(
'found require.resolve() call in you reskript.config.js, which is not allowed by ESM',
'see: https://reskript.vercel.app/docs/migration/v4#项目配置文件'
);
}

if (typeof settings.build?.finalize === 'function') {
tip(
'build.finalize in reskript.config.js can be async now',
Expand Down
10 changes: 10 additions & 0 deletions site/docs/migration/v4.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ V4要求你的NodeJS版本为`^14.18.0 || >= 16.0.0`,即必须是`14.18.0`以
+ );
```

此处,还请注意配置文件中的一些特殊变量和函数调用:

- `__dirname``__filename`在ESM下不可用,请用`fileURLToPath(import.url.meta)`代替`__filename`
- `require`在ESM下不可用,请改用`import`
- `require.resovle`在ESM下不可用,如果你能确定路径,请使用`path.join`计算路径。如果实在需要`require.resolve`,可以尝试使用[resolve](https://www.npmjs.com/package/resolve)包。

:::note
`resolve`包在纯ESM下或许也有一些问题,如果你确实需要,可以通过issue向我们咨询。
:::

### finalize函数异步化

在V4版本中,项目配置中的`build.finalize``devServer.finalize``build.script.finalize`三个函数支持异步返回。以`build.finalize`为例,你可以返回`WebpackConfiguration``Promise<WebpackConfiguration>`,reSKRipt在调用时会妥善处理你的返回值。
Expand Down

0 comments on commit 341190f

Please sign in to comment.