Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: restart dev server when tsconfig and tailwind config changes #4947

Merged
merged 14 commits into from
Oct 12, 2022
Prev Previous commit
Next Next commit
Refactored watchTargets to watchFiles
JuanM04 committed Oct 3, 2022
commit 0524996c37d55234b0f9a8524d7a9a7259fdf47f
8 changes: 2 additions & 6 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
@@ -870,10 +870,6 @@ export interface InjectedRoute {
pattern: string;
entryPoint: string;
}
export interface InjectedWatchTarget {
path: string;
type: 'relative' | 'absolute';
}
export interface AstroConfig extends z.output<typeof AstroConfigSchema> {
// Public:
// This is a more detailed type than zod validation gives us.
@@ -894,7 +890,7 @@ export interface AstroSettings {
}[];
tsConfig: TsConfigJson | undefined;
tsConfigPath: string | undefined;
watchTargets: InjectedWatchTarget[];
watchFiles: string[];
}

export type AsyncRendererComponentFn<U> = (
@@ -1149,7 +1145,7 @@ export interface AstroIntegration {
isRestart: boolean;
updateConfig: (newConfig: Record<string, any>) => void;
addRenderer: (renderer: AstroRenderer) => void;
addWatchFile: (target: InjectedWatchTarget) => void;
addWatchFile: (path: string) => void;
injectScript: (stage: InjectedScriptStage, content: string) => void;
injectRoute: (injectRoute: InjectedRoute) => void;
// TODO: Add support for `injectElement()` for full HTML element injection, not just scripts.
12 changes: 4 additions & 8 deletions packages/astro/src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -204,15 +204,11 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
`${normalizePath(resolvedRoot)}.*astro\.config\.((mjs)|(cjs)|(js)|(ts))$`
).test(normalizePath(changedFile));

if (!shouldRestart && settings.watchTargets.length > 0) {
if (!shouldRestart && settings.watchFiles.length > 0) {
// If the config file didn't change, check if any of the watched files changed.
shouldRestart = settings.watchTargets.some(({ path, type }) => {
const target =
type === 'absolute'
? normalizePath(path)
: `${normalizePath(resolvedRoot)}/${normalizePath(path)}`;
return target === normalizePath(changedFile);
});
shouldRestart = settings.watchFiles.some(
(path) => normalizePath(path) === normalizePath(changedFile)
);
}

if (!shouldRestart) return;
2 changes: 1 addition & 1 deletion packages/astro/src/core/config/settings.ts
Original file line number Diff line number Diff line change
@@ -20,6 +20,6 @@ export function createSettings({ config, tsConfig, tsConfigPath }: CreateSetting
pageExtensions: ['.astro', '.md', '.html'],
renderers: [jsxRenderer],
scripts: [],
watchTargets: tsConfigPath ? [{ path: tsConfigPath, type: 'absolute' }] : [],
watchFiles: tsConfigPath ? [tsConfigPath] : [],
};
}
4 changes: 2 additions & 2 deletions packages/astro/src/integrations/index.ts
Original file line number Diff line number Diff line change
@@ -89,8 +89,8 @@ export async function runHookConfigSetup({
injectRoute: (injectRoute) => {
updatedSettings.injectedRoutes.push(injectRoute);
},
addWatchFile: (target) => {
updatedSettings.watchTargets.push(target);
addWatchFile: (path) => {
updatedSettings.watchFiles.push(path);
},
};
// Semi-private `addPageExtension` hook
2 changes: 1 addition & 1 deletion packages/integrations/tailwind/src/index.ts
Original file line number Diff line number Diff line change
@@ -104,7 +104,7 @@ export default function tailwindIntegration(options?: TailwindOptions): AstroInt
}

if (userConfig?.filePath) {
addWatchFile({ path: userConfig.filePath, type: 'absolute' });
addWatchFile(userConfig.filePath);
}

const tailwindConfig: TailwindConfig =