Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Lazy load optional dependencies #5886

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 32 additions & 25 deletions src/nodes/CodeBlockView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
import debounce from 'debounce'
import { NodeViewWrapper, NodeViewContent } from '@tiptap/vue-2'
import { NcActions, NcActionButton, NcActionInput, NcActionLink, NcActionSeparator, NcLoadingIcon } from '@nextcloud/vue'
import mermaid from 'mermaid'
import { v4 as uuidv4 } from 'uuid'

import ViewSplitVertical from 'vue-material-design-icons/ViewSplitVertical.vue'
Expand All @@ -93,7 +92,6 @@ import Check from 'vue-material-design-icons/Check.vue'
import CopyToClipboardMixin from '../mixins/CopyToClipboardMixin.js'

export default {
// eslint-disable-next-line vue/match-component-file-name
name: 'CodeBlockView',
components: {
MarkerIcon,
Expand Down Expand Up @@ -123,6 +121,12 @@ export default {
required: true,
},
},
setup() {
return {
/** The lazy loaded mermaid js module */
mermaid: null,
}
},
data() {
return {
isEditable: false,
Expand Down Expand Up @@ -153,18 +157,34 @@ export default {
return this.supportPreview() ? 'code' : 'preview'
}
},
renderMermaidDebounced() {
return debounce(this.renderMermaid, 250)
},
},
watch: {
'node.textContent'() {
this.renderMermaid()
'node.textContent': {
handler() {
this.renderMermaidDebounced()
},
immediate: true,
},
},
beforeMount() {
this.isEditable = this.editor.isEditable
this.editor.on('update', ({ editor }) => {
this.isEditable = editor.isEditable
})
this.renderMermaidDebounced = debounce(async function() {
},
methods: {
async copyCode() {
await this.copyToClipboard(this.node?.textContent)
},
updateLanguage(event) {
this.updateAttributes({
language: event.target.value,
})
},
async renderMermaid() {
if (!this.supportPreview) {
this.viewMode = 'code'
return
Expand All @@ -177,9 +197,14 @@ export default {
}

try {
await mermaid.parse(textContent)
// lazy load mermaid on first real usage
if (this.mermaid === null) {
this.mermaid = (await import('mermaid')).default
this.mermaid.initialize({ startOnLoad: false })
}
await this.mermaid.parse(textContent)

const { svg } = await mermaid.render(this.targetId, textContent)
const { svg } = await this.mermaid.render(this.targetId, textContent)
const targetElement = document.getElementById(this.targetId)
if (targetElement) {
targetElement.style.display = 'none'
Expand All @@ -191,24 +216,6 @@ export default {
this.viewMode = this.isEditable ? 'side-by-side' : 'code'
}
}
}, 250)

mermaid.initialize({ startOnLoad: false })
this.$nextTick(() => {
this.renderMermaid()
})
},
methods: {
async copyCode() {
await this.copyToClipboard(this.node?.textContent)
},
updateLanguage(event) {
this.updateAttributes({
language: event.target.value,
})
},
renderMermaid() {
this.renderMermaidDebounced()
},
},
}
Expand Down
14 changes: 13 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

import { createAppConfig } from '@nextcloud/vite-config'
import { webpackStats } from 'rollup-plugin-webpack-stats'
import path from 'path'
import { webpackStats } from 'rollup-plugin-webpack-stats';

const ENTRIES_TO_INCLUDE_CSS = ['text', 'public', 'viewer', 'editors']

Expand All @@ -29,6 +29,18 @@ const config = createAppConfig({
plugins: [
webpackStats(),
],
build: {
rollupOptions: {
output: {
manualChunks: (id) => {
// Make the emoji related dependencies a custom chunk to reduce the size of the RichText chunk
if (id.includes('emoji-mart-vue') || id.includes('emoji-datasource')) {
return 'emoji-picker'
}
},
},
},
},
},
})

Expand Down
Loading