Skip to content

Commit

Permalink
fix: run type gen when content/ dir added in dev
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Jan 10, 2023
1 parent e653360 commit 96fb348
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions packages/astro/src/content/vite-plugin-content-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { pathToFileURL } from 'node:url';
import type { Plugin } from 'vite';
import type { AstroSettings } from '../@types/astro.js';
import { info, LogOptions } from '../core/logger/core.js';
import { appendForwardSlash } from '../core/path.js';
import { escapeViteEnvReferences, getFileInfo } from '../vite-plugin-utils/index.js';
import { contentFileExts, CONTENT_FLAG } from './consts.js';
import { createContentTypesGenerator, getEntryType } from './types-generator.js';
Expand Down Expand Up @@ -32,56 +33,56 @@ export function astroContentServerPlugin({
mode,
}: AstroContentServerPluginParams): Plugin[] {
const contentPaths = getContentPaths(settings.config);
let contentDirExists = false;
let contentGenerator: Awaited<ReturnType<typeof createContentTypesGenerator>>;
const contentConfigObserver = contentObservable({ status: 'loading' });

async function initContentGenerator() {
const contentGenerator = await createContentTypesGenerator({
fs,
settings,
logging,
contentConfigObserver,
});
await contentGenerator.init();
return contentGenerator;
}

return [
{
name: 'astro-content-server-plugin',
async config(viteConfig) {
try {
await fs.promises.stat(contentPaths.contentDir);
contentDirExists = true;
} catch {
/* silently move on */
return;
}

if (contentDirExists && (mode === 'dev' || viteConfig.build?.ssr === true)) {
contentGenerator = await createContentTypesGenerator({
fs,
settings,
logging,
contentConfigObserver,
});
await contentGenerator.init();
info(logging, 'content', 'Types generated');
// Production build type gen
if (fs.existsSync(contentPaths.contentDir) && viteConfig.build?.ssr === true) {
await initContentGenerator();
}
},
async configureServer(viteServer) {
if (mode !== 'dev') return;

if (contentDirExists) {
// Dev server type gen
if (fs.existsSync(contentPaths.contentDir)) {
info(
logging,
'content',
`Watching ${cyan(
contentPaths.contentDir.href.replace(settings.config.root.href, '')
)} for changes`
);
attachListeners();
await attachListeners();
} else {
viteServer.watcher.on('addDir', (dir) => {
if (pathToFileURL(dir).href === contentPaths.contentDir.href) {
viteServer.watcher.on('addDir', contentDirListener);
async function contentDirListener(dir: string) {
if (appendForwardSlash(pathToFileURL(dir).href) === contentPaths.contentDir.href) {
info(logging, 'content', `Content dir found. Watching for changes`);
contentDirExists = true;
attachListeners();
await attachListeners();
viteServer.watcher.removeListener('addDir', contentDirListener);
}
});
}
}

function attachListeners() {
async function attachListeners() {
const contentGenerator = await initContentGenerator();
info(logging, 'content', 'Types generated');

viteServer.watcher.on('add', (entry) => {
contentGenerator.queueEvent({ name: 'add', entry });
});
Expand Down

0 comments on commit 96fb348

Please sign in to comment.