Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions packages/astro/src/content/content-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ class ContentLayer {
false,
// FUTURE: Remove in this in v6
id.endsWith('.svg'),
undefined,
this.#settings,
);

return parsedData;
Expand Down
14 changes: 10 additions & 4 deletions packages/astro/src/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { normalizePath } from '../core/viteUtils.js';
import type { AstroSettings } from '../types/astro.js';
import type { AstroConfig } from '../types/public/config.js';
import type { ContentEntryType, DataEntryType } from '../types/public/content.js';
import { getConfigAlias } from '../vite-plugin-config-alias/index.js';
import {
type CONTENT_FLAGS,
CONTENT_LAYER_TYPE,
Expand Down Expand Up @@ -155,7 +156,8 @@ export async function getEntryDataAndImages<
collectionConfig: CollectionConfig,
shouldEmitFile: boolean,
experimentalSvgEnabled: boolean,
pluginContext?: PluginContext,
pluginContext: PluginContext | undefined = undefined,
astroSettings: AstroSettings,
): Promise<{ data: TOutputData; imageImports: Array<string> }> {
let data: TOutputData;
// Legacy content collections have 'slug' removed
Expand All @@ -169,6 +171,7 @@ export async function getEntryDataAndImages<
let schema = collectionConfig.schema;

const imageImports = new Set<string>();
const importAliases = getConfigAlias(astroSettings);

if (typeof schema === 'function') {
if (pluginContext) {
Expand All @@ -190,7 +193,7 @@ export async function getEntryDataAndImages<
// - Relative paths (./foo, ../foo)
// - Absolute paths (/foo)
// - URLs (http://...)
// - Aliases (~/, @/, etc.)
// - Aliases (~/, @/, tsconfig aliases)
let normalizedPath = val;
if (
val &&
Expand All @@ -199,7 +202,8 @@ export async function getEntryDataAndImages<
!val.startsWith('/') &&
!val.startsWith('~') &&
!val.startsWith('@') &&
!val.includes('://')
!val.includes('://') &&
!importAliases?.some((alias) => alias.find.test(val))
) {
normalizedPath = `./${val}`;
}
Expand Down Expand Up @@ -273,14 +277,16 @@ export async function getEntryData(
collectionConfig: CollectionConfig,
shouldEmitFile: boolean,
experimentalSvgEnabled: boolean,
pluginContext?: PluginContext,
pluginContext: PluginContext | undefined = undefined,
astroSettings: AstroSettings,
) {
const { data } = await getEntryDataAndImages(
entry,
collectionConfig,
shouldEmitFile,
experimentalSvgEnabled,
pluginContext,
astroSettings,
);
return data;
}
Expand Down
9 changes: 5 additions & 4 deletions packages/astro/src/content/vite-plugin-content-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { AstroError } from '../core/errors/errors.js';
import { AstroErrorData } from '../core/errors/index.js';
import type { Logger } from '../core/logger/core.js';
import type { AstroSettings } from '../types/astro.js';
import type { AstroConfig } from '../types/public/config.js';
import type {
ContentEntryModule,
ContentEntryType,
Expand Down Expand Up @@ -105,7 +104,7 @@ export function astroContentImportPlugin({
fileId,
entryConfigByExt: dataEntryConfigByExt,
contentDir,
config: settings.config,
settings,
fs,
pluginContext: this,
shouldEmitFile,
Expand All @@ -128,7 +127,7 @@ export const _internal = {
fileId,
entryConfigByExt: contentEntryConfigByExt,
contentDir,
config: settings.config,
settings,
fs,
pluginContext: this,
shouldEmitFile,
Expand Down Expand Up @@ -210,7 +209,7 @@ type GetEntryModuleParams<TEntryType extends ContentEntryType | DataEntryType> =
contentDir: URL;
pluginContext: PluginContext;
entryConfigByExt: Map<string, TEntryType>;
config: AstroConfig;
settings: AstroSettings;
shouldEmitFile: boolean;
};

Expand Down Expand Up @@ -248,6 +247,7 @@ async function getContentEntryModule(
// FUTURE: Remove in this in v6
id.endsWith('.svg'),
pluginContext,
params.settings,
)
: unvalidatedData;

Expand Down Expand Up @@ -285,6 +285,7 @@ async function getDataEntryModule(
// FUTURE: Remove in this in v6
id.endsWith('.svg'),
pluginContext,
params.settings,
)
: unvalidatedData;

Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/vite-plugin-config-alias/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Alias = {
};

/** Returns a list of compiled aliases. */
const getConfigAlias = (settings: AstroSettings): Alias[] | null => {
export const getConfigAlias = (settings: AstroSettings): Alias[] | null => {
const { tsConfig, tsConfigPath } = settings;
if (!tsConfig || !tsConfigPath || !tsConfig.compilerOptions) return null;

Expand Down
5 changes: 5 additions & 0 deletions packages/astro/test/content-layer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ describe('Content Layer', () => {
assert.equal(json.rockets[1].data.image.format, 'jpg');
});

it('loads images with tsconfig aliases in JSON', async () => {
assert.ok(json.rockets[2].data.image.src.startsWith('/_astro'));
assert.equal(json.rockets[2].data.image.format, 'jpg');
});

it('renders images from frontmatter', async () => {
assert.ok($('img[alt="Lunar Module"]').attr('src').startsWith('/_astro'));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@
"name": "Saturn V",
"manufacturer": "NASA",
"image": "./shuttle.jpg"
},
{
"id": "apollo-11",
"name": "Apollo 11",
"manufacturer": "NASA",
"image": "$images/shuttle.jpg"
}
]
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/content-layer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "astro/tsconfigs/base",
"compilerOptions": {
"paths": {
"$images/*": ["./images/*"]
}
}
}
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading