mirror of
https://github.com/priyanshujain/sanderling.git
synced 2026-10-04 12:07:09 +00:00
refactor(bundler): inject globalThis trailer from spec named exports
This commit is contained in:
1 parent
9a2cb61561
commit
868ab78c8e
1 file changed
+14
-1
@@ -74,7 +74,7 @@ func Bundle(options Options) (Result, error) {
|
|||||||
return Result{}, fmt.Errorf("entry path: %w", err)
|
return Result{}, fmt.Errorf("entry path: %w", err)
|
||||||
}
|
}
|
||||||
buildOptions.Stdin = &esbuild.StdinOptions{
|
buildOptions.Stdin = &esbuild.StdinOptions{
|
||||||
Contents: fmt.Sprintf("import %q;\nimport %q;\n", runtimeAbs, specAbs),
|
Contents: fmt.Sprintf("import %q;\n%s", runtimeAbs, registrationEntry(specAbs)),
|
||||||
ResolveDir: filepath.Dir(specAbs),
|
ResolveDir: filepath.Dir(specAbs),
|
||||||
Loader: esbuild.LoaderTS,
|
Loader: esbuild.LoaderTS,
|
||||||
}
|
}
|
||||||
@@ -100,3 +100,16 @@ func Bundle(options Options) (Result, error) {
|
|||||||
SHA256: hex.EncodeToString(sum[:]),
|
SHA256: hex.EncodeToString(sum[:]),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// registrationEntry imports the spec as a namespace and copies its named
|
||||||
|
// exports onto globalThis so authors write plain `export const properties /
|
||||||
|
// actionsRoot / setup` without hand-assigning globalThis. The guards let web
|
||||||
|
// specs omit setup. esbuild keeps the exports because the trailer references
|
||||||
|
// them; output stays an IIFE.
|
||||||
|
func registrationEntry(specAbs string) string {
|
||||||
|
return fmt.Sprintf(`import * as __spec from %q;
|
||||||
|
if (__spec.actionsRoot !== undefined) globalThis.actions = __spec.actionsRoot;
|
||||||
|
if (__spec.properties !== undefined) globalThis.properties = __spec.properties;
|
||||||
|
if (__spec.setup !== undefined) globalThis.setup = __spec.setup;
|
||||||
|
`, specAbs)
|
||||||
|
}
|
||||||
Reference in new issue
Block a user