Skip to content

Commit 344464c

Browse files
authored
Merge pull request #483 from dev-five-git/fix-turbo-load-async-issue
Fix turbo loader async issue
2 parents 6dc1203 + ed6bc01 commit 344464c

File tree

3 files changed

+40
-52
lines changed

3 files changed

+40
-52
lines changed

.changeset/shaggy-animals-turn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@devup-ui/next-plugin': patch
3+
---
4+
5+
Fix turbo loader async issue

packages/next-plugin/src/__tests__/loader.test.ts

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { existsSync } from 'node:fs'
2-
import { readFile, writeFile } from 'node:fs/promises'
1+
import { existsSync, readFileSync } from 'node:fs'
2+
import { writeFile } from 'node:fs/promises'
33
import { join, relative } from 'node:path'
44

55
import {
@@ -387,7 +387,7 @@ describe('devupUILoader', () => {
387387
path === 'themeFile'
388388
)
389389
})
390-
vi.mocked(readFile).mockResolvedValue('{}')
390+
vi.mocked(readFileSync).mockReturnValue('{}')
391391
vi.mocked(codeExtract).mockReturnValue({
392392
code: 'code',
393393
css: 'css',
@@ -403,16 +403,14 @@ describe('devupUILoader', () => {
403403
expect(existsSync).toHaveBeenCalledWith('classMapFile')
404404
expect(existsSync).toHaveBeenCalledWith('fileMapFile')
405405
expect(existsSync).toHaveBeenCalledWith('themeFile')
406-
await vi.waitFor(() => {
407-
expect(readFile).toHaveBeenCalledWith('sheetFile', 'utf-8')
408-
expect(readFile).toHaveBeenCalledWith('classMapFile', 'utf-8')
409-
expect(readFile).toHaveBeenCalledWith('fileMapFile', 'utf-8')
410-
expect(readFile).toHaveBeenCalledWith('themeFile', 'utf-8')
411-
expect(importSheet).toHaveBeenCalledWith({})
412-
expect(importClassMap).toHaveBeenCalledWith({})
413-
expect(importFileMap).toHaveBeenCalledWith({})
414-
expect(registerTheme).toHaveBeenCalledWith({})
415-
})
406+
expect(readFileSync).toHaveBeenCalledWith('sheetFile', 'utf-8')
407+
expect(readFileSync).toHaveBeenCalledWith('classMapFile', 'utf-8')
408+
expect(readFileSync).toHaveBeenCalledWith('fileMapFile', 'utf-8')
409+
expect(readFileSync).toHaveBeenCalledWith('themeFile', 'utf-8')
410+
expect(importSheet).toHaveBeenCalledWith({})
411+
expect(importClassMap).toHaveBeenCalledWith({})
412+
expect(importFileMap).toHaveBeenCalledWith({})
413+
expect(registerTheme).toHaveBeenCalledWith({})
416414
})
417415

418416
it('should not read files when they do not exist in watch mode', async () => {
@@ -448,7 +446,7 @@ describe('devupUILoader', () => {
448446
expect(existsSync).toHaveBeenCalledWith('classMapFile')
449447
expect(existsSync).toHaveBeenCalledWith('fileMapFile')
450448
expect(existsSync).toHaveBeenCalledWith('themeFile')
451-
expect(readFile).not.toHaveBeenCalled()
449+
expect(readFileSync).not.toHaveBeenCalled()
452450
})
453451

454452
it('should not write base style when watch is false even if updatedBaseStyle is true', async () => {
@@ -507,7 +505,7 @@ describe('devupUILoader', () => {
507505
addDependency: vi.fn(),
508506
}
509507
vi.mocked(existsSync).mockReturnValue(true)
510-
vi.mocked(readFile).mockResolvedValue('{}')
508+
vi.mocked(readFileSync).mockReturnValue('{}')
511509
vi.mocked(codeExtract).mockImplementation(() => {
512510
throw new Error('error')
513511
})
@@ -545,7 +543,7 @@ describe('devupUILoader', () => {
545543
},
546544
}
547545
vi.mocked(existsSync).mockImplementation((path) => path === 'themeFile')
548-
vi.mocked(readFile).mockResolvedValue(JSON.stringify(themeData))
546+
vi.mocked(readFileSync).mockReturnValue(JSON.stringify(themeData))
549547
vi.mocked(codeExtract).mockReturnValue({
550548
code: 'code',
551549
css: 'css',
@@ -558,14 +556,12 @@ describe('devupUILoader', () => {
558556
devupUILoader.bind(t as any)(Buffer.from('code'), 'index.tsx')
559557

560558
expect(existsSync).toHaveBeenCalledWith('themeFile')
561-
await vi.waitFor(() => {
562-
expect(readFile).toHaveBeenCalledWith('themeFile', 'utf-8')
563-
expect(registerTheme).toHaveBeenCalledWith({
564-
colors: {
565-
primary: '#000',
566-
secondary: '#fff',
567-
},
568-
})
559+
expect(readFileSync).toHaveBeenCalledWith('themeFile', 'utf-8')
560+
expect(registerTheme).toHaveBeenCalledWith({
561+
colors: {
562+
primary: '#000',
563+
secondary: '#fff',
564+
},
569565
})
570566
})
571567

@@ -590,7 +586,9 @@ describe('devupUILoader', () => {
590586
otherProperty: 'value',
591587
}
592588
vi.mocked(existsSync).mockImplementation((path) => path === 'themeFile')
593-
vi.mocked(readFile).mockResolvedValue(JSON.stringify(themeDataWithoutTheme))
589+
vi.mocked(readFileSync).mockReturnValue(
590+
JSON.stringify(themeDataWithoutTheme),
591+
)
594592
vi.mocked(codeExtract).mockReturnValue({
595593
code: 'code',
596594
css: 'css',
@@ -603,10 +601,8 @@ describe('devupUILoader', () => {
603601
devupUILoader.bind(t as any)(Buffer.from('code'), 'index.tsx')
604602

605603
expect(existsSync).toHaveBeenCalledWith('themeFile')
606-
await vi.waitFor(() => {
607-
expect(readFile).toHaveBeenCalledWith('themeFile', 'utf-8')
608-
expect(registerTheme).toHaveBeenCalledWith({})
609-
})
604+
expect(readFileSync).toHaveBeenCalledWith('themeFile', 'utf-8')
605+
expect(registerTheme).toHaveBeenCalledWith({})
610606
})
611607

612608
it('should read themeFile and use empty object when theme property is null', async () => {
@@ -630,7 +626,7 @@ describe('devupUILoader', () => {
630626
theme: null,
631627
}
632628
vi.mocked(existsSync).mockImplementation((path) => path === 'themeFile')
633-
vi.mocked(readFile).mockResolvedValue(
629+
vi.mocked(readFileSync).mockReturnValue(
634630
JSON.stringify(themeDataWithNullTheme),
635631
)
636632
vi.mocked(codeExtract).mockReturnValue({
@@ -645,9 +641,7 @@ describe('devupUILoader', () => {
645641
devupUILoader.bind(t as any)(Buffer.from('code'), 'index.tsx')
646642

647643
expect(existsSync).toHaveBeenCalledWith('themeFile')
648-
await vi.waitFor(() => {
649-
expect(readFile).toHaveBeenCalledWith('themeFile', 'utf-8')
650-
expect(registerTheme).toHaveBeenCalledWith({})
651-
})
644+
expect(readFileSync).toHaveBeenCalledWith('themeFile', 'utf-8')
645+
expect(registerTheme).toHaveBeenCalledWith({})
652646
})
653647
})

packages/next-plugin/src/loader.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { existsSync } from 'node:fs'
2-
import { readFile, writeFile } from 'node:fs/promises'
1+
import { existsSync, readFileSync } from 'node:fs'
2+
import { writeFile } from 'node:fs/promises'
33
import { basename, dirname, join, relative } from 'node:path'
44

55
import {
@@ -56,25 +56,14 @@ const devupUILoader: RawLoaderDefinitionFunction<DevupUILoaderOptions> =
5656
// restart loader issue
5757
// loader should read files when they exist in watch mode
5858
if (existsSync(sheetFile))
59-
promises.push(
60-
readFile(sheetFile, 'utf-8').then(JSON.parse).then(importSheet),
61-
)
59+
importSheet(JSON.parse(readFileSync(sheetFile, 'utf-8')))
6260
if (existsSync(classMapFile))
63-
promises.push(
64-
readFile(classMapFile, 'utf-8')
65-
.then(JSON.parse)
66-
.then(importClassMap),
67-
)
61+
importClassMap(JSON.parse(readFileSync(classMapFile, 'utf-8')))
6862
if (existsSync(fileMapFile))
69-
promises.push(
70-
readFile(fileMapFile, 'utf-8').then(JSON.parse).then(importFileMap),
71-
)
63+
importFileMap(JSON.parse(readFileSync(fileMapFile, 'utf-8')))
7264
if (existsSync(themeFile))
73-
promises.push(
74-
readFile(themeFile, 'utf-8')
75-
.then(JSON.parse)
76-
.then((data) => data?.['theme'] ?? {})
77-
.then(registerTheme),
65+
registerTheme(
66+
JSON.parse(readFileSync(themeFile, 'utf-8'))?.['theme'] ?? {},
7867
)
7968
} else {
8069
importFileMap(defaultFileMap)

0 commit comments

Comments
 (0)