0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-20 22:12:38 -05:00
astro/packages/integrations/partytown/src/index.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

66 lines
2.2 KiB
TypeScript
Raw Normal View History

import type { PartytownConfig } from '@builder.io/partytown/integration';
2023-03-28 08:47:15 +00:00
import { partytownSnippet } from '@builder.io/partytown/integration';
import { copyLibFiles, libDirPath } from '@builder.io/partytown/utils';
import type { AstroIntegration } from 'astro';
import { createRequire } from 'module';
import * as fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
2022-06-06 16:49:53 +00:00
import sirv from './sirv.js';
const resolve = createRequire(import.meta.url).resolve;
export type PartytownOptions = {
config?: PartytownConfig;
};
function appendForwardSlash(str: string) {
return str.endsWith('/') ? str : str + '/';
}
export default function createPlugin(options?: PartytownOptions): AstroIntegration {
let partytownSnippetHtml: string;
const partytownEntrypoint = resolve('@builder.io/partytown/package.json');
const partytownLibDirectory = path.resolve(partytownEntrypoint, '../lib');
2024-01-11 18:28:28 +00:00
const SELF_DESTRUCT_ON_VIEW_TRANSITION = `;((d,s)=>(s=d.currentScript,d.addEventListener('astro:before-swap',()=>s.remove(),{once:true})))(document);`;
return {
name: '@astrojs/partytown',
hooks: {
'astro:config:setup': ({ config: _config, command, injectScript }) => {
const lib = `${appendForwardSlash(_config.base)}~partytown/`;
const partytownConfig = {
lib,
...options?.config,
debug: options?.config?.debug ?? command === 'dev',
};
partytownSnippetHtml = partytownSnippet(partytownConfig);
partytownSnippetHtml += SELF_DESTRUCT_ON_VIEW_TRANSITION;
injectScript('head-inline', partytownSnippetHtml);
},
'astro:server:setup': ({ server }) => {
const lib = `/~partytown/`;
server.middlewares.use(
sirv(partytownLibDirectory, {
mount: lib,
dev: true,
etag: true,
extensions: [],
})
);
},
'astro:build:done': async ({ dir }) => {
await copyLibFiles(fileURLToPath(new URL('~partytown', dir)), {
debugDir: options?.config?.debug ?? false,
Migrate to new config (#2962) * wip: config migration * fix: formatting * refactor: projectRoot -> root * refactor: pageUrlFormat -> format * refactor: buildOptions.site -> site * refactor: public -> publicDir * refactor: dist -> outDir * refactor: styleOptions -> style * fix: some dist tests -> outDir * refactor: remove legacyBuild (with TODOs) * refactor: more legacyBuild cleanup * refactor: server host and port * fix: remove experimentalStaticBuild CLI flag * refactor: src -> srcDir * refactor: devOptions.trailing -> trailing * refactor: remove sitemap + related flags * refactor: experimentalSSR -> experimental.ssr * fix: last devOptions * refactor: drafts -> markdown.drafts * fix: TS error on port as const * refactor: remove pages * refactor: more --project-root updates * refactor: markdownOptions -> markdown * fix: remaining type errors * feat: update AstroUserConfig * refactor: update CLI flag mapper + server mapper * fix: loadFixture projectRoot * fix: merge CLI flags before validating / transforming * wip: attempt to fix bad createRouteManifest config * refactor: combine config.base and config.site * fix: skip route manifest test for now * fix: site and base handling * refactor: update failing config testes * fix: build failure * feat: update config types with migration help * chore: update types * fix(deno): update deno fixture * chore: remove config migration logic * chore: remove logLevel * chore: clean-up config types * chore: update config warning * chore: add changeset * Sitemap Integration (#2965) * feat: add sitemap filter config option * feat: add canonicalURL sitemap config option * docs: update sitemap README * fix: update for new config * fix: filter not being applied * chore: changeset Co-authored-by: bholmesdev <hey@bholmes.dev> * fred pass * fix: Astro.resolve typo * fix: public => publicDir Co-authored-by: bholmesdev <hey@bholmes.dev> Co-authored-by: Fred K. Schott <fkschott@gmail.com>
2022-04-02 13:29:59 -05:00
});
},
'astro:build:ssr': async ({ manifest }) => {
const dirpath = libDirPath({ debugDir: false });
const files = await fs.promises.readdir(dirpath);
2022-06-23 13:05:55 +00:00
for (const file of files) {
if (file === 'debug') continue;
manifest.assets.push(`/~partytown/${file}`);
}
2022-06-23 13:05:55 +00:00
},
},
};
}