mr-world
TypeScript icon, indicating that this package has built-in type declarations

0.0.12 • Public • Published

mr-world The global variable integration for Astro

mr-world

It’s never a matter of old and new. It’s only about patterns.
—Mr. World, American Gods

Total Downloads Latest Release License


Motivation

Provide an integration for Astro to define global variables that work in:

Quick start

Install

Install via npm:

npm install mr-world

Configure

In your Astro config file:

import { defineConfig } from "astro/config";
import { mrWorld } from "mr-world/integration";

export default defineConfig({
  site: "https://example.com",
  integrations: [
    mrWorld({
      id: "DEFAULT_LOCALE",
      useEffect: () => {

        // set global properties and functions
        globalThis.DEFAULT_LOCALE = "en";

        return () => {
          // cleanup side effects
        };
      },
    }),
  ],
});

In your Astro env.d.ts file:

declare module globalThis {
  var DEFAULT_LOCALE: "en";
}

Configure script tags

In the head of your document:

---
import { Serialize } from "mr-world/serialize";
---

<Serialize id="locale" data={{ DEFAULT_LOCALE: "en" }} />

After your <Serialize /> component:

<script>
  import { deserialize } from "mr-world/deserialize";

  const data = await deserialize({
    id: "locale",
  });
</script>

You can pass a custom serializer as props or a custom deserializer in parameters. Custom serializers may support nested structures or other data types like Date or ArrayBuffer:

By default, JSON.stringify and JSON.parse are used as serializers.

License

MIT Licensed

Contributing

PRs welcome! Thank you for your help.

Package Sidebar

Install

npm i mr-world

Weekly Downloads

184

Version

0.0.12

License

MIT

Unpacked Size

23.3 kB

Total Files

33

Last publish

Collaborators

  • jlarmstrongiv