2023-03-28 14:14:54 +05:30
|
|
|
import type { PartytownConfig } from '@builder.io/partytown/integration';
|
2023-03-28 08:47:15 +00:00
|
|
|
import { partytownSnippet } from '@builder.io/partytown/integration';
|
2022-06-23 09:03:43 -04:00
|
|
|
import { copyLibFiles, libDirPath } from '@builder.io/partytown/utils';
|
2023-07-03 05:59:43 -07:00
|
|
|
import type { AstroIntegration } from 'astro';
|
2022-03-18 15:35:45 -07:00
|
|
|
import { createRequire } from 'module';
|
2023-07-18 02:17:59 +02:00
|
|
|
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';
|
2022-03-18 15:35:45 -07:00
|
|
|
const resolve = createRequire(import.meta.url).resolve;
|
|
|
|
|
2023-10-03 17:16:49 +02:00
|
|
|
export type PartytownOptions = {
|
2023-05-25 13:54:59 +05:30
|
|
|
config?: PartytownConfig;
|
|
|
|
};
|
2022-05-16 19:32:05 +01:00
|
|
|
|
2023-01-23 20:13:06 +05:30
|
|
|
function appendForwardSlash(str: string) {
|
|
|
|
return str.endsWith('/') ? str : str + '/';
|
|
|
|
}
|
|
|
|
|
2023-05-25 13:54:59 +05:30
|
|
|
export default function createPlugin(options?: PartytownOptions): AstroIntegration {
|
2022-03-18 15:35:45 -07:00
|
|
|
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);`;
|
2022-03-18 15:35:45 -07:00
|
|
|
return {
|
|
|
|
name: '@astrojs/partytown',
|
|
|
|
hooks: {
|
|
|
|
'astro:config:setup': ({ config: _config, command, injectScript }) => {
|
2023-01-23 20:13:06 +05:30
|
|
|
const lib = `${appendForwardSlash(_config.base)}~partytown/`;
|
2023-03-28 14:14:54 +05:30
|
|
|
const partytownConfig = {
|
|
|
|
lib,
|
2023-10-27 11:54:41 +02:00
|
|
|
...options?.config,
|
2023-03-28 14:14:54 +05:30
|
|
|
debug: options?.config?.debug ?? command === 'dev',
|
|
|
|
};
|
|
|
|
partytownSnippetHtml = partytownSnippet(partytownConfig);
|
2024-01-11 19:26:49 +01:00
|
|
|
partytownSnippetHtml += SELF_DESTRUCT_ON_VIEW_TRANSITION;
|
2022-03-18 15:35:45 -07:00
|
|
|
injectScript('head-inline', partytownSnippetHtml);
|
|
|
|
},
|
|
|
|
'astro:server:setup': ({ server }) => {
|
2022-11-17 13:46:20 -06:00
|
|
|
const lib = `/~partytown/`;
|
2022-03-18 15:35:45 -07:00
|
|
|
server.middlewares.use(
|
|
|
|
sirv(partytownLibDirectory, {
|
2022-05-24 17:44:40 -03:00
|
|
|
mount: lib,
|
2022-03-18 15:35:45 -07:00
|
|
|
dev: true,
|
|
|
|
etag: true,
|
|
|
|
extensions: [],
|
|
|
|
})
|
|
|
|
);
|
|
|
|
},
|
2022-04-06 17:20:58 -03:00
|
|
|
'astro:build:done': async ({ dir }) => {
|
|
|
|
await copyLibFiles(fileURLToPath(new URL('~partytown', dir)), {
|
2023-03-29 09:18:25 -03:00
|
|
|
debugDir: options?.config?.debug ?? false,
|
2022-04-02 13:29:59 -05:00
|
|
|
});
|
2022-03-18 15:35:45 -07:00
|
|
|
},
|
2022-06-23 09:03:43 -04: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 09:03:43 -04:00
|
|
|
}
|
2022-06-23 13:05:55 +00:00
|
|
|
},
|
2022-03-18 15:35:45 -07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|