Skip to content
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
9 changes: 7 additions & 2 deletions packages/next-plugin/src/__tests__/preload.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { writeFileSync } from 'node:fs'
import { realpathSync, writeFileSync } from 'node:fs'
import { readFileSync } from 'node:fs'
import { existsSync } from 'node:fs'
import { join } from 'node:path'
Expand All @@ -19,6 +19,7 @@ vi.mock('node:fs', () => ({
writeFileSync: vi.fn(),
mkdirSync: vi.fn(),
existsSync: vi.fn(),
realpathSync: vi.fn().mockReturnValue('src/App.tsx'),
}))

vi.mock('glob', () => ({
Expand Down Expand Up @@ -68,6 +69,7 @@ describe('preload', () => {
['**/*.tsx', '**/*.ts', '**/*.js', '**/*.mjs'],
{
follow: true,
absolute: true,
},
)
})
Expand All @@ -83,7 +85,10 @@ describe('preload', () => {
it('should process each collected file', () => {
const files = ['src/App.tsx', 'src/components/Button.tsx', '.next/page.tsx']
vi.mocked(globSync).mockReturnValue(files)

vi.mocked(realpathSync)
.mockReturnValueOnce('src/App.tsx')
.mockReturnValueOnce('src/components/Button.tsx')
.mockReturnValueOnce('.next/page.tsx')
preload(/node_modules/, '@devup-ui/react', false, {}, '/output/css')

expect(codeExtract).toHaveBeenCalledTimes(2)
Expand Down
13 changes: 6 additions & 7 deletions packages/next-plugin/src/preload.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFileSync, writeFileSync } from 'node:fs'
import { readFileSync, realpathSync, writeFileSync } from 'node:fs'
import { basename, join, relative } from 'node:path'

import { codeExtract, registerTheme } from '@devup-ui/wasm'
Expand All @@ -11,20 +11,19 @@ export function preload(
theme: object,
cssDir: string,
) {
const projectRoot = process.cwd()

const collected = globSync(['**/*.tsx', '**/*.ts', '**/*.js', '**/*.mjs'], {
follow: true,
absolute: true,
})
registerTheme(theme)
for (const file of collected) {
const filePath = relative(process.cwd(), realpathSync(file))
if (
/\.(test(-d)?|d|spec)\.(tsx|ts|js|mjs)$/.test(file) ||
/^(out|.next)[/\\]/.test(file) ||
excludeRegex.test(file)
/\.(test(-d)?|d|spec)\.(tsx|ts|js|mjs)$/.test(filePath) ||
/^(out|.next)[/\\]/.test(filePath) ||
excludeRegex.test(filePath)
)
continue
const filePath = relative(process.cwd(), join(projectRoot, file))
const { cssFile, css } = codeExtract(
filePath,
readFileSync(filePath, 'utf-8'),
Expand Down