-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: only load CSS when mdx is rendered!
- Loading branch information
1 parent
a9af735
commit 9fc5311
Showing
6 changed files
with
85 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,49 @@ | ||
export const FLAG = '?astro-asset-ssr'; | ||
import { Plugin } from 'vite'; | ||
import { moduleIsTopLevelPage, walkParentInfos } from '../core/build/graph.js'; | ||
import { BuildInternals, getPageDataByViteID } from '../core/build/internal.js'; | ||
|
||
const assetPlaceholder = `'@@ASTRO-ASSET-PLACEHOLDER@@'`; | ||
|
||
export const DELAYED_ASSET_FLAG = '?astro-asset-ssr'; | ||
|
||
export function injectDelayedAssetPlugin(): Plugin { | ||
return { | ||
name: 'astro-inject-delayed-asset-plugin', | ||
enforce: 'post', | ||
load(id) { | ||
if (id.endsWith(DELAYED_ASSET_FLAG)) { | ||
const code = ` | ||
export { Content } from ${JSON.stringify(id.replace(DELAYED_ASSET_FLAG, ''))}; | ||
export const collectedCss = ${assetPlaceholder} | ||
`; | ||
return code; | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
export function assetSsrPlugin({ internals }: { internals: BuildInternals }): Plugin { | ||
return { | ||
name: 'astro-asset-ssr-plugin', | ||
async generateBundle(_options, bundle) { | ||
for (const [_, chunk] of Object.entries(bundle)) { | ||
if (chunk.type === 'chunk' && chunk.code.includes(assetPlaceholder)) { | ||
for (const id of Object.keys(chunk.modules)) { | ||
for (const [pageInfo, depth, order] of walkParentInfos(id, this)) { | ||
if (moduleIsTopLevelPage(pageInfo)) { | ||
const pageViteID = pageInfo.id; | ||
const pageData = getPageDataByViteID(internals, pageViteID); | ||
if (pageData) { | ||
chunk.code = chunk.code.replace( | ||
assetPlaceholder, | ||
JSON.stringify([...(pageData.delayedCss ?? [])]) | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
} |