Files
insights-js/packages/rrweb/vite.config.default.ts
Arnaud HillenandGitHub 029a77b3c2 chore: move posthog-rrweb into the monorepo (#3510)
* chore: move posthog-rrweb into the monorepo

Imports the posthog-rrweb fork (currently published as v0.0.60) into
packages/rrweb/ so that the rrweb packages release from this repo via
the existing changesets pipeline. This collapses the current two-step
publish flow (posthog-rrweb -> npm -> posthog-js) into a single
monorepo release.

Scope of the move:
- Snapshot taken from posthog-rrweb@main; rrvideo, rrweb-player and
  web-extension are intentionally dropped because we do not consume
  them from posthog-js.
- The seven packages we publish today continue to publish from this
  repo: @posthog/rrweb, @posthog/rrweb-types, @posthog/rrweb-utils,
  @posthog/rrdom, @posthog/rrweb-snapshot, @posthog/rrweb-record and
  @posthog/rrweb-plugin-console-record.
- Internal-only packages (rrdom-nodejs, @posthog/rrweb-replay,
  @posthog/rrweb-all, @posthog/rrweb-packer, the experimental plugins)
  are listed under changesets ignore.
- packages/browser now depends on @posthog/rrweb-record,
  @posthog/rrweb-plugin-console-record and @posthog/rrweb-types via
  workspace:* instead of pinned npm versions.

Tooling/config updates:
- pnpm-workspace.yaml gains packages/rrweb/* and
  packages/rrweb/plugins/* globs and a small catalog for vite,
  vite-plugin-dts and esbuild-plugin-umd-wrapper.
- turbo.json picks up vite.config.* as build inputs and adds
  prepublish / check-types tasks used by the rrweb subpackages.
- Root devDependencies hoist esbuild, esbuild-plugin-umd-wrapper and
  rollup-plugin-visualizer so vite builds resolve correctly.
- .npmrc adds a few trust-policy-exclude entries for transitive deps
  brought in by the rrweb tree that lost provenance attestations.
- release.yml extends the publish matrix to the seven publishable
  rrweb packages.
- library-ci.yml adds an rrweb job (vitest + puppeteer under xvfb)
  that only runs when packages/rrweb/** changes.

No runtime behavior changes are intended: the recorder bundle in
packages/browser is byte-identical to main (verified locally), and
the published rrweb packages get a single patch bump via the
accompanying changeset.


* stop using rrweb npm package and point imports to local rrweb

* fix tests

* fix tests

* fix docker build

* fix docker build

* silence false positives

* fix for tue's comment

* pin package version

* add minimal permissions to silence alert
2026-05-05 11:56:21 +01:00

164 lines
4.6 KiB
TypeScript

/// <reference types="vite/client" />
import dts from 'vite-plugin-dts';
import { copyFileSync } from 'node:fs';
import { defineConfig, LibraryOptions, LibraryFormats, Plugin } from 'vite';
import { build, Format } from 'esbuild';
import { resolve } from 'path';
import { umdWrapper } from 'esbuild-plugin-umd-wrapper';
import * as fs from 'node:fs';
import { visualizer } from 'rollup-plugin-visualizer';
// don't empty out dir if --watch flag is passed
const emptyOutDir = !process.argv.includes('--watch');
function minifyAndUMDPlugin({
name,
outDir,
}: {
name: LibraryOptions['name'];
outDir: string;
}): Plugin {
return {
name: 'minify-plugin',
async writeBundle(outputOptions, bundle) {
for (const file of Object.values(bundle)) {
if (
file.type === 'asset' &&
(file.fileName.endsWith('.cjs.map') || file.fileName.endsWith('.css'))
) {
const isCSS = file.fileName.endsWith('.css');
const inputFilePath = resolve(
outputOptions.dir!,
file.fileName,
).replace(/\.map$/, '');
const baseFileName = file.fileName.replace(
/(\.cjs|\.css)(\.map)?$/,
'',
);
const outputFilePath = resolve(outputOptions.dir!, baseFileName);
// console.log(outputFilePath, 'minifying', file.fileName);
if (isCSS) {
await buildFile({
input: inputFilePath,
output: `${outputFilePath}.min.css`,
minify: true,
isCss: true,
outDir,
});
} else {
await buildFile({
name,
input: inputFilePath,
output: `${outputFilePath}.umd.cjs`,
minify: false,
isCss: false,
outDir,
});
await buildFile({
name,
input: inputFilePath,
output: `${outputFilePath}.umd.min.cjs`,
minify: true,
isCss: false,
outDir,
});
}
}
}
},
};
}
async function buildFile({
name,
input,
output,
minify,
isCss,
outDir,
}: {
name?: LibraryOptions['name'];
input: string;
output: string;
outDir: string;
minify: boolean;
isCss: boolean;
}) {
await build({
entryPoints: [input],
outfile: output,
minify,
sourcemap: true,
format: isCss ? undefined : ('umd' as Format),
target: isCss ? undefined : 'es2017',
treeShaking: !isCss,
plugins: [
umdWrapper({
libraryName: name,
}),
],
});
const filename = output.replace(new RegExp(`^.+/(${outDir}/)`), '$1');
console.log(filename);
console.log(`${filename}.map`);
}
export default function (
entry: LibraryOptions['entry'],
name: LibraryOptions['name'],
options?: { outputDir?: string; fileName?: string; plugins?: Plugin[] },
) {
const { fileName, outputDir: outDir = 'dist', plugins = [] } = options || {};
let formats: LibraryFormats[] = ['es', 'cjs'];
return defineConfig(() => ({
build: {
// See https://vitejs.dev/guide/build.html#library-mode
lib: {
cssFileName: 'style',
entry,
name,
fileName,
// TODO: turn on `umd` for rrweb when https://github.com/schummar/vite/tree/feature/libMultiEntryUMD gets merged
// More info: https://github.com/vitejs/vite/pull/7047#issuecomment-1288080855
// formats: ['es', 'umd', 'cjs'],
formats,
},
outDir,
emptyOutDir,
// Leaving this unminified so you can see what exactly gets included in
// the bundles
minify: false,
sourcemap: true,
},
plugins: [
dts({
insertTypesEntry: true,
rollupTypes: true,
afterBuild: (emittedFiles: Map<string, string>) => {
// To pass publint (`npm x publint@latest`) and ensure the
// package is supported by all consumers, we must export types that are
// read as ESM. To do this, there must be duplicate types with the
// correct extension supplied in the package.json exports field.
const files: string[] = Array.from(emittedFiles.keys());
files.forEach((file) => {
const ctsFile = file.replace('.d.ts', '.d.cts');
copyFileSync(file, ctsFile);
});
},
}),
minifyAndUMDPlugin({ name, outDir }),
visualizer({
filename: resolve(__dirname, name + '-bundle-analysis.html'), // Path for the HTML report
open: false, // don't Automatically open the report in the browser
}),
...plugins,
],
}));
}