Skip to content

Commit 4b03d17

Browse files
authored
feat: support add custom tempdir for modern.js App (#3706)
* feat: support add custom tempdir for modern.js App * chore: add output.tempDir config * docs: change changeset * feat: add docs
1 parent 9787dff commit 4b03d17

File tree

18 files changed

+377
-0
lines changed

18 files changed

+377
-0
lines changed

.changeset/popular-toes-whisper.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@modern-js/app-tools': patch
3+
'@modern-js/core': patch
4+
---
5+
6+
feat: 添加 output.tempDir 配置,支持单项目多配置同时启动
7+
feat: add output.tempDir configuration, supports launching project with multiple config at the same time

packages/cli/core/src/createCli.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ export const createCli = () => {
109109
},
110110
);
111111

112+
await hooksRunner.beforeConfig();
113+
112114
const extraConfigs = await hooksRunner.config();
113115

114116
const extraSchemas = await hooksRunner.validateSchema();

packages/cli/core/src/manager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616

1717
// eslint-disable-next-line @typescript-eslint/ban-types
1818
const baseHooks: BaseHooks<{}> = {
19+
beforeConfig: createAsyncWorkflow(),
1920
config: createParallelWorkflow(),
2021
resolvedConfig: createAsyncWaterfall(),
2122
validateSchema: createParallelWorkflow(),

packages/cli/core/src/types/hooks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export type BaseHooks<
2222
// eslint-disable-next-line @typescript-eslint/ban-types
2323
ExtendNormalizedConfig extends Record<string, any> = {},
2424
> = {
25+
beforeConfig: AsyncWorkflow<void, void>;
2526
config: ParallelWorkflow<void, UserConfig<Extends>>;
2627
resolvedConfig: AsyncWaterfall<{
2728
resolved: NormalizedConfig<Extends>;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
sidebar_label: tempDir
3+
---
4+
5+
# output.tempDir
6+
7+
- **Type:** `string`
8+
- **Default:** `''`
9+
10+
When developing or building a project, Modern.js generates real Webpack entries and HTML templates, placing them in a temporary directory.
11+
12+
If you want to start a project with multiple configurations at the same time, you can use this configuration to generate files in different temporary directories to avoid interference with each other.
13+
14+
The configuration can be a relative or absolute path, but paths outside the project should be avoided.
15+
16+
Example:
17+
18+
```ts
19+
export default {
20+
output: {
21+
tempDir: path.join('node_modules', '.temp-dir'),
22+
}
23+
}
24+
```

packages/document/main-doc/docs/en/guides/topic-detail/framework-plugin/hook-list.mdx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@ Modern.js exposes three types of plugins: CLI, Runtime, and Server.
99

1010
## CLI
1111

12+
### `beforeConfig`
13+
14+
- Functionality: Running tasks before the config process
15+
- Execution phase: Before the config process
16+
- Hook model: AsyncWorkflow
17+
- Type: `AsyncWorkflow<void, void>`
18+
- Example usage:
19+
20+
```ts
21+
import type { CliPlugin } from '@modern-js/core';
22+
23+
export default (): CliPlugin => ({
24+
setup(api) {
25+
return {
26+
beforeConfig: () => {
27+
// do something
28+
},
29+
};
30+
},
31+
});
32+
```
33+
1234
### `config`
1335

1436
- Functionality: Collect configuration
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
sidebar_label: tempDir
3+
---
4+
5+
# output.tempDir
6+
7+
- **类型:** `string`
8+
- **默认值:** `''`
9+
10+
项目开发或构建时,Modern.js 会生成真实的 Webpack 入口和 HTML 模板,并放在临时目录下。
11+
12+
如果希望由多个配置同时启动某项目,可以通过该配置,将文件生成到不同的临时目录下,避免相互干扰。配置可以是相对路径或绝对路径,但应避免项目外的路径。
13+
14+
示例
15+
16+
```ts
17+
export default {
18+
output: {
19+
tempDir: path.join('node_modules', '.temp-dir'),
20+
}
21+
}
22+
```

packages/document/main-doc/docs/zh/guides/topic-detail/framework-plugin/hook-list.mdx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,28 @@ sidebar_position: 8
88

99
## CLI
1010

11+
### `beforeConfig`
12+
13+
- 功能:运行收集配置前的任务
14+
- 执行阶段:收集配置前
15+
- Hook 模型:AsyncWorkflow
16+
- 类型:`AsyncWorkflow<void, void>`
17+
- 使用示例:
18+
19+
```ts
20+
import type { CliPlugin } from '@modern-js/core';
21+
22+
export default (): CliPlugin => ({
23+
setup(api) {
24+
return {
25+
beforeConfig: () => {
26+
// do something
27+
},
28+
};
29+
},
30+
});
31+
```
32+
1133
### `config`
1234

1335
- 功能:收集配置

packages/solutions/app-tools/src/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,19 @@ export default (
154154
i18n.changeLanguage({ locale });
155155

156156
return {
157+
async beforeConfig() {
158+
const userConfig = api.useConfigContext();
159+
const appContext = api.useAppContext();
160+
if (userConfig.output?.tempDir) {
161+
api.setAppContext({
162+
...appContext,
163+
internalDirectory: path.resolve(
164+
appContext.appDirectory,
165+
userConfig.output.tempDir,
166+
),
167+
});
168+
}
169+
},
157170
async commands({ program }) {
158171
await devCommand(program, api);
159172

packages/solutions/app-tools/src/types/config/output.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface SharedOutputConfig extends BuilderSharedOutputConfig {
1414
ssg?: SSGConfig;
1515
splitRouteChunks?: boolean;
1616
disableNodePolyfill?: boolean;
17+
tempDir?: string;
1718
}
1819

1920
export interface OutputUserConfig

0 commit comments

Comments
 (0)