@@ -18,12 +18,9 @@ import {
18
18
} from './consts.js' ;
19
19
import { getContentEntryExts } from './utils.js' ;
20
20
21
- function isPropagatedAsset ( viteId : string , contentEntryExts : string [ ] ) : boolean {
22
- const url = new URL ( viteId , 'file://' ) ;
23
- return (
24
- url . searchParams . has ( PROPAGATED_ASSET_FLAG ) &&
25
- contentEntryExts . some ( ( ext ) => url . pathname . endsWith ( ext ) )
26
- ) ;
21
+ function isPropagatedAsset ( viteId : string ) {
22
+ const flags = new URLSearchParams ( viteId . split ( '?' ) [ 1 ] ) ;
23
+ return flags . has ( PROPAGATED_ASSET_FLAG ) ;
27
24
}
28
25
29
26
export function astroContentAssetPropagationPlugin ( {
@@ -37,51 +34,56 @@ export function astroContentAssetPropagationPlugin({
37
34
const contentEntryExts = getContentEntryExts ( settings ) ;
38
35
return {
39
36
name : 'astro:content-asset-propagation' ,
40
- enforce : 'pre' ,
41
37
configureServer ( server ) {
42
38
if ( mode === 'dev' ) {
43
39
devModuleLoader = createViteLoader ( server ) ;
44
40
}
45
41
} ,
46
- load ( id ) {
47
- if ( isPropagatedAsset ( id , contentEntryExts ) ) {
42
+ async transform ( _ , id , options ) {
43
+ if ( isPropagatedAsset ( id ) ) {
48
44
const basePath = id . split ( '?' ) [ 0 ] ;
45
+ let stringifiedLinks : string , stringifiedStyles : string , stringifiedScripts : string ;
46
+
47
+ // We can access the server in dev,
48
+ // so resolve collected styles and scripts here.
49
+ if ( options ?. ssr && devModuleLoader ) {
50
+ if ( ! devModuleLoader . getModuleById ( basePath ) ?. ssrModule ) {
51
+ await devModuleLoader . import ( basePath ) ;
52
+ }
53
+ const { stylesMap, urls } = await getStylesForURL (
54
+ pathToFileURL ( basePath ) ,
55
+ devModuleLoader ,
56
+ 'development'
57
+ ) ;
58
+
59
+ const hoistedScripts = await getScriptsForURL (
60
+ pathToFileURL ( basePath ) ,
61
+ settings . config . root ,
62
+ devModuleLoader
63
+ ) ;
64
+
65
+ stringifiedLinks = JSON . stringify ( [ ...urls ] ) ;
66
+ stringifiedStyles = JSON . stringify ( [ ...stylesMap . values ( ) ] ) ;
67
+ stringifiedScripts = JSON . stringify ( [ ...hoistedScripts ] ) ;
68
+ } else {
69
+ // Otherwise, use placeholders to inject styles and scripts
70
+ // during the production bundle step.
71
+ // @see the `astro:content-build-plugin` below.
72
+ stringifiedLinks = JSON . stringify ( LINKS_PLACEHOLDER ) ;
73
+ stringifiedStyles = JSON . stringify ( STYLES_PLACEHOLDER ) ;
74
+ stringifiedScripts = JSON . stringify ( SCRIPTS_PLACEHOLDER ) ;
75
+ }
76
+
49
77
const code = `
50
78
export async function getMod() {
51
79
return import(${ JSON . stringify ( basePath ) } );
52
80
}
53
- export const collectedLinks = ${ JSON . stringify ( LINKS_PLACEHOLDER ) } ;
54
- export const collectedStyles = ${ JSON . stringify ( STYLES_PLACEHOLDER ) } ;
55
- export const collectedScripts = ${ JSON . stringify ( SCRIPTS_PLACEHOLDER ) } ;
81
+ export const collectedLinks = ${ stringifiedLinks } ;
82
+ export const collectedStyles = ${ stringifiedStyles } ;
83
+ export const collectedScripts = ${ stringifiedScripts } ;
56
84
` ;
57
- return { code } ;
58
- }
59
- } ,
60
- async transform ( code , id , options ) {
61
- if ( ! options ?. ssr ) return ;
62
- if ( devModuleLoader && isPropagatedAsset ( id , contentEntryExts ) ) {
63
- const basePath = id . split ( '?' ) [ 0 ] ;
64
- if ( ! devModuleLoader . getModuleById ( basePath ) ?. ssrModule ) {
65
- await devModuleLoader . import ( basePath ) ;
66
- }
67
- const { stylesMap, urls } = await getStylesForURL (
68
- pathToFileURL ( basePath ) ,
69
- devModuleLoader ,
70
- 'development'
71
- ) ;
72
85
73
- const hoistedScripts = await getScriptsForURL (
74
- pathToFileURL ( basePath ) ,
75
- settings . config . root ,
76
- devModuleLoader
77
- ) ;
78
-
79
- return {
80
- code : code
81
- . replace ( JSON . stringify ( LINKS_PLACEHOLDER ) , JSON . stringify ( [ ...urls ] ) )
82
- . replace ( JSON . stringify ( STYLES_PLACEHOLDER ) , JSON . stringify ( [ ...stylesMap . values ( ) ] ) )
83
- . replace ( JSON . stringify ( SCRIPTS_PLACEHOLDER ) , JSON . stringify ( [ ...hoistedScripts ] ) ) ,
84
- } ;
86
+ return { code, map : { mappings : '' } } ;
85
87
}
86
88
} ,
87
89
} ;
0 commit comments