Skip to content

Commit 379436b

Browse files
committed
refactor: 优化首屏打包体积
- jschardet 仅在导入非 utf-8 时引入 - 各个组件面板改为按需加载
1 parent 5b7dffe commit 379436b

5 files changed

Lines changed: 34 additions & 19 deletions

File tree

src/components/common/HelpDocument.vue

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@
3434
<h3>{{ section.title }}</h3>
3535
</div>
3636
<ul v-if="section.type === 'tips'" class="tip-list">
37-
<li
38-
v-for="item in section.items"
39-
:key="item.term"
40-
>
37+
<li v-for="item in section.items" :key="item.term">
4138
<strong>{{ item.term }}</strong>
4239
- {{ item.description }}
4340
</li>

src/components/layout/SidebarLeft.vue

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,28 @@ import {
150150
TextInitial,
151151
UserRound,
152152
} from '@lucide/vue';
153-
import { ref, onMounted, onUnmounted, watch } from 'vue';
153+
import { defineAsyncComponent, ref, onMounted, onUnmounted, watch } from 'vue';
154154
import { useStyleStore } from '@/stores/styleStore';
155155
import { usePanelResize } from '@/composables/usePanelResize';
156156
import { useUiStore } from '@/stores/uiStore';
157157
import { useWindowStore } from '@/stores/windowStore';
158158
import ChunkListPanel from '@/components/panels/ChunkListPanel.vue';
159-
import IdentityPanel from '@/components/panels/IdentityPanel.vue';
160-
import RuleEditorPanel from '@/components/panels/RuleEditorPanel.vue';
161-
import ExportFormatPanel from '@/components/panels/ExportFormatPanel.vue';
162-
import SearchPanel from '@/components/panels/SearchPanel.vue';
163-
import SettingsPopover from '@/components/popovers/SettingsPopover.vue';
159+
160+
const IdentityPanel = defineAsyncComponent(
161+
() => import('@/components/panels/IdentityPanel.vue'),
162+
);
163+
const RuleEditorPanel = defineAsyncComponent(
164+
() => import('@/components/panels/RuleEditorPanel.vue'),
165+
);
166+
const ExportFormatPanel = defineAsyncComponent(
167+
() => import('@/components/panels/ExportFormatPanel.vue'),
168+
);
169+
const SearchPanel = defineAsyncComponent(
170+
() => import('@/components/panels/SearchPanel.vue'),
171+
);
172+
const SettingsPopover = defineAsyncComponent(
173+
() => import('@/components/popovers/SettingsPopover.vue'),
174+
);
164175
165176
const uiStore = useUiStore();
166177
const windowStore = useWindowStore();

src/components/workspace/MainWorkspace.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@
3939
</template>
4040

4141
<script setup lang="ts">
42-
import { computed } from 'vue';
42+
import { computed, defineAsyncComponent } from 'vue';
4343
import { useWindowStore } from '@/stores/windowStore';
4444
import type { WindowInstance, WindowName } from '@/types/window';
4545
import FileImporter from '@/components/common/FileImporter.vue';
46-
import ChunkView from './ChunkView.vue';
47-
import ExportPreview from './ExportPreview.vue';
4846
import DefaultView from './DefaultView.vue';
4947
48+
const ChunkView = defineAsyncComponent(() => import('./ChunkView.vue'));
49+
const ExportPreview = defineAsyncComponent(() => import('./ExportPreview.vue'));
50+
5051
const windowStore = useWindowStore();
5152
const workspacePanes = computed(() => windowStore.workspacePanes);
5253

src/io/import/textDecoder.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { detect } from 'jschardet';
2-
31
export interface DecodedText {
42
text: string;
53
encoding: string;
@@ -78,7 +76,7 @@ function toBinaryString(bytes: Uint8Array): string {
7876
return chunks.join('');
7977
}
8078

81-
export function decodeText(bytes: Uint8Array): DecodedText {
79+
export async function decodeText(bytes: Uint8Array): Promise<DecodedText> {
8280
// UTF-8 可被浏览器直接检测,优先用这个
8381
if (isValidUtf8(bytes)) {
8482
return {
@@ -97,6 +95,8 @@ export function decodeText(bytes: Uint8Array): DecodedText {
9795
};
9896
}
9997

98+
// 绝大多数文件都是 UTF-8,遇到检测不了的再导入,优化首屏加载
99+
const { detect } = await import('jschardet');
100100
const detected = detect(toBinaryString(bytes));
101101
const detectedName = detected.encoding || '';
102102
const decoderLabel = ENCODING_LABELS[normalizeEncodingName(detectedName)];
@@ -120,5 +120,5 @@ export function decodeText(bytes: Uint8Array): DecodedText {
120120
}
121121

122122
export async function readFileAsText(file: File): Promise<DecodedText> {
123-
return decodeText(new Uint8Array(await file.arrayBuffer()));
123+
return await decodeText(new Uint8Array(await file.arrayBuffer()));
124124
}

src/views/Editor.vue

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,20 @@
2424
</template>
2525

2626
<script setup lang="ts">
27+
import { defineAsyncComponent } from 'vue';
2728
import { useWindowStore } from '@/stores/windowStore';
2829
import { useKeyboardShortcuts } from '@/composables/useKeyboardShortcuts';
29-
import HelpDocument from '@/components/common/HelpDocument.vue';
3030
import TopMenuBar from '@/components/layout/TopMenuBar.vue';
3131
import SidebarLeft from '@/components/layout/SidebarLeft.vue';
3232
import MainWorkspace from '@/components/workspace/MainWorkspace.vue';
3333
import StatusBar from '@/components/layout/StatusBar.vue';
34-
import SidebarRight from '@/components/layout/SidebarRight.vue';
34+
35+
const HelpDocument = defineAsyncComponent(
36+
() => import('@/components/common/HelpDocument.vue'),
37+
);
38+
const SidebarRight = defineAsyncComponent(
39+
() => import('@/components/layout/SidebarRight.vue'),
40+
);
3541
3642
const windowStore = useWindowStore();
3743
useKeyboardShortcuts();

0 commit comments

Comments
 (0)