Skip to content

Commit e5d0892

Browse files
authored
fix: 修复创建项目时htmlWebpackPlugin插件报错问题 (#254)
* fix: 修复创建项目时htmlWebpackPlugin插件报错问题 * fix: 删除log语句 * fix: 添加prettierignore忽略文件 * fix: 修改FileTree文件格式
1 parent d9b419d commit e5d0892

File tree

6 files changed

+43
-21
lines changed

6 files changed

+43
-21
lines changed

.prettierignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ dist/
22
node_modules/
33
package-lock.json
44
apps/
5-
5+
packages/core/template
66
# package.json

packages/core/src/models/FileTree.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class FileTree {
176176
*/
177177
async fileRender(src: string, file: FileData, options: any) {
178178
const rendered = ejs.render(file.describe?.fileContent, options, {});
179-
await fs.writeFile(src, rendered,{flag: 'w'});
179+
await fs.writeFile(src, rendered, { flag: "w" });
180180
}
181181

182182
/**

packages/core/src/models/Generator.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ class Generator {
173173
return baseGenerator;
174174
}
175175

176-
// 生成构建工具配置文件
176+
// 借助ast合并构建工具配置
177+
// 主要用于一些插件以及模板可能需要对构建工具做一些配置,该方法就是获取插件或模板相关文件中的配置与模板ast进行合并
178+
// 最后将ast生成代码放到相关文件中插入到根目录
177179
async buildToolGenerate(entryPath: string) {
178180
// 执行 plugin的入口文件,把 config 合并到构建工具原始配置中
179181
const baseEntry = await loadModule(entryPath, path.resolve(__dirname, relativePathToRoot));
@@ -215,7 +217,7 @@ class Generator {
215217
await this.buildToolGenerate(`packages/@plugin/plugin-${pluginName}/index.cjs`);
216218
}
217219

218-
// 单独处理一个框架相关文件
220+
// 单独处理一个框架相关依赖,主要是将框架相关的依赖包插入到pkg内,以及将需要的构建工具配置合并到构建工具模板中
219221
async templateGenerate() {
220222
const templateGenerator = await this.loadBase(
221223
`packages/core/template/template-${this.templateName}/generator/index.cjs`,
@@ -232,16 +234,16 @@ class Generator {
232234
}
233235
}
234236

235-
// 单独处理一个构建工具相关的文件,将构建工具相关的配置插入到package.json中
236-
async webpackGenerator() {
237-
const webpackGenerator = await this.loadBase(
237+
// 单独处理一个构建工具相关的依赖,将构建工具相关的依赖插入到package.json中
238+
async buildGenerator() {
239+
const buildGenerator = await this.loadBase(
238240
`packages/core/template/template-${this.buildToolConfig.buildTool}-script/generator/index.cjs`,
239241
"",
240242
);
241243

242-
if (webpackGenerator && typeof webpackGenerator === "function") {
244+
if (buildGenerator && typeof buildGenerator === "function") {
243245
// 将框架需要的依赖加入到package.json中
244-
await webpackGenerator(this.generatorAPI, this.templateName);
246+
await buildGenerator(this.generatorAPI, this.templateName);
245247
}
246248
}
247249

@@ -260,7 +262,8 @@ class Generator {
260262
// 将框架需要的依赖添加到package.json中,以及如果该框架如果需要添加构建工具配置属性,则借助ast进行添加
261263
await this.templateGenerate();
262264

263-
await this.webpackGenerator();
265+
// 将构建工具需要的依赖添加到package.json中
266+
await this.buildGenerator();
264267

265268
// 根据选择的框架拉取模板进行渲染
266269
const templatePath = join(
@@ -283,6 +286,7 @@ class Generator {
283286
useReactRouter: !!this.preset.plugins["react-router"],
284287
},
285288
};
289+
286290
new FileTree(templatePath).renderTemplates(this.rootDirectory, undefined, options);
287291

288292
// 与构建工具有关的配置全部添加完毕,生成构建工具配置文件

packages/core/src/utils/createApp.ts

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export default async function createAppTest(projectName: string, options: Record
118118
scripts: {},
119119
};
120120

121+
// 拉取webpack执行命令脚本
121122
if (buildTool === "webpack") {
122123
await copyDirectory(
123124
"./template-webpack-script/generator/template",
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<!doctype html>
22
<html lang="en">
3-
<head>
4-
<meta charset="UTF-8" />
5-
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7-
<link rel="icon" href="./favicon.ico" />
8-
<title><%= htmlWebpackPlugin.options.title %></title>
9-
</head>
10-
<body>
11-
<div id="root"></div>
12-
</body>
13-
</html>
3+
4+
<head>
5+
<meta charset="UTF-8" />
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
8+
<link rel="icon" href="./favicon.ico" />
9+
</head>
10+
11+
<body>
12+
<div id="root"></div>
13+
</body>
14+
15+
</html>

pnpm-lock.yaml

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)