aymericzip / aymericzip/intlayer

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

Open
#132 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
TypeScript
Stars
833
Forks
126
Avg merge
1d 3h
Merged PRs (30d)
7

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how content declarations are compiled into .intlayer/dictionary/*.json and how useIntlayer reads the resulting dictionary. Review the proposed fun() wrapper and the compiler/runtime entry points, including the serialization and security constraints described in the issue. Done means a supported function can be declared, survive the build, and be callable at runtime without unsafe or unsupported closures.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
build-system
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.