super-productivity/packages/plugin-dev/doc-mode/scripts/build.js
Johannes Millan 3443bcacd0 refactor(plugins): rename document-mode to doc-mode
Renames the bundled plugin's id (`document-mode` → `doc-mode`), display
name (`Document Mode (Alpha)` → `Doc Mode (Alpha)`), package name,
directory, and asset path. No migration: the plugin has never been
published, so there is no on-disk user data to preserve.

Touched everywhere the id was hardcoded:
- Plugin manifest, package.json, build/deploy/test scripts, log prefixes
- Host BUNDLED_PLUGIN_PATHS entry (src/app/plugins/plugin.service.ts)
- Host comments and test fixtures (plugin-hooks, plugin-persistence-key,
  plugin-persistence.model, conflict-resolution.service.spec)
- E2E spec filenames + PLUGIN_ID constant + label assertions
- build-all.js plugin orchestrator
- build/release-notes.md user-facing entry

Test pass: 88/88 plugin specs, 8/8 plugin-hooks, 132/132
conflict-resolution, 15/15 plugin-persistence-key.util.
Bundle redeployed to src/assets/bundled-plugins/doc-mode (gitignored).

The old `src/assets/bundled-plugins/document-mode/` dir is gitignored and
left on disk after this commit; the host loader no longer references it,
so it's inert. Remove with `rm -rf src/assets/bundled-plugins/document-mode`.
2026-05-27 16:48:59 +02:00

73 lines
2.4 KiB
JavaScript

#!/usr/bin/env node
/* eslint-disable @typescript-eslint/no-require-imports */
const fs = require('fs');
const path = require('path');
const { build } = require('esbuild');
const ROOT_DIR = path.join(__dirname, '..');
const SRC_DIR = path.join(ROOT_DIR, 'src');
const DIST_DIR = path.join(ROOT_DIR, 'dist');
async function buildPlugin() {
console.log('Building doc-mode plugin with esbuild...');
if (fs.existsSync(DIST_DIR)) {
fs.rmSync(DIST_DIR, { recursive: true });
}
fs.mkdirSync(DIST_DIR);
// Background script — runs in the host page context, registers UI + hooks.
console.log('Building plugin.js (background)...');
await build({
entryPoints: [path.join(SRC_DIR, 'background.ts')],
bundle: true,
outfile: path.join(DIST_DIR, 'plugin.js'),
platform: 'browser',
target: 'es2020',
format: 'iife',
globalName: 'DocumentModePlugin',
logLevel: 'info',
minify: true,
sourcemap: false,
});
// Editor bundle — runs inside the iframe, hosts TipTap.
console.log('Building editor.js (iframe)...');
await build({
entryPoints: [path.join(SRC_DIR, 'ui', 'editor.ts')],
bundle: true,
outfile: path.join(DIST_DIR, 'editor.js'),
platform: 'browser',
target: 'es2020',
format: 'iife',
logLevel: 'info',
minify: true,
sourcemap: false,
});
// The iframe is loaded via blob URL, so relative <script src="editor.js">
// does not resolve. Inline the bundle into index.html instead.
const editorJs = fs.readFileSync(path.join(DIST_DIR, 'editor.js'), 'utf8');
const rawHtml = fs.readFileSync(path.join(SRC_DIR, 'ui', 'index.html'), 'utf8');
// Function-form replacement so `$&`, ``$`` ``, etc. in the bundle aren't
// interpreted as replace() back-references. Also escape any literal
// `</script>` inside the JS so the HTML parser doesn't close the tag early.
const safeEditorJs = editorJs.replace(/<\/script>/gi, '<\\/script>');
const inlinedHtml = rawHtml.replace(
/<script src="editor\.js"><\/script>/,
() => `<script>${safeEditorJs}</script>`,
);
fs.writeFileSync(path.join(DIST_DIR, 'index.html'), inlinedHtml);
fs.copyFileSync(
path.join(SRC_DIR, 'manifest.json'),
path.join(DIST_DIR, 'manifest.json'),
);
fs.copyFileSync(path.join(SRC_DIR, 'icon.svg'), path.join(DIST_DIR, 'icon.svg'));
console.log('\nBuild complete! Output in dist/');
}
buildPlugin().catch((err) => {
console.error('Build failed:', err);
process.exit(1);
});