Skip to content

Commit

Permalink
Provide app to avoid using the deprecated global app
Browse files Browse the repository at this point in the history
For users, nothing needs to be changed.

Signed-off-by: William So <polyipseity@gmail.com>
  • Loading branch information
polyipseity committed Sep 29, 2023
1 parent 71e6b15 commit de069e6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-hotels-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"obsidian-modules": patch
---

Provide `app` to avoid using the deprecated global `app`. For users, nothing needs to be changed.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ Contributions are welcome!

The todos here, ordered alphabetically, are things planned for the plugin. There are no guarantees that they will be completed. However, we are likely to accept contributions for them.

- Provide `app` to avoid using the deprecated global `app`.
- Add startup modules.
- User-defined module aliases.
- Add bare module transformation support for more CDNs such as <https://cdn.jsdelivr.net>.
Expand Down
6 changes: 6 additions & 0 deletions sources/@types/obsidian-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ declare module "obsidian-modules" {
*/
readonly aliases: Map<string, Set<string>>

/**
* Associated {@link App}.
*/
readonly app: App

/**
* Context for loading modules.
*/
Expand Down Expand Up @@ -252,5 +257,6 @@ declare module "obsidian-modules" {
}
}
import type { } from "obsidian-modules"
import type { App } from "obsidian"
import type { AsyncOrSync } from "ts-essentials"
import type { EventEmitterLite } from "@polyipseity/obsidian-plugin-library"
20 changes: 14 additions & 6 deletions sources/require/require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
aroundIdentityFactory,
attachFunctionSourceMap,
clearProperties,
escapeJavaScriptString as escJSStr,
launderUnchecked,
patchWindows,
promisePromise,
Expand Down Expand Up @@ -228,7 +229,12 @@ function createRequire(
})
}
preload(cleanup, rd, context)
new self0.Function("module", "exports", "process", compiledSyncCode ??
new self0.Function(
"module",
"exports",
"process",
"app",
compiledSyncCode ??
attachFunctionSourceMap(
self0.Function,
`${PRECOMPILE_SYNC_PREFIX}${code}`,
Expand All @@ -240,10 +246,11 @@ function createRequire(
file: id,
sourceRoot: `${sourceRoot}${sourceRoot && "/"}${id}`,
},
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
))(module, module.exports, self0.process ??
// eslint-disable-next-line @typescript-eslint/naming-convention
{ env: { NODE_DEV: "production" } })
),
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
)(module, module.exports, self0.process ??
// eslint-disable-next-line @typescript-eslint/naming-convention
{ env: { NODE_DEV: "production" } }, ret.app)
const { exports } = module
if (isObject(exports)) {
Reflect.defineProperty(exports, Symbol.toStringTag, {
Expand All @@ -265,6 +272,7 @@ function createRequire(
[REQUIRE_TAG]: true,
aliased: new Map(),
aliases: new Map(),
app: ctx.app,
cache: new Map(),
context: { cwds: [], parents: [] } satisfies Context,
dependants: new Map(),
Expand Down Expand Up @@ -305,7 +313,7 @@ function createRequire(
"export let module={exports:Object.defineProperty({},Symbol.toStringTag,{configurable:!0,enumerable:!1,value:\"Module\",writable:!0})}",
"let{exports}=module",
"let{process}=self;process??={env:{NODE_DEV:\"production\"}}",
"",
`let{app}=self[${escJSStr(ctx.settings.value.requireName)}]`,
].join(";")
: "",
url = URL.createObjectURL(new Blob(
Expand Down

0 comments on commit de069e6

Please sign in to comment.