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(next): remove CCC #12081

Merged
merged 9 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 15 additions & 0 deletions .changeset/wise-carrots-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'astro': minor
florian-lefebvre marked this conversation as resolved.
Show resolved Hide resolved
---

Removes the experimental `contentCollectionsCache` introduced in `3.5.0`.
florian-lefebvre marked this conversation as resolved.
Show resolved Hide resolved

To migrate, remove the flag from your Astro config:

```diff
export default defineConfig({
experimental: {
- contentCollectionsCache: true
}
})
```
florian-lefebvre marked this conversation as resolved.
Show resolved Hide resolved
110 changes: 21 additions & 89 deletions packages/astro/src/content/vite-plugin-content-virtual-mod.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { dataToEsm } from '@rollup/pluginutils';
import glob from 'fast-glob';
import nodeFs from 'node:fs';
import { extname } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { dataToEsm } from '@rollup/pluginutils';
import glob from 'fast-glob';
import pLimit from 'p-limit';
import type { Plugin } from 'vite';
import { encodeName } from '../core/build/util.js';
import { AstroError, AstroErrorData } from '../core/errors/index.js';
import { appendForwardSlash, removeFileExtension } from '../core/path.js';
import { appendForwardSlash } from '../core/path.js';
import { rootRelativePath } from '../core/viteUtils.js';
import type { AstroSettings } from '../types/astro.js';
import type { AstroPluginMetadata } from '../vite-plugin-astro/index.js';
Expand Down Expand Up @@ -52,7 +51,6 @@ export function astroContentVirtualModPlugin({
fs,
}: AstroContentVirtualModPluginParams): Plugin {
let IS_DEV = false;
const IS_SERVER = settings.buildOutput === 'server';
let dataStoreFile: URL;
return {
name: 'astro-content-virtual-mod-plugin',
Expand All @@ -63,15 +61,7 @@ export function astroContentVirtualModPlugin({
},
async resolveId(id) {
if (id === VIRTUAL_MODULE_ID) {
if (!settings.config.experimental.contentCollectionCache) {
return RESOLVED_VIRTUAL_MODULE_ID;
}
if (IS_DEV || IS_SERVER) {
return RESOLVED_VIRTUAL_MODULE_ID;
} else {
// For SSG (production), we will build this file ourselves
return { id: RESOLVED_VIRTUAL_MODULE_ID, external: true };
}
return RESOLVED_VIRTUAL_MODULE_ID;
}
if (id === DATA_STORE_VIRTUAL_ID) {
return RESOLVED_DATA_STORE_VIRTUAL_ID;
Expand Down Expand Up @@ -117,8 +107,6 @@ export function astroContentVirtualModPlugin({
settings,
fs,
lookupMap,
IS_DEV,
IS_SERVER,
isClient,
});

Expand Down Expand Up @@ -167,17 +155,6 @@ export function astroContentVirtualModPlugin({
return fs.readFileSync(modules, 'utf-8');
}
},
renderChunk(code, chunk) {
if (!settings.config.experimental.contentCollectionCache) {
return;
}
if (code.includes(RESOLVED_VIRTUAL_MODULE_ID)) {
const depth = chunk.fileName.split('/').length - 1;
const prefix = depth > 0 ? '../'.repeat(depth) : './';
return code.replaceAll(RESOLVED_VIRTUAL_MODULE_ID, `${prefix}content/entry.mjs`);
}
},

configureServer(server) {
const dataStorePath = fileURLToPath(dataStoreFile);

Expand Down Expand Up @@ -214,15 +191,11 @@ export function astroContentVirtualModPlugin({
export async function generateContentEntryFile({
settings,
lookupMap,
IS_DEV,
IS_SERVER,
isClient,
}: {
settings: AstroSettings;
fs: typeof nodeFs;
lookupMap: ContentLookupMap;
IS_DEV: boolean;
IS_SERVER: boolean;
isClient: boolean;
}) {
const contentPaths = getContentPaths(settings.config);
Expand All @@ -231,33 +204,23 @@ export async function generateContentEntryFile({
let contentEntryGlobResult: string;
let dataEntryGlobResult: string;
let renderEntryGlobResult: string;
if (IS_DEV || IS_SERVER || !settings.config.experimental.contentCollectionCache) {
const contentEntryConfigByExt = getEntryConfigByExtMap(settings.contentEntryTypes);
const contentEntryExts = [...contentEntryConfigByExt.keys()];
const dataEntryExts = getDataEntryExts(settings);
const createGlob = (value: string[], flag: string) =>
`import.meta.glob(${JSON.stringify(value)}, { query: { ${flag}: true } })`;
contentEntryGlobResult = createGlob(
globWithUnderscoresIgnored(relContentDir, contentEntryExts),
CONTENT_FLAG,
);
dataEntryGlobResult = createGlob(
globWithUnderscoresIgnored(relContentDir, dataEntryExts),
DATA_FLAG,
);
renderEntryGlobResult = createGlob(
globWithUnderscoresIgnored(relContentDir, contentEntryExts),
CONTENT_RENDER_FLAG,
);
} else {
contentEntryGlobResult = getStringifiedCollectionFromLookup(
'content',
relContentDir,
lookupMap,
);
dataEntryGlobResult = getStringifiedCollectionFromLookup('data', relContentDir, lookupMap);
renderEntryGlobResult = getStringifiedCollectionFromLookup('render', relContentDir, lookupMap);
}
const contentEntryConfigByExt = getEntryConfigByExtMap(settings.contentEntryTypes);
const contentEntryExts = [...contentEntryConfigByExt.keys()];
const dataEntryExts = getDataEntryExts(settings);
const createGlob = (value: string[], flag: string) =>
`import.meta.glob(${JSON.stringify(value)}, { query: { ${flag}: true } })`;
contentEntryGlobResult = createGlob(
globWithUnderscoresIgnored(relContentDir, contentEntryExts),
CONTENT_FLAG,
);
dataEntryGlobResult = createGlob(
globWithUnderscoresIgnored(relContentDir, dataEntryExts),
DATA_FLAG,
);
renderEntryGlobResult = createGlob(
globWithUnderscoresIgnored(relContentDir, contentEntryExts),
CONTENT_RENDER_FLAG,
);

let virtualModContents: string;
if (isClient) {
Expand All @@ -278,37 +241,6 @@ export async function generateContentEntryFile({
return virtualModContents;
}

function getStringifiedCollectionFromLookup(
wantedType: 'content' | 'data' | 'render',
relContentDir: string,
lookupMap: ContentLookupMap,
) {
let str = '{';
// In dev, we don't need to normalize the import specifier at all. Vite handles it.
let normalize = (slug: string) => slug;
// For prod builds, we need to transform from `/src/content/**/*.{md,mdx,json,yaml}` to a relative `./**/*.mjs` import
if (process.env.NODE_ENV === 'production') {
const suffix = wantedType === 'render' ? '.entry.mjs' : '.mjs';
normalize = (slug: string) =>
`${removeFileExtension(encodeName(slug)).replace(relContentDir, './')}${suffix}`;
} else {
let suffix = '';
if (wantedType === 'content') suffix = CONTENT_FLAG;
else if (wantedType === 'data') suffix = DATA_FLAG;
else if (wantedType === 'render') suffix = CONTENT_RENDER_FLAG;
normalize = (slug: string) => `${slug}?${suffix}`;
}
for (const { type, entries } of Object.values(lookupMap)) {
if (type === wantedType || (wantedType === 'render' && type === 'content')) {
for (const slug of Object.values(entries)) {
str += `\n "${slug}": () => import("${normalize(slug)}"),`;
}
}
}
str += '\n}';
return str;
}

/**
* Generate a map from a collection + slug to the local file path.
* This is used internally to resolve entry imports when using `getEntry()`.
Expand Down
8 changes: 0 additions & 8 deletions packages/astro/src/core/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,6 @@ export default async function build(
const settings = await createSettings(astroConfig, fileURLToPath(astroConfig.root));

if (inlineConfig.force) {
if (astroConfig.experimental.contentCollectionCache) {
const contentCacheDir = new URL('./content/', astroConfig.cacheDir);
if (fs.existsSync(contentCacheDir)) {
logger.debug('content', 'clearing content cache');
await fs.promises.rm(contentCacheDir, { force: true, recursive: true });
logger.warn('content', 'content cache cleared (force)');
}
}
await clearContentLayerCache({ settings, logger, fs });
}

Expand Down
2 changes: 0 additions & 2 deletions packages/astro/src/core/build/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { AstroBuildPluginContainer } from '../plugin.js';
import { pluginAnalyzer } from './plugin-analyzer.js';
import { pluginChunks } from './plugin-chunks.js';
import { pluginComponentEntry } from './plugin-component-entry.js';
import { pluginContent } from './plugin-content.js';
import { pluginCSS } from './plugin-css.js';
import { pluginInternals } from './plugin-internals.js';
import { pluginManifest } from './plugin-manifest.js';
Expand All @@ -23,7 +22,6 @@ export function registerAllPlugins({ internals, options, register }: AstroBuildP
register(pluginRenderers(options));
register(pluginMiddleware(options, internals));
register(pluginPages(options, internals));
register(pluginContent(options, internals));
register(pluginCSS(options, internals));
register(astroHeadBuildPlugin(internals));
register(pluginPrerender(options, internals));
Expand Down
Loading
Loading