Skip to content

Commit

Permalink
feat: add astroPropagatedAssets flag with vite
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Apr 26, 2023
1 parent ac08dc3 commit 2993e80
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/with-markdoc/markdoc.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineMarkdocConfig } from '@astrojs/markdoc/config';
import Aside from './src/components/Aside.astro?astroPropagatedAssets';
import Aside from './src/components/Aside.astro';

export default defineMarkdocConfig({
tags: {
Expand Down
27 changes: 26 additions & 1 deletion packages/integrations/markdoc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ export default function markdocIntegration(legacyConfig: any): AstroIntegration
name: '@astrojs/markdoc',
hooks: {
'astro:config:setup': async (params) => {
const { config: astroConfig, addContentEntryType } = params as SetupHookParams;
const {
config: astroConfig,
updateConfig,
addContentEntryType,
} = params as SetupHookParams;

const configLoadResult = await loadMarkdocConfig(astroConfig);
const userMarkdocConfig = configLoadResult?.config ?? {};
Expand Down Expand Up @@ -127,6 +131,27 @@ export const Content = createComponent({
'utf-8'
),
});

updateConfig({
vite: {
plugins: [
{
name: '@astrojs/markdoc:astro-propagated-assets',
enforce: 'pre',
// Astro component styles and scripts should only be injected
// When a given Markdoc file actually uses that component.
// Add the `astroPropagatedAssets` flag to inject only when rendered.
resolveId(this: rollup.TransformPluginContext, id: string, importer: string) {
if (importer === configLoadResult?.fileUrl.pathname && id.endsWith('.astro')) {
return this.resolve(id + '?astroPropagatedAssets', importer, {
skipSelf: true,
});
}
},
},
],
},
});
},
},
};
Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/markdoc/src/load-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async function bundleConfigFile({
{
name: 'stub-astro-imports',
setup(build) {
build.onResolve({ filter: /.*\.astro\?astroPropagatedAssets$/ }, () => {
build.onResolve({ filter: /.*\.astro$/ }, () => {
return {
// Stub with an unused default export
path: 'data:text/javascript,export default true',
Expand Down

0 comments on commit 2993e80

Please sign in to comment.