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: 9 additions & 0 deletions .changepacks/changepack_log_config_extends_watch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"changes": {
"packages/webpack-plugin/package.json": "Patch",
"packages/rsbuild-plugin/package.json": "Patch",
"packages/next-plugin/package.json": "Patch"
},
"note": "webpack (and Next.js on webpack) and Rsbuild watch every config `devup.json` extends, as Vite does, and reload the theme when one is edited, added or deleted. Rsbuild did not reload `devup.json` itself in dev before; webpack only watched a `devup.json` that existed when the build started",
"date": "2026-11-15T00:00:00.000Z"
}
2 changes: 1 addition & 1 deletion packages/rsbuild-plugin/src/__tests__/atom-hoist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ compiler.hooks.thisCompilation.call(compilation);
const jsTransform = transforms.find(item => item.options.test instanceof RegExp).handler;
const cssTransform = transforms.find(item => item.options.test === cssDir).handler;
const transform = async (file, code) => {
const result = await jsTransform({ code, resourcePath: join(root, file), addDependency() {} });
const result = await jsTransform({ code, resourcePath: join(root, file), addDependency() {}, addMissingDependency() {} });
return result.code;
};
const loadCss = name => cssTransform({ resourcePath: join(cssDir, name), environment: { name: 'web' } });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ async function extractedCssDirIn(checkout: string) {

codeExtractSpy.mockClear()
await transform.mock.calls[1][1]({
addDependency: mock(),
addMissingDependency: mock(),
code: source,
resourcePath: join(checkout, 'src', 'App.tsx'),
})
Expand Down
83 changes: 83 additions & 0 deletions packages/rsbuild-plugin/src/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ describe('DevupUIRsbuildPlugin', () => {
)
const addDependency = mock()
await transform.mock.calls[1][1]({
addMissingDependency: mock(),
code: "import { Box } from '@devup-ui/react'",
resourcePath: 'src/App.tsx',
addDependency,
Expand Down Expand Up @@ -551,6 +552,8 @@ describe('DevupUIRsbuildPlugin', () => {
)
await expect(
transform.mock.calls[1][1]({
addDependency: mock(),
addMissingDependency: mock(),
code: `import { Box } from '@devup-ui/react'
const App = () => <Box></Box>`,
resourcePath: 'src/App.tsx',
Expand All @@ -561,6 +564,8 @@ const App = () => <Box></Box>`,
})
await expect(
transform.mock.calls[1][1]({
addDependency: mock(),
addMissingDependency: mock(),
code: `import { Box } from '@devup-ui/react'
const App = () => <Box></Box>`,
resourcePath: 'node_modules/@wrong-ui/react/index.tsx',
Expand Down Expand Up @@ -590,6 +595,8 @@ export const a = <Box bg="red" />`
)
await expect(
transform.mock.calls[1][1]({
addDependency: mock(),
addMissingDependency: mock(),
code: source,
resourcePath: 'src/App.tsx',
}),
Expand Down Expand Up @@ -620,6 +627,8 @@ export const a = <div css={{ color: 'red' }} />`
)
await expect(
transform.mock.calls[1][1]({
addDependency: mock(),
addMissingDependency: mock(),
code: source,
resourcePath: 'src/App.tsx',
}),
Expand Down Expand Up @@ -663,6 +672,8 @@ export const a = <div css={{ color: 'red' }} />`
}),
)
const ret = await transform.mock.calls[1][1]({
addDependency: mock(),
addMissingDependency: mock(),
code: `import { Box } from '@devup-ui/react'
const App = () => <Box></Box>`,
resourcePath: 'src/App.tsx',
Expand All @@ -679,6 +690,8 @@ const App = () => <Box></Box>`,
)

const ret1 = await transform.mock.calls[1][1]({
addDependency: mock(),
addMissingDependency: mock(),
code: `import { Box } from '@devup-ui/react'
const App = () => <Box></Box>`,
resourcePath: 'node_modules/@devup-ui/react/index.tsx',
Expand Down Expand Up @@ -730,6 +743,7 @@ const App = () => <Box></Box>`,
codeExtractSpy.mockReturnValue(createCodeExtractResult())
const addDependency = mock()
await transform.mock.calls[1][1]({
addMissingDependency: mock(),
code: "import { Box } from '@devup-ui/react'",
resourcePath: 'src/App.tsx',
addDependency,
Expand All @@ -739,6 +753,71 @@ const App = () => <Box></Box>`,
readFileSyncSpy.mockRestore()
readFileSpy.mockResolvedValue('{}')
})
it('watches the configs devup.json extends and reloads when one changes or is deleted', async () => {
const base = resolve('themes/base.json')
const statSpy = spyOn(fsPromises, 'stat')
try {
const present = new Set(['devup.json', base])
existsSyncSpy.mockImplementation(
(path: string) => present.has(path) || path === resolve('devup.json'),
)
readFileSpy.mockImplementation(async (path: string) =>
path === 'devup.json'
? JSON.stringify({ extends: ['./themes/base.json'] })
: JSON.stringify({
theme: { colors: { default: { primary: '#123456' } } },
}),
)
const stamps = new Map([
[resolve('devup.json'), 1],
[base, 1],
])
statSpy.mockImplementation(async (path: fs.PathLike) => {
const mtimeMs = stamps.get(String(path))
if (mtimeMs === undefined) throw new Error('gone')
return { mtimeMs } as fs.Stats
})
const modifyRspackConfig = mock()
const transform = mock()
await DevupUI({}).setup(
createSetupContext({ modifyRspackConfig, transform }),
)
const config: { plugins?: { apply: (compiler: unknown) => void }[] } = {}
modifyRspackConfig.mock.calls[0][0](config)
const tapPromise = mock()
config.plugins?.[0].apply({ hooks: { watchRun: { tapPromise } } })
const watchRun = tapPromise.mock.calls[0][1] as () => Promise<void>

await watchRun()
registerThemeSpy.mockClear()
await watchRun()
expect(registerThemeSpy).not.toHaveBeenCalled()
stamps.set(base, 2)
await watchRun()
expect(registerThemeSpy).toHaveBeenCalledTimes(1)
present.delete(base)
stamps.delete(base)
await watchRun()
expect(registerThemeSpy).toHaveBeenCalledTimes(2)

// Every module depends on the configs; a deleted one is watched for
// being added again
codeExtractSpy.mockReturnValue(createCodeExtractResult())
const addDependency = mock()
const addMissingDependency = mock()
await transform.mock.calls[1][1]({
code: "import { Box } from '@devup-ui/react'",
resourcePath: 'src/App.tsx',
addDependency,
addMissingDependency,
})
expect(addDependency).toHaveBeenCalledWith(resolve('devup.json'))
expect(addMissingDependency).toHaveBeenCalledWith(base)
} finally {
statSpy.mockRestore()
readFileSpy.mockResolvedValue('{}')
}
})
it('registers the text of the Tailwind CSS with the theme', async () => {
const readFileSyncSpy = spyOn(fs, 'readFileSync').mockReturnValue(
'@theme { --color-brand: #0af; }',
Expand Down Expand Up @@ -1020,6 +1099,7 @@ const App = () => <Box></Box>`,
const userRspack = config.tools.rspack
modifyRsbuildConfig.mock.calls[0][0](config)
await transform.mock.calls[1][1]({
addMissingDependency: mock(),
code: 'source',
resourcePath,
addDependency: mock(),
Expand Down Expand Up @@ -1164,6 +1244,8 @@ const App = () => <Box></Box>`,
// calls[1] is the source transform; atom mode posix-normalizes the
// filename and passes a relative cssDir + import_main_css_in_code=true.
await transform.mock.calls[1][1]({
addDependency: mock(),
addMissingDependency: mock(),
code: `import { Box } from '@devup-ui/react'\nconst A = () => <Box w="1px" />`,
resourcePath: 'src/App.tsx',
})
Expand Down Expand Up @@ -1337,6 +1419,7 @@ const App = () => <Box></Box>`,
// When
for (const name of [first, second]) {
await transform.mock.calls[1][1]({
addMissingDependency: mock(),
code: 'source',
resourcePath: resolve('src', `${name}.tsx`),
addDependency: mock(),
Expand Down
48 changes: 30 additions & 18 deletions packages/rsbuild-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,17 @@ async function writeDataFiles(
>,
compiler: DevupCompiler,
watching: boolean,
): Promise<{ tailwindFiles: string[]; themeError: unknown }> {
): Promise<{
tailwindFiles: string[]
configFiles: string[]
themeError: unknown
}> {
let tailwindFiles: string[] = []
let themeError: unknown
// devup.json and every config it `extends`, read or not
const inputs = new Set([resolve(options.devupFile)])
try {
const config = await loadDevupConfig(options.devupFile)
const config = await loadDevupConfig(options.devupFile, inputs)
const theme = config.theme ?? {}

// Watched even when the theme fails, so fixing the Tailwind CSS reloads it
Expand Down Expand Up @@ -116,7 +122,7 @@ async function writeDataFiles(
compiler.getCss(null, false),
)
}
return { tailwindFiles, themeError }
return { tailwindFiles, configFiles: [...inputs], themeError }
}

/**
Expand Down Expand Up @@ -176,7 +182,7 @@ export const DevupUI = ({

// `rsbuild dev` outlives a broken devup.json; every compilation reports
// it until it loads again
let { tailwindFiles, themeError } = await writeDataFiles(
let { tailwindFiles, configFiles, themeError } = await writeDataFiles(
{
package: libPackage,
cssDir,
Expand All @@ -189,8 +195,9 @@ export const DevupUI = ({
)
if (!extractCss) return

// The project's Tailwind CSS defines what a Tailwind class compiles to:
// read it again when it changes, and compile every module again
// devup.json, the configs it extends and the project's Tailwind CSS
// define the theme: read them again when one changes, is added or is
// deleted, and compile every module again
api.modifyRspackConfig((config) => {
config.plugins ??= []
config.plugins.push({
Expand All @@ -207,7 +214,7 @@ export const DevupUI = ({
async () => {
const current = (
await Promise.all(
tailwindFiles.map((file) =>
[...configFiles, ...tailwindFiles].map((file) =>
stat(file).then(
(stats) => stats.mtimeMs,
() => 0,
Expand All @@ -216,17 +223,18 @@ export const DevupUI = ({
)
).join()
if (stamp !== undefined && stamp !== current) {
;({ tailwindFiles, themeError } = await writeDataFiles(
{
package: libPackage,
cssDir,
devupFile,
distDir,
singleCss,
},
compiler,
true,
))
;({ tailwindFiles, configFiles, themeError } =
await writeDataFiles(
{
package: libPackage,
cssDir,
devupFile,
distDir,
singleCss,
},
compiler,
true,
))
}
stamp = current
},
Expand Down Expand Up @@ -499,9 +507,13 @@ export const DevupUI = ({
code,
resourcePath,
addDependency,
addMissingDependency,
}) => {
if (excludeRegex.test(resourcePath)) return code
for (const file of tailwindFiles) addDependency(file)
for (const file of configFiles)
if (existsSync(file)) addDependency(file)
else addMissingDependency(file)
// The stylesheet import is emitted relative to the importing file, as
// in the next/webpack/vite loaders. An absolute cssDir would bake this
// checkout's path into the emitted module, so byte-identical sources
Expand Down
Loading
Loading