Skip to content

Commit

Permalink
fix(#6827): ensure appEntrypoint is referenced in Vue components
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re committed Jan 5, 2024
1 parent 64a8470 commit bc2a4ac
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions packages/integrations/vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import vue from '@vitejs/plugin-vue';
import type { Options as VueJsxOptions } from '@vitejs/plugin-vue-jsx';
import type { AstroIntegration, AstroRenderer } from 'astro';
import type { Plugin, UserConfig } from 'vite';
import { MagicString } from '@vue/compiler-sfc';

interface Options extends VueOptions {
jsx?: boolean | VueJsxOptions;
Expand Down Expand Up @@ -39,6 +40,7 @@ function virtualAppEntrypoint(options?: Options): Plugin {

let isBuild: boolean;
let root: string;
let appEntrypoint: string | undefined;

return {
name: '@astrojs/vue/virtual-app',
Expand All @@ -47,6 +49,11 @@ function virtualAppEntrypoint(options?: Options): Plugin {
},
configResolved(config) {
root = config.root;
if (options?.appEntrypoint) {
appEntrypoint = options.appEntrypoint.startsWith('.')
? path.resolve(root, options.appEntrypoint)
: options.appEntrypoint;
}
},
resolveId(id: string) {
if (id == virtualModuleId) {
Expand All @@ -55,11 +62,7 @@ function virtualAppEntrypoint(options?: Options): Plugin {
},
load(id: string) {
if (id === resolvedVirtualModuleId) {
if (options?.appEntrypoint) {
const appEntrypoint = options.appEntrypoint.startsWith('.')
? path.resolve(root, options.appEntrypoint)
: options.appEntrypoint;

if (appEntrypoint) {
return `\
import * as mod from ${JSON.stringify(appEntrypoint)};
Expand All @@ -80,6 +83,20 @@ export const setup = async (app) => {
return `export const setup = () => {};`;
}
},
// Ensure that Vue components reference appEntrypoint directly
// This allows Astro to assosciate global styles imported in this file
// with the pages they should be injected to
transform(code, id) {
if (!appEntrypoint) return;
if (id.endsWith('.vue')) {
const s = new MagicString(code);
s.prepend(`import "${appEntrypoint}";\n`);
return {
code: s.toString(),
map: s.generateMap()
}
}
},
};
}

Expand Down

0 comments on commit bc2a4ac

Please sign in to comment.