浏览代码

Perf: avoid string comparision

SukkaW 1 年之前
父节点
当前提交
af04018be9
共有 3 个文件被更改,包括 15 次插入16 次删除
  1. 0 7
      Build/build-common.ts
  2. 2 2
      Build/build-public.ts
  3. 13 7
      Build/lib/tree-dir.ts

+ 0 - 7
Build/build-common.ts

@@ -23,13 +23,6 @@ export const buildCommon = task(require.main === module, __filename)(async (span
 
   const paths = await new Fdir()
     .withRelativePaths()
-    // .exclude((dirName, dirPath) => {
-    //   if (dirName === 'domainset' || dirName === 'ip' || dirName === 'non_ip') {
-    //     return false;
-    //   }
-    //   console.error(picocolors.red(`[build-comman] Unknown dir: ${dirPath}`));
-    //   return true;
-    // })
     .filter((filepath, isDirectory) => {
       if (isDirectory) return true;
 

+ 2 - 2
Build/build-public.ts

@@ -3,7 +3,7 @@ import fs from 'node:fs';
 import fsp from 'node:fs/promises';
 
 import { task } from './trace';
-import { treeDir } from './lib/tree-dir';
+import { treeDir, TreeFileType } from './lib/tree-dir';
 import type { TreeType, TreeTypeArray } from './lib/tree-dir';
 
 import { OUTPUT_MOCK_DIR, OUTPUT_MODULES_DIR, PUBLIC_DIR, ROOT_DIR } from './constants/dir';
@@ -101,7 +101,7 @@ function walk(tree: TreeTypeArray) {
   tree.sort(prioritySorter);
   for (let i = 0, len = tree.length; i < len; i++) {
     const entry = tree[i];
-    if (entry.type === 'directory') {
+    if (entry.type === TreeFileType.DIRECTORY) {
       result += html`
         <li class="folder">
           ${entry.name}

+ 13 - 7
Build/lib/tree-dir.ts

@@ -1,20 +1,26 @@
 import fsp from 'node:fs/promises';
 import { sep } from 'node:path';
 
-interface TreeFileType {
-  type: 'file',
+// eslint-disable-next-line sukka/no-export-const-enum -- TODO: fix this in the future
+export const enum TreeFileType {
+  FILE = 1,
+  DIRECTORY = 2
+}
+
+interface TreeFile {
+  type: TreeFileType.FILE,
   name: string,
   path: string
 }
 
 interface TreeDirectoryType {
-  type: 'directory',
+  type: TreeFileType.DIRECTORY,
   name: string,
   path: string,
   children: TreeTypeArray
 }
 
-export type TreeType = TreeDirectoryType | TreeFileType;
+export type TreeType = TreeDirectoryType | TreeFile;
 export type TreeTypeArray = TreeType[];
 
 type VoidOrVoidArray = void | VoidOrVoidArray[];
@@ -35,7 +41,7 @@ export async function treeDir(rootPath: string): Promise<TreeTypeArray> {
 
       if (child.isDirectory()) {
         const newNode: TreeDirectoryType = {
-          type: 'directory',
+          type: TreeFileType.DIRECTORY,
           name: child.name,
           path: childRelativeToRoot,
           children: []
@@ -45,8 +51,8 @@ export async function treeDir(rootPath: string): Promise<TreeTypeArray> {
         continue;
       }
       if (child.isFile()) {
-        const newNode: TreeFileType = {
-          type: 'file',
+        const newNode: TreeFile = {
+          type: TreeFileType.FILE,
           name: child.name,
           path: childRelativeToRoot
         };