Skip to content

Commit

Permalink
feat: fallback to Rollup/Vite output directory (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann authored Oct 4, 2023
1 parent 9b92969 commit 1e0468e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
4 changes: 1 addition & 3 deletions docs/sveltekit.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ import { partytownVite } from '@builder.io/partytown/utils'
const config = {
plugins: [
sveltekit(),
partytownVite({
dest: join(process.cwd(), '.svelte-kit/output/client/~partytown')
})
partytownVite()
]
}

Expand Down
34 changes: 16 additions & 18 deletions src/utils/rollup.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { isAbsolute } from 'path';
import { isAbsolute, join } from 'node:path';
import type { Plugin } from 'rollup';
import { copyLibFiles } from './copy-lib-files';

/** @public */
export interface PartytownRollupOptions {
/** An absolute path to the destination directory where the lib files should be copied. */
dest: string;
dest?: string;

/**
* When `debug` is set to `false`, the `lib/debug` directory will not be copied.
Expand All @@ -21,26 +22,23 @@ export interface PartytownRollupOptions {
*
* @public
*/
export function partytownRollup(opts: PartytownRollupOptions) {
export function partytownRollup(opts?: PartytownRollupOptions) {
opts = opts || ({} as any);

if (typeof opts.dest !== 'string' || opts.dest.length === 0) {
throw new Error(`Partytown plugin must have "dest" property.`);
}

if (!isAbsolute(opts.dest)) {
throw new Error(`Partytown plugin "dest" property must be an absolute path.`);
}

let hasCopied = false;

const plugin = {
const plugin: Plugin = {
name: 'rollup-plugin-partytown',
async writeBundle() {
if (!hasCopied) {
await copyLibFiles(opts.dest, { debugDir: opts.debug });
hasCopied = true;
async writeBundle(rollupOpts) {
const dir = opts?.dest || (rollupOpts.dir ? join(rollupOpts.dir, '~partytown') : undefined);

if (typeof dir !== 'string') {
throw new Error(`A destination directory must be specified either via the Partytown "dest" option or Rollup output dir option.`);
}

if (!isAbsolute(dir)) {
throw new Error(`Partytown plugin "dest" property must be an absolute path.`);
}

await copyLibFiles(dir, { debugDir: opts?.debug });
},
};

Expand Down

1 comment on commit 1e0468e

@vercel
Copy link

@vercel vercel bot commented on 1e0468e Oct 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.