-
Notifications
You must be signed in to change notification settings - Fork 805
fix(memos-local-openclaw): ship compiled JS for OpenClaw plugin loader (#1619) #1622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fancyboi999
wants to merge
2
commits into
MemTensor:main
Choose a base branch
from
fancyboi999:fix/openclaw-plugin-1619-publish-compiled-output
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| #!/usr/bin/env node | ||
| "use strict"; | ||
|
|
||
| /** | ||
| * Post-build: append `.js` (or `/index.js`) to every relative ESM import in dist/. | ||
| * | ||
| * Why: tsconfig uses `moduleResolution: "bundler"` so source files don't need | ||
| * `.js` suffixes — but Node's native ESM loader requires explicit suffixes. | ||
| * OpenClaw's plugin loader runs the emitted JS through the standard loader, | ||
| * so we rewrite the imports here instead of polluting source with `.js`. | ||
| */ | ||
|
|
||
| const fs = require("fs"); | ||
| const path = require("path"); | ||
|
|
||
| const DIST = path.resolve(__dirname, "..", "dist"); | ||
|
|
||
| // Matches: import ... from "X" | export ... from "X" | import("X") | ||
| const IMPORT_RE = /(\bfrom\s*['"]|\bimport\s*\(\s*['"])(\.[^'"]+)(['"])/g; | ||
|
|
||
| function rewriteSpec(spec, fileDir) { | ||
| if (/\.(m?js|cjs|json)$/.test(spec)) return spec; | ||
| const abs = path.resolve(fileDir, spec); | ||
| if (fs.existsSync(abs + ".js")) return spec + ".js"; | ||
| if (fs.existsSync(path.join(abs, "index.js"))) return spec.replace(/\/?$/, "/index.js"); | ||
| return spec; | ||
| } | ||
|
|
||
| let touched = 0; | ||
| function walk(dir) { | ||
| for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { | ||
| const full = path.join(dir, entry.name); | ||
| if (entry.isDirectory()) { walk(full); continue; } | ||
| if (!entry.name.endsWith(".js")) continue; | ||
| const src = fs.readFileSync(full, "utf8"); | ||
| let changed = false; | ||
| const out = src.replace(IMPORT_RE, (m, head, spec, tail) => { | ||
| const next = rewriteSpec(spec, path.dirname(full)); | ||
| if (next !== spec) { changed = true; return head + next + tail; } | ||
| return m; | ||
| }); | ||
| if (changed) { fs.writeFileSync(full, out); touched++; } | ||
| } | ||
| } | ||
|
|
||
| if (!fs.existsSync(DIST)) { | ||
| console.error("[fix-esm-imports] dist/ not found — run tsc first"); | ||
| process.exit(1); | ||
| } | ||
| walk(DIST); | ||
| console.log(`[fix-esm-imports] rewrote imports in ${touched} file(s)`); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| declare module "openclaw/plugin-sdk" { | ||
| export interface OpenClawPluginApi { | ||
| registerTool(...args: any[]): void; | ||
| registerHook(...args: any[]): void; | ||
| registerMemoryCapability(...args: any[]): void; | ||
| registerService(...args: any[]): void; | ||
| getConfig(): any; | ||
| getLogger(): any; | ||
| config: any; | ||
| logger: any; | ||
| [key: string]: any; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import * as fs from "node:fs"; | ||
| import * as path from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| /** | ||
| * Resolve the plugin's installation root by walking up from the caller's file | ||
| * until a `package.json` whose name matches `memos-local-openclaw-plugin` is found. | ||
| * | ||
| * Necessary because the build emits to `dist/` with `rootDir: "."`, so | ||
| * compiled files live one extra level deep than their sources. Hard-coded | ||
| * `../../` paths break across dev (src/) vs published (dist/src/) layouts. | ||
|
fancyboi999 marked this conversation as resolved.
|
||
| */ | ||
| export function findPluginRoot(importMetaUrl: string): string { | ||
| let dir = path.dirname(fileURLToPath(importMetaUrl)); | ||
| for (let i = 0; i < 8; i++) { | ||
| const pkgPath = path.join(dir, "package.json"); | ||
| if (fs.existsSync(pkgPath)) { | ||
| try { | ||
| const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8")); | ||
| if (typeof pkg.name === "string" && pkg.name.includes("memos-local")) return dir; | ||
| } catch { /* keep walking */ } | ||
| } | ||
| const parent = path.dirname(dir); | ||
| if (parent === dir) break; | ||
| dir = parent; | ||
| } | ||
| throw new Error(`findPluginRoot: could not locate plugin root from ${importMetaUrl}`); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.