fix(plugin-sdk): make require('trek-plugin-sdk') actually resolve everywhere

A freshly scaffolded plugin could not load anywhere: the npm package is
ESM-only (no require condition in its exports map), so the scaffold's
require('trek-plugin-sdk') threw ERR_PACKAGE_PATH_NOT_EXPORTED under
`trek-plugin dev` - and the runtime injection the wiki promised for the
plugin child never existed, so a packed plugin (node_modules stripped)
crashed with MODULE_NOT_FOUND after a real install.

- plugin child: inject a frozen {definePlugin, PLUGIN_API_VERSION} shim
  for require('trek-plugin-sdk'); subpaths fail with a pointed error
- trek-plugin dev: inject the exact same shim, so a fresh scaffold runs
  with zero npm install and dev parity with production holds
- npm package: ship a real CommonJS build (dist/cjs + require export
  conditions) so the installed package also requires cleanly on Node 18+
- create: scaffold a package.json (type commonjs, SDK as devDependency,
  npx scripts); print resolvable `npx trek-plugin-sdk ...` hints
- wiki: package.json in the scaffold tree + publishing checklist, and
  document the zero-install dev flow
This commit is contained in:
Maurice
2026-07-04 19:57:43 +02:00
committed by Maurice
parent 8b801af94a
commit d110f8f0ce
12 changed files with 185 additions and 14 deletions
+35
View File
@@ -105,6 +105,15 @@ describe('scaffold + validate CLIs', () => {
expect(r.warnings.some((w) => /placeholder|screenshot/.test(w))).toBe(true); // README is the unfilled template
});
it('scaffolds a CommonJS package.json with the SDK as a devDependency only', () => {
scaffold('my-widget', 'widget', tmp);
const pkg = JSON.parse(fs.readFileSync(path.join(tmp, 'my-widget', 'package.json'), 'utf8'));
expect(pkg.type).toBe('commonjs');
expect(pkg.private).toBe(true);
expect(pkg.devDependencies['trek-plugin-sdk']).toMatch(/^\^\d/);
expect(pkg.dependencies).toBeUndefined(); // runtime deps are the author's call; the SDK never is one
});
it('applies author, description, and permissions from options', () => {
scaffold('opt-plug', 'integration', tmp, { author: 'Jane', description: 'Does X', permissions: ['db:own', 'db:read:trips'] });
const m = JSON.parse(fs.readFileSync(path.join(tmp, 'opt-plug', 'trek-plugin.json'), 'utf8'));
@@ -122,6 +131,32 @@ describe('scaffold + validate CLIs', () => {
});
});
describe('dev-server SDK injection', () => {
let tmp: string;
beforeEach(() => { tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'inject-')); });
afterEach(() => { fs.rmSync(tmp, { recursive: true, force: true }); });
it('makes require(trek-plugin-sdk) resolve from the package itself — no npm install', async () => {
const { installSdkInjection } = await import('../src/cli/dev.js');
const { createRequire } = await import('node:module');
installSdkInjection();
// A scaffold-shaped CJS entry that requires the SDK without any node_modules.
// The injected surface must MATCH the prod child shim: definePlugin +
// PLUGIN_API_VERSION only, subpaths throw the same pointed error.
fs.writeFileSync(path.join(tmp, 'entry.cjs'),
"const sdkShim = require('trek-plugin-sdk');\n" +
"let testingError = '';\n" +
"try { require('trek-plugin-sdk/testing'); } catch (e) { testingError = e.message; }\n" +
'module.exports = { def: sdkShim.definePlugin({ routes: [] }), api: sdkShim.PLUGIN_API_VERSION, keys: Object.keys(sdkShim).sort(), testingError };\n');
const req = createRequire(path.join(tmp, 'entry.cjs'));
const mod = req(path.join(tmp, 'entry.cjs')) as { def: unknown; api: number; keys: string[]; testingError: string };
expect(mod.def).toEqual({ routes: [] });
expect(mod.api).toBe(1);
expect(mod.keys).toEqual(['PLUGIN_API_VERSION', 'definePlugin']); // prod parity — no dev-only extras
expect(mod.testingError).toMatch(/build\/test-time/);
});
});
describe('reference plugin (examples/koffi)', () => {
const dir = path.resolve(import.meta.dirname, '..', 'examples', 'koffi');