Skip to content

Commit

Permalink
Fix importing from external links not working with custom require name
Browse files Browse the repository at this point in the history
Signed-off-by: William So <polyipseity@gmail.com>
  • Loading branch information
polyipseity committed Sep 30, 2023
1 parent 58de366 commit dbef51c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-rice-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"obsidian-modules": patch
---

Fix importing from external links not working with custom require name.
12 changes: 10 additions & 2 deletions sources/require/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
codePoint,
dynamicRequire,
dynamicRequireSync,
escapeJavaScriptString as escJSStr,
isNonNil,
} from "@polyipseity/obsidian-plugin-library"
import type { Context, Resolve, Resolved } from "obsidian-modules"
Expand Down Expand Up @@ -696,7 +697,7 @@ export class ExternalLinkResolve
readonly [string, ExternalLinkResolve.Identity]> {
const href = await this.anormalizeURL(id, cwd)
if (href === null) { return [null, null] }
const { tsTranspile } = this
const { context: { settings }, tsTranspile } = this
let identity = this.identities.get(href)
if (identity === void 0 || identity === "await") {
const awaiting = identity === "await"
Expand Down Expand Up @@ -750,7 +751,14 @@ export class ExternalLinkResolve
const { code, requires } =
await (await this.workerPool).exec<typeof parseAndRewriteRequire>(
"parseAndRewriteRequire",
[{ code: ret.code, href }],
[
{
code: ret.code,
href,
requireExpression:
`self[${escJSStr(settings.value.requireName)}]`,
},
],
)
// eslint-disable-next-line require-atomic-updates
ret.code = code
Expand Down
8 changes: 4 additions & 4 deletions sources/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export async function parseAndRewriteRequire(
input: parseAndRewriteRequire.Input,
): Promise<parseAndRewriteRequire.Output> {
const { importable, escapeJavaScriptString } = await library,
{ OPTIONS, SELF_REQUIRE_EXPRESSION } = parseAndRewriteRequire,
{ OPTIONS } = parseAndRewriteRequire,
reqExpr = parseExpressionAt(input.requireExpression, 0, OPTIONS),
requires: string[] = [],
tree = parse(input.code, OPTIONS)
simple(tree, {
Expand Down Expand Up @@ -86,7 +87,7 @@ export async function parseAndRewriteRequire(
}
const value2 = normalizeURL(`${prefix}${value}`, input.href)
if (value2 === null) { return }
node2.callee = SELF_REQUIRE_EXPRESSION
node2.callee = reqExpr
arg0.raw = escapeJavaScriptString(value2)
requires.push(arg0.value = value2)
},
Expand All @@ -110,11 +111,10 @@ export namespace parseAndRewriteRequire {
ranges: false,
sourceType: "script",
}
export const SELF_REQUIRE_EXPRESSION =
parseExpressionAt("self.require", 0, OPTIONS)
export interface Input {
readonly code: string
readonly href: string
readonly requireExpression: string
}
export interface Output {
readonly code: string
Expand Down

0 comments on commit dbef51c

Please sign in to comment.