Skip to content

Add support function serialization in Content Declarations for Preserving Functions in Dictionary Build #132

@aymericzip

Description

@aymericzip

Feature Request

Summary:
Enable support for wrapping functions with a fun() helper in content declaration files, allowing them to be preserved in the compiled dictionary (.intlayer/dictionary/*.json) and usable at runtime.


Current Behavior

Currently, content declarations like this:

import { type Dictionary } from 'intlayer';

const exampleContent = {
  key: 'example',
  content: {
    functionTest: (name) => `Hi ${name}`,
  },
} satisfies Dictionary;

export default exampleContent;

Are compiled into JSON and lose all function behavior due to serialization. As a result, calling content.functionTest(name) at runtime is not possible.


Proposed Behavior

Introduce an official fun() wrapper utility (or recognize it in the compiler) like so:

import { type Dictionary, fun } from 'intlayer';

const exampleContent = {
  key: 'example',
  content: {
    functionTest: fun((name) => `Hi ${name}`),
  },
} satisfies Dictionary;

export default exampleContent;

This would signal to the compiler to preserve or rehydrate the function logic. After build, the dictionary remains usable like:

const content = useIntlayer("example");
content.functionTest("John"); // => "Hi John"

Benefits

  • Enables dynamic content (e.g., string templating, computed values).
  • Keeps content logic centralized and reusable.
  • Maintains type safety and developer ergonomics in TypeScript.

Considerations

  • Functions must be safely serializable (e.g., limited to pure JS, no closures).
  • JSON output may need a function-safe encoding or separate JS export.
  • Security or sandboxing should be considered if user-defined functions are allowed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureA request, idea, or new functionality

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions