diff --git a/.changeset/fast-hotels-know.md b/.changeset/fast-hotels-know.md new file mode 100644 index 00000000..9f3f499f --- /dev/null +++ b/.changeset/fast-hotels-know.md @@ -0,0 +1,5 @@ +--- +"obsidian-modules": patch +--- + +Provide `app` to avoid using the deprecated global `app`. For users, nothing needs to be changed. diff --git a/README.md b/README.md index efb176f4..1146fdc6 100644 --- a/README.md +++ b/README.md @@ -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 . diff --git a/sources/@types/obsidian-modules.ts b/sources/@types/obsidian-modules.ts index d7f0b0e3..d7e42a8a 100644 --- a/sources/@types/obsidian-modules.ts +++ b/sources/@types/obsidian-modules.ts @@ -74,6 +74,11 @@ declare module "obsidian-modules" { */ readonly aliases: Map> + /** + * Associated {@link App}. + */ + readonly app: App + /** * Context for loading modules. */ @@ -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" diff --git a/sources/require/require.ts b/sources/require/require.ts index 5ebd6601..756e4153 100644 --- a/sources/require/require.ts +++ b/sources/require/require.ts @@ -16,6 +16,7 @@ import { aroundIdentityFactory, attachFunctionSourceMap, clearProperties, + escapeJavaScriptString as escJSStr, launderUnchecked, patchWindows, promisePromise, @@ -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}`, @@ -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, { @@ -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(), @@ -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(