Skip to content

Commit

Permalink
Merge branch 'main' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re committed Nov 9, 2023
2 parents 139088d + 1eb5d7f commit ea0cb9c
Show file tree
Hide file tree
Showing 17 changed files with 259 additions and 192 deletions.
2 changes: 1 addition & 1 deletion packages/astro/content-module.template.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function createGlobLookup(glob) {
};
}

const renderEntryGlob = '@@RENDER_ENTRY_GLOB_PATH@@'
const renderEntryGlob = '@@RENDER_ENTRY_GLOB_PATH@@';
const collectionToRenderEntryMap = createCollectionToGlobResultMap({
globResult: renderEntryGlob,
contentDir,
Expand Down
15 changes: 3 additions & 12 deletions packages/astro/src/content/vite-plugin-content-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,21 +194,15 @@ export function astroConfigBuildPlugin(
JSON.stringify(Array.from(entryStyles))
);
} else {
newCode = newCode.replace(
JSON.stringify(STYLES_PLACEHOLDER),
"[]"
);
newCode = newCode.replace(JSON.stringify(STYLES_PLACEHOLDER), '[]');
}
if (entryLinks.size) {
newCode = newCode.replace(
JSON.stringify(LINKS_PLACEHOLDER),
JSON.stringify(Array.from(entryLinks).map(prependBase))
);
} else {
newCode = newCode.replace(
JSON.stringify(LINKS_PLACEHOLDER),
"[]"
);
newCode = newCode.replace(JSON.stringify(LINKS_PLACEHOLDER), '[]');
}
if (entryScripts.size) {
const entryFileNames = new Set<string>();
Expand All @@ -235,10 +229,7 @@ export function astroConfigBuildPlugin(
)
);
} else {
newCode = newCode.replace(
JSON.stringify(SCRIPTS_PLACEHOLDER),
"[]"
);
newCode = newCode.replace(JSON.stringify(SCRIPTS_PLACEHOLDER), '[]');
}
mutate(chunk, ['server'], newCode);
}
Expand Down
94 changes: 54 additions & 40 deletions packages/astro/src/content/vite-plugin-content-virtual-mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ import { fileURLToPath, pathToFileURL } from 'node:url';
import pLimit from 'p-limit';
import { type Plugin } from 'vite';
import type { AstroSettings } from '../@types/astro.js';
import { encodeName } from '../core/build/util.js';
import { AstroError, AstroErrorData } from '../core/errors/index.js';
import { appendForwardSlash, removeFileExtension } from '../core/path.js';
import { rootRelativePath } from '../core/util.js';
import { encodeName } from '../core/build/util.js';
import { CONTENT_FLAG, CONTENT_RENDER_FLAG, DATA_FLAG, VIRTUAL_MODULE_ID, RESOLVED_VIRTUAL_MODULE_ID } from './consts.js';
import { isServerLikeOutput } from '../prerender/utils.js';
import type { AstroPluginMetadata } from '../vite-plugin-astro/index.js';
import {
CONTENT_FLAG,
CONTENT_RENDER_FLAG,
DATA_FLAG,
RESOLVED_VIRTUAL_MODULE_ID,
VIRTUAL_MODULE_ID,
} from './consts.js';
import {
getContentEntryIdAndSlug,
getContentPaths,
Expand All @@ -22,12 +30,10 @@ import {
getExtGlob,
type ContentLookupMap,
} from './utils.js';
import type { AstroPluginMetadata } from '../vite-plugin-astro/index.js';
import { isServerLikeOutput } from '../prerender/utils.js';

interface AstroContentVirtualModPluginParams {
settings: AstroSettings;
fs: typeof nodeFs
fs: typeof nodeFs;
}

export function astroContentVirtualModPlugin({
Expand All @@ -40,7 +46,7 @@ export function astroContentVirtualModPlugin({
name: 'astro-content-virtual-mod-plugin',
enforce: 'pre',
configResolved(config) {
IS_DEV = config.mode === 'development'
IS_DEV = config.mode === 'development';
},
resolveId(id) {
if (id === VIRTUAL_MODULE_ID) {
Expand All @@ -51,7 +57,7 @@ export function astroContentVirtualModPlugin({
return RESOLVED_VIRTUAL_MODULE_ID;
} else {
// For SSG (production), we will build this file ourselves
return { id: RESOLVED_VIRTUAL_MODULE_ID, external: true }
return { id: RESOLVED_VIRTUAL_MODULE_ID, external: true };
}
}
},
Expand All @@ -72,9 +78,9 @@ export function astroContentVirtualModPlugin({
scripts: [],
containsHead: true,
propagation: 'in-tree',
pageOptions: {}
}
} satisfies AstroPluginMetadata
pageOptions: {},
},
} satisfies AstroPluginMetadata,
};
}
},
Expand All @@ -87,19 +93,19 @@ export function astroContentVirtualModPlugin({
const prefix = depth > 0 ? '../'.repeat(depth) : './';
return code.replaceAll(RESOLVED_VIRTUAL_MODULE_ID, `${prefix}content/entry.mjs`);
}
}
},
};
}

export async function generateContentEntryFile({
settings,
lookupMap,
IS_DEV,
IS_SERVER
IS_SERVER,
}: {
settings: AstroSettings;
fs: typeof nodeFs;
lookupMap: ContentLookupMap
lookupMap: ContentLookupMap;
IS_DEV: boolean;
IS_SERVER: boolean;
}) {
Expand All @@ -113,61 +119,69 @@ export async function generateContentEntryFile({
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);
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);
contentEntryGlobResult = getStringifiedCollectionFromLookup(
'content',
relContentDir,
lookupMap
);
dataEntryGlobResult = getStringifiedCollectionFromLookup('data', relContentDir, lookupMap);
renderEntryGlobResult = getStringifiedCollectionFromLookup('render', relContentDir, lookupMap);
}

const virtualModContents = nodeFs
.readFileSync(contentPaths.virtualModTemplate, 'utf-8')
.replace('@@CONTENT_DIR@@', relContentDir)
.replace(
"'@@CONTENT_ENTRY_GLOB_PATH@@'",
contentEntryGlobResult
)
.replace(
"'@@DATA_ENTRY_GLOB_PATH@@'",
dataEntryGlobResult
)
.replace(
"'@@RENDER_ENTRY_GLOB_PATH@@'",
renderEntryGlobResult
).replace(
'/* @@LOOKUP_MAP_ASSIGNMENT@@ */',
`lookupMap = ${JSON.stringify(lookupMap)};`
);
.replace("'@@CONTENT_ENTRY_GLOB_PATH@@'", contentEntryGlobResult)
.replace("'@@DATA_ENTRY_GLOB_PATH@@'", dataEntryGlobResult)
.replace("'@@RENDER_ENTRY_GLOB_PATH@@'", renderEntryGlobResult)
.replace('/* @@LOOKUP_MAP_ASSIGNMENT@@ */', `lookupMap = ${JSON.stringify(lookupMap)};`);

return virtualModContents;
}

function getStringifiedCollectionFromLookup(wantedType: 'content' | 'data' | 'render', relContentDir: string, lookupMap: ContentLookupMap) {
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}`
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}`
normalize = (slug: string) => `${slug}?${suffix}`;
}
for (const { type, entries } of Object.values(lookupMap)) {
if (type === wantedType || wantedType === 'render' && type === 'content') {
if (type === wantedType || (wantedType === 'render' && type === 'content')) {
for (const slug of Object.values(entries)) {
str += `\n "${slug}": () => import("${normalize(slug)}"),`
str += `\n "${slug}": () => import("${normalize(slug)}"),`;
}
}
}
str += '\n}'
str += '\n}';
return str;
}

Expand Down Expand Up @@ -202,7 +216,7 @@ export async function generateLookupMap({
cwd: fileURLToPath(root),
fs,
}
)
);

// Run 10 at a time to prevent `await getEntrySlug` from accessing the filesystem all at once.
// Each await shouldn't take too long for the work to be noticably slow too.
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default async function build(
const contentCacheDir = new URL('./content/', astroConfig.cacheDir);
if (fs.existsSync(contentCacheDir)) {
logger.warn('content', 'clearing cache');
await fs.promises.rm(contentCacheDir, { force: true, recursive: true })
await fs.promises.rm(contentCacheDir, { force: true, recursive: true });
}
}

Expand Down
5 changes: 4 additions & 1 deletion packages/astro/src/core/build/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export interface BuildBeforeHookResult {
export type AstroBuildPlugin = {
targets: BuildTarget[];
hooks?: {
'build:before'?: (opts: { target: BuildTarget; input: Set<string> }) => BuildBeforeHookResult | Promise<BuildBeforeHookResult>;
'build:before'?: (opts: {
target: BuildTarget;
input: Set<string>;
}) => BuildBeforeHookResult | Promise<BuildBeforeHookResult>;
'build:post'?: (opts: {
ssrOutputs: RollupOutputArray;
clientOutputs: RollupOutputArray;
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { astroHeadBuildPlugin } from '../../../vite-plugin-head/index.js';
import type { AstroBuildPluginContainer } from '../plugin.js';
import { pluginAliasResolve } from './plugin-alias-resolve.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';
Expand All @@ -13,7 +14,6 @@ import { pluginMiddleware } from './plugin-middleware.js';
import { pluginPages } from './plugin-pages.js';
import { pluginPrerender } from './plugin-prerender.js';
import { pluginRenderers } from './plugin-renderers.js';
import { pluginChunks } from './plugin-chunks.js';
import { pluginSSR, pluginSSRSplit } from './plugin-ssr.js';

export function registerAllPlugins({ internals, options, register }: AstroBuildPluginContainer) {
Expand Down
26 changes: 13 additions & 13 deletions packages/astro/src/core/build/plugins/plugin-chunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import type { AstroBuildPlugin } from '../plugin.js';
import { extendManualChunks } from './util.js';

export function vitePluginChunks(): VitePlugin {
return {
name: 'astro:chunks',
outputOptions(outputOptions) {
extendManualChunks(outputOptions, {
after(id) {
// Place Astro's server runtime in a single `astro/server.mjs` file
if (id.includes('astro/dist/runtime/server/')) {
return 'astro/server'
}
},
});
}
}
return {
name: 'astro:chunks',
outputOptions(outputOptions) {
extendManualChunks(outputOptions, {
after(id) {
// Place Astro's server runtime in a single `astro/server.mjs` file
if (id.includes('astro/dist/runtime/server/')) {
return 'astro/server';
}
},
});
},
};
}

// Build plugin that configures specific chunking behavior
Expand Down
Loading

0 comments on commit ea0cb9c

Please sign in to comment.