Skip to content

Commit

Permalink
Merge pull request #4432 from Shopify/jm/patch-watcher-init
Browse files Browse the repository at this point in the history
[Themes] - Theme Dev - Reduce API calls and IO operations triggered from theme reconciliation step
  • Loading branch information
jamesmengo committed Sep 18, 2024
2 parents ec86e77 + 60f895a commit 6669c25
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('hot-reload server', () => {
files: [[assetJsonKey, JSON.stringify(assetJsonValue)]],
})

await setupInMemoryTemplateWatcher(mockTheme, ctx)
await setupInMemoryTemplateWatcher(ctx)
const {event: subscribeEvent, data: hotReloadEvents} = createH3Event('/__hot-reload/subscribe')
const streamPromise = hotReloadHandler(subscribeEvent)
// Next tick to flush the connection:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export function getInMemoryTemplates(ctx: DevServerContext, currentRoute?: strin
* Watchs for file changes and updates in-memory templates, triggering
* HotReload if needed.
*/
export function setupInMemoryTemplateWatcher(theme: Theme, ctx: DevServerContext) {
export function setupInMemoryTemplateWatcher(ctx: DevServerContext) {
const handleFileUpdate = ({fileKey, onContent, onSync}: ThemeFSEventPayload) => {
const extension = extname(fileKey)
const isAsset = fileKey.startsWith('assets/')
Expand Down Expand Up @@ -128,7 +128,6 @@ export function setupInMemoryTemplateWatcher(theme: Theme, ctx: DevServerContext
// is reloaded, we can quickly find what to update in the DOM without
// spending time reading files.
return ctx.localThemeFileSystem.ready().then(async () => {
await ctx.localThemeFileSystem.startWatcher(theme.id.toString(), ctx.session)
const files = [...ctx.localThemeFileSystem.files]
return Promise.allSettled(
files.map(async ([fileKey, file]) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import type {Checksum, Theme} from '@shopify/cli-kit/node/themes/types'
import type {DevServerContext} from './types.js'

export function setupDevServer(theme: Theme, ctx: DevServerContext) {
const watcherPromise = setupInMemoryTemplateWatcher(theme, ctx)
const watcherPromise = setupInMemoryTemplateWatcher(ctx)
const envSetup = ensureThemeEnvironmentSetup(theme, ctx)
const workPromise = Promise.all([watcherPromise, envSetup.workPromise]).then(() => {})
const workPromise = Promise.all([watcherPromise, envSetup.workPromise]).then(() =>
ctx.localThemeFileSystem.startWatcher(theme.id.toString(), ctx.session),
)
const server = createDevelopmentServer(theme, ctx, workPromise)

return {
Expand Down
12 changes: 11 additions & 1 deletion packages/theme/src/cli/utilities/theme-fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ describe('theme-fs', () => {
key: 'assets/new_file.css',
checksum: '1010',
value: 'content',
stats: {size: 7, mtime: expect.any(Number)},
attachment: '',
})
})

Expand All @@ -203,6 +205,8 @@ describe('theme-fs', () => {
key: 'assets/new_image.gif',
checksum: '1010',
attachment,
value: '',
stats: {size: 6, mtime: expect.any(Number)},
})
})

Expand All @@ -216,7 +220,13 @@ describe('theme-fs', () => {

let filesUpdated = false
vi.mocked(writeFile).mockImplementationOnce(() => {
filesUpdated = themeFileSystem.files.get(newAsset.key) === newAsset
expect(themeFileSystem.files.get(newAsset.key)).toEqual({
...newAsset,
attachment: '',
stats: {size: 7, mtime: expect.any(Number)},
})
filesUpdated = true

return Promise.resolve()
})

Expand Down
10 changes: 9 additions & 1 deletion packages/theme/src/cli/utilities/theme-fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,15 @@ export function mountThemeFileSystem(root: string, options?: ThemeFileSystemOpti
await removeThemeFile(root, fileKey)
},
write: async (asset: ThemeAsset) => {
files.set(asset.key, asset)
files.set(
asset.key,
buildThemeAsset({
key: asset.key,
checksum: asset.checksum,
value: asset.value ?? '',
attachment: asset.attachment ?? '',
}),
)
await writeThemeFile(root, asset)
},
read,
Expand Down

0 comments on commit 6669c25

Please sign in to comment.