Skip to content

Commit 0667756

Browse files
refactor: move to lazy load some scripts
1 parent 89ed671 commit 0667756

File tree

14 files changed

+92
-52
lines changed

14 files changed

+92
-52
lines changed

.config/webpack.config.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = [
3434

3535
output: {
3636
filename: '[name].js',
37-
chunkFilename: '[name].[contenthash].json', // Output filename for dynamically imported chunks
37+
chunkFilename: 'chunks/[name].[contenthash].js', // Output filename for dynamically imported chunks
3838
},
3939

4040
// Externals are only WordPress loaded libraries.

.config/webpack.config.prod.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = [
3434

3535
output: {
3636
filename: '[name].js',
37-
chunkFilename: '[name].[contenthash].json', // Output filename for dynamically imported chunks
37+
chunkFilename: 'chunks/[name].[contenthash].js', // Output filename for dynamically imported chunks
3838
},
3939

4040
// Externals are only WordPress loaded libraries.

src/components/font-family-control/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { select } from '@wordpress/data'
2626

2727
const loadGoogleFonts = async () => {
2828
const { default: fonts } =
29-
await import( /* webpackChunkName: "data/google-fonts" */ './google-fonts.json' )
29+
await import( /* webpackChunkName: "google-fonts" */ './google-fonts.json' )
3030
return fonts.map( font => ( { label: font.family, value: font.family } ) )
3131
}
3232

src/components/guided-modal-tour/index.js

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
guidedTourStates,
1919
} from 'stackable'
2020
import classNames from 'classnames'
21-
import confetti from 'canvas-confetti'
2221

2322
/**
2423
* WordPress dependencies
@@ -208,7 +207,7 @@ const ModalTour = props => {
208207
if ( currentStep === steps.length - 1 ) {
209208
steps[ currentStep ]?.postStep?.( currentStep )
210209
if ( hasConfetti ) {
211-
throwConfetti()
210+
throwConfetti() // Fire and forget - don't wait for confetti
212211
}
213212
onClose()
214213
return
@@ -544,33 +543,42 @@ const ModalTour = props => {
544543
)
545544
}
546545

547-
const throwConfetti = () => {
548-
confetti( {
549-
particleCount: 50,
550-
angle: 60,
551-
spread: 70,
552-
origin: { x: 0 },
553-
zIndex: 100000,
554-
disableForReducedMotion: true,
555-
} )
556-
confetti( {
557-
particleCount: 50,
558-
angle: 120,
559-
spread: 70,
560-
origin: { x: 1 },
561-
zIndex: 100000,
562-
disableForReducedMotion: true,
563-
} )
564-
setTimeout( () => {
565-
confetti( {
546+
const throwConfetti = async () => {
547+
try {
548+
// Dynamically import canvas-confetti only when needed
549+
const confetti = await import( /* webpackChunkName: "canvas-confetti" */ 'canvas-confetti' )
550+
551+
confetti.default( {
552+
particleCount: 50,
553+
angle: 60,
554+
spread: 70,
555+
origin: { x: 0 },
556+
zIndex: 100000,
557+
disableForReducedMotion: true,
558+
} )
559+
confetti.default( {
566560
particleCount: 50,
567-
angle: -90,
568-
spread: 90,
569-
origin: { y: -0.3 },
561+
angle: 120,
562+
spread: 70,
563+
origin: { x: 1 },
570564
zIndex: 100000,
571565
disableForReducedMotion: true,
572566
} )
573-
}, 150 )
567+
setTimeout( () => {
568+
confetti.default( {
569+
particleCount: 50,
570+
angle: -90,
571+
spread: 90,
572+
origin: { y: -0.3 },
573+
zIndex: 100000,
574+
disableForReducedMotion: true,
575+
} )
576+
}, 150 )
577+
} catch ( err ) {
578+
// Silently fail if confetti can't be loaded
579+
// eslint-disable-next-line no-console
580+
console.warn( 'Failed to load confetti:', err )
581+
}
574582
}
575583

576584
const Steps = props => {

src/components/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export { default as TaxonomyControl } from './taxonomy-control'
5757
export { default as Tooltip } from './tooltip'
5858
export { default as BlockStyles } from './block-styles'
5959
export { default as GuidedModalTour } from './guided-modal-tour'
60-
export { default as StyleGuide, StyleGuidePopover } from './style-guide'
60+
export { default as StyleGuidePopover } from './style-guide-popover'
6161

6262
// V2 only Components, for deprecation
6363
export { default as BlockContainer } from './block-container'

src/components/style-guide/popover.js renamed to src/components/style-guide-popover/index.js

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ import { i18n } from 'stackable'
66
/**
77
* Internal dependencies
88
*/
9-
import { StyleGuide } from '~stackable/components'
10-
import { toPng } from 'html-to-image'
119

1210
/**
1311
* WordPress dependencies
1412
*/
1513
import { __, sprintf } from '@wordpress/i18n'
1614
import {
17-
useMemo, useState, useRef,
15+
useMemo, useState, useRef, useEffect,
1816
} from '@wordpress/element'
1917
import { Popover, Button } from '@wordpress/components'
2018
import {
@@ -25,6 +23,8 @@ import { useDesignSystem } from '~stackable/hooks'
2523
const StyleGuidePopover = props => {
2624
const { onClose } = props
2725
const styleGuideRef = useRef( null )
26+
const [ StyleGuide, setStyleGuide ] = useState( null )
27+
const [ isLoading, setIsLoading ] = useState( true )
2828

2929
// On load, look for the .interface-interface-skeleton__content and position over it.
3030
const [ editorStylesWrapper, width, height ] = useMemo( () => {
@@ -34,6 +34,28 @@ const StyleGuidePopover = props => {
3434

3535
const designSystem = useDesignSystem()
3636

37+
// Lazy load the StyleGuide component
38+
useEffect( () => {
39+
const loadStyleGuide = async () => {
40+
try {
41+
// Import the StyleGuide component with explicit chunk naming
42+
const { default: StyleGuideComponent } = await import(
43+
/* webpackChunkName: "style-guide" */
44+
/* webpackMode: "lazy" */
45+
'../../lazy-components/style-guide'
46+
)
47+
setStyleGuide( () => StyleGuideComponent )
48+
} catch ( err ) {
49+
// eslint-disable-next-line no-console
50+
console.error( 'Failed to load StyleGuide component:', err )
51+
} finally {
52+
setIsLoading( false )
53+
}
54+
}
55+
56+
loadStyleGuide()
57+
}, [] )
58+
3759
const style = width && height ? { '--width': `${ width }px`, '--height': `${ height }px` } : {}
3860

3961
return (
@@ -58,7 +80,17 @@ const StyleGuidePopover = props => {
5880
{ __( 'Welcome to your Style Guide! Here you can see a live preview of your design system in action. Any changes you make to your design settings will instantly update here.', i18n ) }
5981
</p>
6082
</div>
61-
<StyleGuide { ...props } designSystem={ designSystem } contentRef={ styleGuideRef } />
83+
{ isLoading ? (
84+
<div className="ugb-style-guide__loading">
85+
{ __( 'Loading style guide…', i18n ) }
86+
</div>
87+
) : StyleGuide ? (
88+
<StyleGuide { ...props } designSystem={ designSystem } contentRef={ styleGuideRef } />
89+
) : (
90+
<div className="ugb-style-guide__error">
91+
{ __( 'Failed to load style guide', i18n ) }
92+
</div>
93+
) }
6294
</Popover>
6395
)
6496
}
@@ -69,20 +101,23 @@ const ExportButton = props => {
69101
const { printRef } = props
70102
const [ isExporting, setIsExporting ] = useState( false )
71103

72-
const handlePrint = () => {
104+
const handlePrint = async () => {
73105
setIsExporting( true )
74-
toPng( printRef.current, { cacheBust: true } )
75-
.then( dataUrl => {
76-
const link = document.createElement( 'a' )
77-
link.download = 'style-guide.png'
78-
link.href = dataUrl
79-
link.click()
80-
setIsExporting( false )
81-
} )
82-
.catch( err => {
83-
alert( sprintf( __( 'Error exporting style guide: %s', i18n ), err.message || err ) ) // eslint-disable-line no-alert
84-
setIsExporting( false )
85-
} )
106+
try {
107+
// Dynamically import html-to-image only when needed
108+
// Webpack will create a separate chunk for this
109+
const htmlToImage = await import( /* webpackChunkName: "html-to-image" */ 'html-to-image' )
110+
111+
const dataUrl = await htmlToImage.toPng( printRef.current, { cacheBust: true } )
112+
const link = document.createElement( 'a' )
113+
link.download = 'style-guide.png'
114+
link.href = dataUrl
115+
link.click()
116+
} catch ( err ) {
117+
alert( sprintf( __( 'Error exporting style guide: %s', i18n ), err.message || err ) ) // eslint-disable-line no-alert
118+
} finally {
119+
setIsExporting( false )
120+
}
86121
}
87122

88123
return (

src/components/style-guide/components.js renamed to src/lazy-components/style-guide/components.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from './utils'
88
import heroBg from './images/hero-bg.webp'
99
import mediaText from './images/media-text.webp'
10-
import { COLOR_SCHEME_PROPERTY_LABELS } from '../color-scheme-preview'
10+
import { COLOR_SCHEME_PROPERTY_LABELS } from '../../components/color-scheme-preview'
1111

1212
import {
1313
i18n, srcUrl, homeUrl,
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)