Skip to content

Commit

Permalink
fix: getCollection _inside_ content directory
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re committed Oct 20, 2023
1 parent 1c772ee commit 0ab8f7d
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions packages/astro/src/core/build/plugins/plugin-external.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Plugin as VitePlugin } from 'vite';
import type { AstroBuildPlugin } from '../plugin.js';
import type { AstroBuildPlugin, BuildTarget } from '../plugin.js';
import { slash, removeLeadingForwardSlash } from '../../path.js';

export function vitePluginExternalize(): VitePlugin {
export function vitePluginExternalize({ target }: { target: BuildTarget }): VitePlugin {
const MODULE_ID = `astro:content`;
const VIRTUAL_MODULE_ID = `\0${MODULE_ID}`;

Expand All @@ -17,6 +17,9 @@ export function vitePluginExternalize(): VitePlugin {
},
renderChunk(code, chunk) {
if (chunk.imports.find(name => name === VIRTUAL_MODULE_ID)) {
if (target === 'content') {
return code.replaceAll(VIRTUAL_MODULE_ID, `./.entry.mjs`);
}
// We want to generate a relative path and avoid a hardcoded absolute path in the output!
const steps = removeLeadingForwardSlash(slash(chunk.fileName)).split('/').length - 1;
const prefix = '../'.repeat(steps) || './';
Expand All @@ -31,9 +34,9 @@ export function pluginExternalize(): AstroBuildPlugin {
return {
targets: ['server', 'content'],
hooks: {
'build:before': () => {
'build:before': ({ target }) => {
return {
vitePlugin: vitePluginExternalize(),
vitePlugin: vitePluginExternalize({ target }),
};
},
},
Expand Down

0 comments on commit 0ab8f7d

Please sign in to comment.