Skip to content

Commit

Permalink
feat(next): remove CCC (#12081)
Browse files Browse the repository at this point in the history
Co-authored-by: Sarah Rainsberger <[email protected]>
  • Loading branch information
florian-lefebvre and sarah11918 authored Sep 30, 2024
1 parent 21b5e80 commit 8679954
Show file tree
Hide file tree
Showing 18 changed files with 59 additions and 1,880 deletions.
29 changes: 29 additions & 0 deletions .changeset/wise-carrots-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
'astro': minor
---

Removes the experimental `contentCollectionsCache` introduced in `3.5.0`.

Astro Content Layer API independently solves some of the caching and performance issues with legacy content collections that this strategy attempted to address. This feature has been replaced with continued work on improvements to the content layer. If you were using this experimental feature, you must now remove the flag from your Astro config as it no longer exists:

```diff
export default defineConfig({
experimental: {
- contentCollectionsCache: true
}
})
```

The `cacheManifest` boolean argument is no longer passed to the `astro:build:done` integration hook:

```diff
const integration = {
name: "my-integration",
hooks: {
"astro:build:done": ({
- cacheManifest,
logger
}) => {}
}
}
```
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
9 changes: 0 additions & 9 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 Expand Up @@ -239,7 +231,6 @@ class AstroBuilder {
.map((pageData) => pageData.route)
.concat(hasServerIslands ? getServerIslandRouteData(this.settings.config) : []),
logging: this.logger,
cacheManifest: internals.cacheManifestUsed,
});

if (this.logger.level && levels[this.logger.level()] <= levels['info']) {
Expand Down
5 changes: 0 additions & 5 deletions packages/astro/src/core/build/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ export interface BuildInternals {
*/
discoveredScripts: Set<string>;

cachedClientEntries: string[];
cacheManifestUsed: boolean;

/**
* Map of propagated module ids (usually something like `/Users/...blog.mdx?astroPropagatedAssets`)
* to a set of stylesheets that it uses.
Expand Down Expand Up @@ -104,7 +101,6 @@ export interface BuildInternals {
*/
export function createBuildInternals(): BuildInternals {
return {
cachedClientEntries: [],
cssModuleToChunkIdMap: new Map(),
inlinedScripts: new Map(),
entrySpecifierToBundleMap: new Map<string, string>(),
Expand All @@ -121,7 +117,6 @@ export function createBuildInternals(): BuildInternals {
staticFiles: new Set(),
componentMetadata: new Map(),
entryPoints: new Map(),
cacheManifestUsed: false,
prerenderOnlyChunks: [],
};
}
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

0 comments on commit 8679954

Please sign in to comment.