Skip to content

Commit e83c87b

Browse files
committed
dynamic loading and bundle optimization
Signed-off-by: nitro56565 <[email protected]>
1 parent 27b14c1 commit e83c87b

File tree

4 files changed

+100
-71
lines changed

4 files changed

+100
-71
lines changed

package-lock.json

Lines changed: 42 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"rollup-plugin-visualizer": "^5.14.0",
7676
"typescript": "^5.1.6",
7777
"vite": "^4.5.6",
78+
"vite-plugin-chunk-split": "^0.5.0",
7879
"vite-plugin-node-polyfills": "^0.9.0",
7980
"vite-plugin-node-stdlib-browser": "^0.2.1",
8081
"vitest": "^1.6.0"

src/store/store.ts

Lines changed: 44 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ import { create } from "zustand";
22
import { devtools } from "zustand/middleware";
33
import { immer } from "zustand/middleware/immer";
44
import { debounce } from "ts-debounce";
5-
import { ModelManager } from "@accordproject/concerto-core";
6-
import { TemplateMarkInterpreter } from "@accordproject/template-engine";
7-
import { TemplateMarkTransformer } from "@accordproject/markdown-template";
8-
import { transform } from "@accordproject/markdown-transform";
95
import { SAMPLES, Sample } from "../samples";
10-
import * as playground from "../samples/playground";
116
import { compress, decompress } from "../utils/compression/compression";
127

138
interface AppState {
@@ -44,9 +39,22 @@ export interface DecompressedData {
4439
agreementHtml: string;
4540
}
4641

47-
const rebuildDeBounce = debounce(rebuild, 500);
42+
async function loadMarkdownProcessingLibs() {
43+
const [{ ModelManager }, { TemplateMarkInterpreter }, { TemplateMarkTransformer }, { transform }] =
44+
await Promise.all([
45+
import("@accordproject/concerto-core"),
46+
import("@accordproject/template-engine"),
47+
import("@accordproject/markdown-template"),
48+
import("@accordproject/markdown-transform"),
49+
]);
50+
51+
return { ModelManager, TemplateMarkInterpreter, TemplateMarkTransformer, transform };
52+
}
53+
54+
const rebuild = debounce(async (template: string, model: string, dataString: string) => {
55+
const { ModelManager, TemplateMarkInterpreter, TemplateMarkTransformer, transform } =
56+
await loadMarkdownProcessingLibs();
4857

49-
async function rebuild(template: string, model: string, dataString: string) {
5058
const modelManager = new ModelManager({ strict: true });
5159
modelManager.addCTOModel(model, undefined, true);
5260
await modelManager.updateExternalModels();
@@ -67,7 +75,7 @@ async function rebuild(template: string, model: string, dataString: string) {
6775
{},
6876
{ verbose: false }
6977
);
70-
}
78+
}, 500);
7179

7280
const useAppStore = create<AppState>()(
7381
immer(
@@ -83,13 +91,13 @@ const useAppStore = create<AppState>()(
8391
};
8492
});
8593
},
86-
sampleName: playground.NAME,
87-
templateMarkdown: playground.TEMPLATE,
88-
editorValue: playground.TEMPLATE,
89-
modelCto: playground.MODEL,
90-
editorModelCto: playground.MODEL,
91-
data: JSON.stringify(playground.DATA, null, 2),
92-
editorAgreementData: JSON.stringify(playground.DATA, null, 2),
94+
sampleName: "",
95+
templateMarkdown: "",
96+
editorValue: "",
97+
modelCto: "",
98+
editorModelCto: "",
99+
data: "",
100+
editorAgreementData: "",
93101
agreementHtml: "",
94102
error: undefined,
95103
samples: SAMPLES,
@@ -99,79 +107,51 @@ const useAppStore = create<AppState>()(
99107
if (compressedData) {
100108
await get().loadFromLink(compressedData);
101109
} else {
102-
await get().rebuild();
110+
await get().loadSample("default");
103111
}
104112
},
105113
loadSample: async (name: string) => {
106-
const sample = SAMPLES.find((s) => s.NAME === name);
107-
if (sample) {
108-
set(() => ({
109-
sampleName: sample.NAME,
110-
agreementHtml: undefined,
111-
error: undefined,
112-
templateMarkdown: sample.TEMPLATE,
113-
editorValue: sample.TEMPLATE,
114-
modelCto: sample.MODEL,
115-
editorModelCto: sample.MODEL,
116-
data: JSON.stringify(sample.DATA, null, 2),
117-
editorAgreementData: JSON.stringify(sample.DATA, null, 2),
118-
}));
119-
await get().rebuild();
120-
}
114+
const playground = await import("../samples/playground");
115+
const sample = SAMPLES.find((s) => s.NAME === name) || playground;
116+
set(() => ({
117+
sampleName: sample.NAME,
118+
agreementHtml: undefined,
119+
error: undefined,
120+
templateMarkdown: sample.TEMPLATE,
121+
editorValue: sample.TEMPLATE,
122+
modelCto: sample.MODEL,
123+
editorModelCto: sample.MODEL,
124+
data: JSON.stringify(sample.DATA, null, 2),
125+
editorAgreementData: JSON.stringify(sample.DATA, null, 2),
126+
}));
127+
await get().rebuild();
121128
},
122129
rebuild: async () => {
123130
const { templateMarkdown, modelCto, data } = get();
124131
try {
125-
const result = await rebuildDeBounce(templateMarkdown, modelCto, data);
132+
const result = await rebuild(templateMarkdown, modelCto, data);
126133
set(() => ({ agreementHtml: result, error: undefined }));
127134
} catch (error: any) {
128135
set(() => ({ error: formatError(error) }));
129136
}
130137
},
131138
setTemplateMarkdown: async (template: string) => {
132-
const { modelCto, data } = get();
133-
try {
134-
const result = await rebuildDeBounce(template, modelCto, data);
135-
set(() => ({
136-
templateMarkdown: template,
137-
agreementHtml: result,
138-
error: undefined,
139-
}));
140-
} catch (error: any) {
141-
set(() => ({ error: formatError(error) }));
142-
}
139+
set(() => ({ templateMarkdown: template }));
140+
await get().rebuild();
143141
},
144142
setEditorValue: (value: string) => {
145143
set(() => ({ editorValue: value }));
146144
},
147145
setModelCto: async (model: string) => {
148-
const { templateMarkdown, data } = get();
149-
try {
150-
const result = await rebuildDeBounce(templateMarkdown, model, data);
151-
set(() => ({
152-
modelCto: model,
153-
agreementHtml: result,
154-
error: undefined,
155-
}));
156-
} catch (error: any) {
157-
set(() => ({ error: formatError(error) }));
158-
}
146+
set(() => ({ modelCto: model }));
147+
await get().rebuild();
159148
},
160149
setEditorModelCto: (value: string) => {
161150
set(() => ({ editorModelCto: value }));
162151
},
163152
setData: async (data: string) => {
164-
try {
165-
const result = await rebuildDeBounce(
166-
get().templateMarkdown,
167-
get().modelCto,
168-
data
169-
);
170-
set(() => ({ agreementHtml: result, error: undefined }));
171-
} catch (error: any) {
172-
set(() => ({ error: formatError(error) }));
173-
}
174153
set(() => ({ data }));
154+
await get().rebuild();
175155
},
176156
setEditorAgreementData: (value: string) => {
177157
set(() => ({ editorAgreementData: value }));

vite.config.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ import { defineConfig as defineVitestConfig } from "vitest/config";
33
import react from "@vitejs/plugin-react";
44
import nodePolyfills from "vite-plugin-node-stdlib-browser";
55
import { visualizer } from "rollup-plugin-visualizer";
6+
7+
async function getPlugins() {
8+
const chunkSplitPlugin = await import('vite-plugin-chunk-split');
9+
return [chunkSplitPlugin.chunkSplitPlugin()];
10+
}
611
// https://vitejs.dev/config/
712
const viteConfig = defineViteConfig({
813
plugins: [nodePolyfills(), react(), visualizer({
914
emitFile: true,
1015
filename: "stats.html",
11-
})],
16+
}), getPlugins()],
1217
optimizeDeps: {
13-
include: ["immer"],
18+
include: ["immer", "zustand"],
19+
exclude: ["typescript"],
1420
needsInterop: ['@accordproject/template-engine'],
1521
},
1622
});
@@ -23,6 +29,11 @@ const vitestConfig = defineVitestConfig({
2329
environment: "jsdom",
2430
setupFiles: "./src/utils/testing/setup.ts",
2531
},
32+
build: {
33+
rollupOptions: {
34+
external: ["typescript"],
35+
},
36+
},
2637
});
2738

2839
export default mergeConfig(viteConfig, vitestConfig);

0 commit comments

Comments
 (0)