1- import { contentEntryTypes } from './~dream.js' ;
21import * as devalue from 'devalue' ;
32import type fsMod from 'node:fs' ;
43import { pathToFileURL } from 'url' ;
@@ -7,9 +6,10 @@ import { AstroSettings } from '../@types/astro.js';
76import { AstroErrorData } from '../core/errors/errors-data.js' ;
87import { AstroError } from '../core/errors/errors.js' ;
98import { escapeViteEnvReferences , getFileInfo } from '../vite-plugin-utils/index.js' ;
10- import { defaultContentFileExts , CONTENT_FLAG } from './consts.js' ;
9+ import { CONTENT_FLAG } from './consts.js' ;
1110import {
1211 ContentConfig ,
12+ getContentEntryExts ,
1313 getContentPaths ,
1414 getEntryData ,
1515 getEntryInfo ,
@@ -19,9 +19,9 @@ import {
1919 parseFrontmatter ,
2020} from './utils.js' ;
2121
22- function isContentFlagImport ( viteId : string ) {
23- const { searchParams } = new URL ( viteId , 'file://' ) ;
24- return searchParams . has ( CONTENT_FLAG ) ;
22+ function isContentFlagImport ( viteId : string , contentEntryExts : string [ ] ) {
23+ const { searchParams, pathname } = new URL ( viteId , 'file://' ) ;
24+ return searchParams . has ( CONTENT_FLAG ) && contentEntryExts . some ( ( ext ) => pathname . endsWith ( ext ) ) ;
2525}
2626
2727export function astroContentImportPlugin ( {
@@ -32,16 +32,13 @@ export function astroContentImportPlugin({
3232 settings : AstroSettings ;
3333} ) : Plugin {
3434 const contentPaths = getContentPaths ( settings . config , fs ) ;
35- const contentFileExts = [
36- ...defaultContentFileExts ,
37- ...contentEntryTypes . map ( ( t ) => t . extensions ) . flat ( ) ,
38- ] ;
35+ const contentEntryExts = getContentEntryExts ( settings ) ;
3936
4037 return {
4138 name : 'astro:content-imports' ,
4239 async load ( id ) {
4340 const { fileId } = getFileInfo ( id , settings . config ) ;
44- if ( isContentFlagImport ( id ) ) {
41+ if ( isContentFlagImport ( id , contentEntryExts ) ) {
4542 const observable = globalContentConfigObserver . get ( ) ;
4643
4744 // Content config should be loaded before this plugin is used
@@ -74,7 +71,7 @@ export function astroContentImportPlugin({
7471 } ) ;
7572 }
7673 const rawContents = await fs . promises . readFile ( fileId , 'utf-8' ) ;
77- const contentEntryType = contentEntryTypes . find ( ( entryType ) =>
74+ const contentEntryType = settings . contentEntryTypes . find ( ( entryType ) =>
7875 entryType . extensions . some ( ( ext ) => fileId . endsWith ( ext ) )
7976 ) ;
8077 let body : string ,
@@ -129,11 +126,11 @@ export const _internal = {
129126 viteServer . watcher . on ( 'all' , async ( event , entry ) => {
130127 if (
131128 [ 'add' , 'unlink' , 'change' ] . includes ( event ) &&
132- getEntryType ( entry , contentPaths , contentFileExts ) === 'config'
129+ getEntryType ( entry , contentPaths , contentEntryExts ) === 'config'
133130 ) {
134131 // Content modules depend on config, so we need to invalidate them.
135132 for ( const modUrl of viteServer . moduleGraph . urlToModuleMap . keys ( ) ) {
136- if ( isContentFlagImport ( modUrl ) ) {
133+ if ( isContentFlagImport ( modUrl , contentEntryExts ) ) {
137134 const mod = await viteServer . moduleGraph . getModuleByUrl ( modUrl ) ;
138135 if ( mod ) {
139136 viteServer . moduleGraph . invalidateModule ( mod ) ;
@@ -144,7 +141,7 @@ export const _internal = {
144141 } ) ;
145142 } ,
146143 async transform ( code , id ) {
147- if ( isContentFlagImport ( id ) ) {
144+ if ( isContentFlagImport ( id , contentEntryExts ) ) {
148145 // Escape before Rollup internal transform.
149146 // Base on MUCH trial-and-error, inspired by MDX integration 2-step transform.
150147 return { code : escapeViteEnvReferences ( code ) } ;
0 commit comments