Skip to content

Commit

Permalink
chore: update to handle is:inline define:vars
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re committed Jul 20, 2022
1 parent e1f8332 commit f8ea331
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 37 deletions.
1 change: 0 additions & 1 deletion packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,6 @@ export interface SSRMetadata {
export interface SSRResult {
styles: Set<SSRElement>;
scripts: Set<SSRElement>;
defineVars: Set<string>;
links: Set<SSRElement>;
createAstro(
Astro: AstroGlobalPartial,
Expand Down
3 changes: 0 additions & 3 deletions packages/astro/src/core/render/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export interface RenderOptions {
origin: string;
pathname: string;
scripts: Set<SSRElement>;
defineVars: Set<string>;
resolve: (s: string) => Promise<string>;
renderers: SSRLoadedRenderer[];
route?: RouteData;
Expand All @@ -95,7 +94,6 @@ export async function render(opts: RenderOptions): Promise<Response> {
mod,
pathname,
scripts,
defineVars,
renderers,
request,
resolve,
Expand Down Expand Up @@ -141,7 +139,6 @@ export async function render(opts: RenderOptions): Promise<Response> {
request,
site,
scripts,
defineVars,
ssr,
streaming,
});
Expand Down
3 changes: 1 addition & 2 deletions packages/astro/src/core/render/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export async function render(
viteServer,
} = ssrOpts;
// Add hoisted script tags
const { scripts, defineVars } = await getScriptsForURL(filePath, astroConfig, viteServer);
const scripts = await getScriptsForURL(filePath, astroConfig, viteServer);

// Inject HMR scripts
if (isPage(filePath, astroConfig) && mode === 'development') {
Expand Down Expand Up @@ -163,7 +163,6 @@ export async function render(
let response = await coreRender({
links,
styles,
defineVars,
logging,
markdown: astroConfig.markdown,
mod,
Expand Down
19 changes: 6 additions & 13 deletions packages/astro/src/core/render/dev/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,25 @@ export async function getScriptsForURL(
filePath: URL,
astroConfig: AstroConfig,
viteServer: vite.ViteDevServer
): Promise<{ scripts: Set<SSRElement>, defineVars: Set<string> }> {
) {
const elements = new Set<SSRElement>();
const defineVars = new Set<string>();
const rootID = viteID(filePath);
let rootProjectFolder = slash(fileURLToPath(astroConfig.root));
const modInfo = viteServer.pluginContainer.getModuleInfo(rootID);
addHoistedScripts(elements, defineVars, modInfo, rootProjectFolder);
addHoistedScripts(elements, modInfo, rootProjectFolder);
for await (const moduleNode of crawlGraph(viteServer, rootID, true)) {
const id = moduleNode.id;
if (id) {
const info = viteServer.pluginContainer.getModuleInfo(id);
addHoistedScripts(elements, defineVars, info, rootProjectFolder);
addHoistedScripts(elements, info, rootProjectFolder);
}
}

return { scripts: elements, defineVars };
return elements;
}

function addHoistedScripts(
elements: Set<SSRElement>,
defineVars: Set<string>,
info: ModuleInfo | null,
rootProjectFolder: string
) {
Expand All @@ -44,12 +42,7 @@ function addHoistedScripts(
const astro = info?.meta?.astro as AstroPluginMetadata['astro'];
for (let i = 0; i < astro.scripts.length; i++) {
const scriptId = `${id}?astro&type=script&index=${i}&lang.ts`;
const script = astro.scripts[i];
if (script.type === 'define:vars') {
defineVars.add(`/@fs${scriptId.replace('/@fs', '')}`);
} else {
const element = createModuleScriptElementWithSrc(scriptId);
elements.add(element);
}
const element = createModuleScriptElementWithSrc(scriptId);
elements.add(element);
}
}
2 changes: 0 additions & 2 deletions packages/astro/src/core/render/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export interface CreateResultArgs {
site: string | undefined;
links?: Set<SSRElement>;
scripts?: Set<SSRElement>;
defineVars?: Set<string>;
styles?: Set<SSRElement>;
request: Request;
}
Expand Down Expand Up @@ -141,7 +140,6 @@ export function createResult(args: CreateResultArgs): SSRResult {
styles: args.styles ?? new Set<SSRElement>(),
scripts: args.scripts ?? new Set<SSRElement>(),
links: args.links ?? new Set<SSRElement>(),
defineVars: args.defineVars ?? new Set<string>(),
/** This function returns the `Astro` faux-global */
createAstro(
astroGlobal: AstroGlobalPartial,
Expand Down
12 changes: 1 addition & 11 deletions packages/astro/src/runtime/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,17 +593,7 @@ export function defineStyleVars(defs: Record<any, any>[]) {
}

// Adds variables to an inline script.
export async function defineScriptVars(vars: Record<any, any>, info?: { file: string, index: number }, result?: SSRResult) {
if (typeof info !== 'undefined' && typeof result !== 'undefined') {
let match: string | undefined;
for (const script of result.defineVars) {
if (script.replace('/@fs', '').startsWith(info.file) && script.includes(`index=${info.index}`)) {
match = await result.resolve(script);
break;
}
}
return markHTMLString(`import("${match}").then(res => res.default(${JSON.stringify(serializeProps(vars))}))`)
}
export async function defineScriptVars(vars: Record<any, any>) {
let output = '';
for (const [key, value] of Object.entries(vars)) {
output += `let ${key} = ${JSON.stringify(value)};\n`;
Expand Down
5 changes: 0 additions & 5 deletions packages/astro/src/vite-plugin-astro/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,6 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu
result.code = `import "${src}"`;
break;
}
case 'define:vars': {
let { code, map } = hoistedScript
result.code = appendSourceMap(code, map);
break;
}
}

return result
Expand Down

0 comments on commit f8ea331

Please sign in to comment.