Skip to content

Commit ba147bf

Browse files
committed
Add dependents for plugins back into dynamic resolving
1 parent a682a28 commit ba147bf

3 files changed

Lines changed: 13 additions & 10 deletions

File tree

build/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ const externals = ["@luna/*", "oby", "react", "react-dom/client", "react/jsx-run
2323
export const pluginBuildOptions = async (pluginPath: string, opts?: BuildOptions) => {
2424
const pluginPackage = await readFile(path.join(pluginPath, "package.json"), "utf8").then(JSON.parse);
2525
// Sanitize pluginPackage.name, remove @, replace / with .
26-
const safeName = pluginPackage.name.replace(/@/g, "").replace(/\//g, ".");
26+
const pkgName = pluginPackage.name;
27+
const safeName = pkgName.replace(/@/g, "").replace(/\//g, ".");
2728
return <BuildOptions>{
2829
...defaultBuildOptions,
2930
write: false,
@@ -36,7 +37,15 @@ export const pluginBuildOptions = async (pluginPath: string, opts?: BuildOptions
3637
external: [...(opts?.external ?? []), ...externals],
3738
plugins: [
3839
...(opts?.plugins ?? []),
39-
dynamicExternalsPlugin("luna?.core?.modules"),
40+
dynamicExternalsPlugin(
41+
(module: string) => `
42+
const imp = luna?.core?.modules?.["${module}"];
43+
if (imp === undefined) throw new Error("Cannot find module ${module} in luna.core.modules");
44+
// Icky but it works
45+
luna.core.LunaPlugin.plugins["${module}"]?.dependents.add(luna.core.LunaPlugin.plugins["${pkgName}"]);
46+
module.exports = imp;
47+
`,
48+
),
4049
fileUrlPlugin,
4150
lunaNativePlugin,
4251
writeBundlePlugin(path.join(pluginPath, "package.json")),

build/plugins/dynamicExternals.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Plugin } from "esbuild";
22

3-
export const dynamicExternalsPlugin = (globalStore: string): Plugin => ({
3+
export const dynamicExternalsPlugin = (resolveModuleContents: (module: string) => string): Plugin => ({
44
name: "dynamicExternals",
55
setup(build) {
66
const externalsRegex = build.initialOptions.external
@@ -12,11 +12,7 @@ export const dynamicExternalsPlugin = (globalStore: string): Plugin => ({
1212
namespace: "dynamicExternals",
1313
}));
1414
build.onLoad({ filter: /.*/, namespace: "dynamicExternals" }, (args) => ({
15-
contents: `
16-
const imp = ${globalStore}?.["${args.path}"];
17-
if (imp === undefined) throw new Error("Cannot find module '${args.path}' in ${globalStore}'");
18-
module.exports = imp;
19-
`,
15+
contents: resolveModuleContents(args.path),
2016
}));
2117
},
2218
});

render/src/LunaPlugin.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,6 @@ export class LunaPlugin {
354354
this.exports = await import(blobURL);
355355
if (this.exports === undefined) return this.trace.err.throw(`Failed to load. Module exports undefined!`);
356356

357-
// todo RETURN LunaPlugin.plugins[name]?.dependents.add(this);
358-
359357
// Ensure loadError is cleared
360358
this.loadError._ = undefined;
361359

0 commit comments

Comments
 (0)