@_tc/template-core 0.0.1-bate.16 → 0.0.1-bate.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -100,7 +100,7 @@ function entries() {
100
100
  input[key] = path_1.default.resolve(base, fileHtml);
101
101
  }
102
102
  }
103
- // for (const file of fs.readdirSync(base)) {
103
+ // for (const file of readdirSync(base)) {
104
104
  // if (!file.endsWith(".html")) continue;
105
105
  // const key = path.basename(file, ".html");
106
106
  // input[key] = path.resolve(base, file);
@@ -132,27 +132,61 @@ const watchFiles = async (callback) => {
132
132
  console.log(`watching ${runFEPath}/...`);
133
133
  };
134
134
  exports.watchFiles = watchFiles;
135
- const customOutputPlugin = {
136
- name: "custom-output-paths",
137
- generateBundle(_, bundle) {
138
- for (const [fileName, chunk] of Object.entries(bundle)) {
139
- if (!fileName.endsWith(".html"))
140
- continue;
141
- const baseName = path_1.default.basename(fileName);
142
- const desiredName = `frontend/${baseName}`;
143
- delete bundle[fileName];
144
- chunk.fileName = desiredName;
145
- bundle[desiredName] = chunk;
146
- }
147
- },
148
- };
135
+ /**
136
+ *
137
+ * 1. 在 build 结束后
138
+ * 2. 获取输出目录下的html文件
139
+ * 3. 移动内部的html 至 输出目录的根节点
140
+ */
141
+ function flattenHtmlPlugin() {
142
+ return {
143
+ name: "flatten-html",
144
+ apply: "build",
145
+ closeBundle() {
146
+ const feDir = outDir();
147
+ if (!(0, fs_1.existsSync)(feDir))
148
+ return;
149
+ // 移动文件
150
+ const htmlFiles = glob.sync("**/*.html", { cwd: feDir });
151
+ for (const file of htmlFiles) {
152
+ const src = path_1.default.join(feDir, file);
153
+ const dest = path_1.default.join(feDir, path_1.default.basename(file));
154
+ if (src !== dest) {
155
+ (0, fs_1.copyFileSync)(src, dest);
156
+ (0, fs_1.unlinkSync)(src);
157
+ }
158
+ }
159
+ const parents = [...new Set(htmlFiles.map((f) => path_1.default.dirname(f)))]
160
+ .sort()
161
+ .reverse();
162
+ /**
163
+ *
164
+ * 删除空的目录
165
+ */
166
+ const delEmptyDir = (pathStr) => {
167
+ const currentDir = (0, fs_1.existsSync)(pathStr) ? (0, fs_1.readdirSync)(pathStr) : [1];
168
+ if (!currentDir.length) {
169
+ (0, fs_1.rmdirSync)(pathStr);
170
+ const pa = pathStr.split(path_1.default.sep).slice(0, -1);
171
+ pa.length && delEmptyDir(pa.join(path_1.default.sep));
172
+ }
173
+ };
174
+ for (const dir of parents) {
175
+ if (dir && dir !== ".") {
176
+ const fullPath = path_1.default.join(feDir, dir);
177
+ delEmptyDir(fullPath);
178
+ }
179
+ }
180
+ },
181
+ };
182
+ }
149
183
  const VBuildFE = async (input) => {
150
184
  // 根据入口构建
151
185
  await (0, vite_1.build)({
152
186
  configFile: false,
153
187
  plugins: [
154
188
  (await Promise.resolve().then(() => __importStar(require("@vitejs/plugin-react")))).default(),
155
- customOutputPlugin,
189
+ flattenHtmlPlugin(),
156
190
  ],
157
191
  build: {
158
192
  outDir: outDir(),
@@ -2,7 +2,7 @@ import path from "path";
2
2
  import * as glob from "glob";
3
3
  import { build } from "vite";
4
4
  import { resolve } from "../packages/utils";
5
- import { readFileSync, writeFileSync } from "fs";
5
+ import { copyFileSync, existsSync, readdirSync, readFileSync, rmdirSync, unlinkSync, writeFileSync, } from "fs";
6
6
  const base = process.cwd();
7
7
  // 瞄准输出后的产物路径
8
8
  const framePath = path.resolve(__dirname, "../../");
@@ -59,7 +59,7 @@ export function entries() {
59
59
  input[key] = path.resolve(base, fileHtml);
60
60
  }
61
61
  }
62
- // for (const file of fs.readdirSync(base)) {
62
+ // for (const file of readdirSync(base)) {
63
63
  // if (!file.endsWith(".html")) continue;
64
64
  // const key = path.basename(file, ".html");
65
65
  // input[key] = path.resolve(base, file);
@@ -90,27 +90,61 @@ export const watchFiles = async (callback) => {
90
90
  });
91
91
  console.log(`watching ${runFEPath}/...`);
92
92
  };
93
- const customOutputPlugin = {
94
- name: "custom-output-paths",
95
- generateBundle(_, bundle) {
96
- for (const [fileName, chunk] of Object.entries(bundle)) {
97
- if (!fileName.endsWith(".html"))
98
- continue;
99
- const baseName = path.basename(fileName);
100
- const desiredName = `frontend/${baseName}`;
101
- delete bundle[fileName];
102
- chunk.fileName = desiredName;
103
- bundle[desiredName] = chunk;
104
- }
105
- },
106
- };
93
+ /**
94
+ *
95
+ * 1. 在 build 结束后
96
+ * 2. 获取输出目录下的html文件
97
+ * 3. 移动内部的html 至 输出目录的根节点
98
+ */
99
+ function flattenHtmlPlugin() {
100
+ return {
101
+ name: "flatten-html",
102
+ apply: "build",
103
+ closeBundle() {
104
+ const feDir = outDir();
105
+ if (!existsSync(feDir))
106
+ return;
107
+ // 移动文件
108
+ const htmlFiles = glob.sync("**/*.html", { cwd: feDir });
109
+ for (const file of htmlFiles) {
110
+ const src = path.join(feDir, file);
111
+ const dest = path.join(feDir, path.basename(file));
112
+ if (src !== dest) {
113
+ copyFileSync(src, dest);
114
+ unlinkSync(src);
115
+ }
116
+ }
117
+ const parents = [...new Set(htmlFiles.map((f) => path.dirname(f)))]
118
+ .sort()
119
+ .reverse();
120
+ /**
121
+ *
122
+ * 删除空的目录
123
+ */
124
+ const delEmptyDir = (pathStr) => {
125
+ const currentDir = existsSync(pathStr) ? readdirSync(pathStr) : [1];
126
+ if (!currentDir.length) {
127
+ rmdirSync(pathStr);
128
+ const pa = pathStr.split(path.sep).slice(0, -1);
129
+ pa.length && delEmptyDir(pa.join(path.sep));
130
+ }
131
+ };
132
+ for (const dir of parents) {
133
+ if (dir && dir !== ".") {
134
+ const fullPath = path.join(feDir, dir);
135
+ delEmptyDir(fullPath);
136
+ }
137
+ }
138
+ },
139
+ };
140
+ }
107
141
  export const VBuildFE = async (input) => {
108
142
  // 根据入口构建
109
143
  await build({
110
144
  configFile: false,
111
145
  plugins: [
112
146
  (await import("@vitejs/plugin-react")).default(),
113
- customOutputPlugin,
147
+ flattenHtmlPlugin(),
114
148
  ],
115
149
  build: {
116
150
  outDir: outDir(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@_tc/template-core",
3
- "version": "0.0.1-bate.16",
3
+ "version": "0.0.1-bate.17",
4
4
  "description": "A TypeScript Koa framework template - Monorepo root",
5
5
  "main": "./index.js",
6
6
  "types": "./index.d.ts",