Skip to content

Conversation

@cho45
Copy link
Owner

@cho45 cho45 commented Jun 15, 2025

new Function が許されない環境向けに module としてコードを直接生成するサンプルを試行錯誤中

usage

以下で test/serialize/*.tmpl をコンパイルし、_serialized.js に esm として出力

npx micro-template-serialize test/serialize/*.tmpl --output _serialized.js --root test/serialize

以下のように import して使える。(実行時には動的関数生成が走らない)

import { extended as template } from '../_serialized.js';

const result = template('main', { foo: 'world', baz: 'baz!' });
console.log('render result:', result);

問題点

  • 事前に使われる変数を知る必要がある
<!--meta.keys=["foo", "baz"] -->

と指定するようにした

非互換

  • tempalte(id) のときの id にスラッシュを含むようにする

@cho45 cho45 requested a review from Copilot June 15, 2025 13:38
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request implements a new method of serializing compiled templates as an ES module to allow usage in environments where dynamic function generation is forbidden. Key changes include:

  • Removing the legacy helper function and updating tests to reference the compiled function via its new 'compiled' property.
  • Adding a suite of tests in serializer.test.js that cover various template scenarios, including nested IDs, meta comments, and include/wrapper functionality.
  • Updating the serializer and micro-template modules to correctly export and attach properties on compiled functions.

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
test/test.js Updated template function invocation and test for source maps.
test/serializer.test.js Added comprehensive tests for the new serialization module.
test/serialize/*.tmpl New template files demonstrating meta keys and template composition.
package.json Updated with new bin and exports configuration.
lib/serializer.js Implements serialization of compiled templates to an ES module.
lib/micro-template.js Adjusted function compilation and property attachment.
index.html Updated documentation section for getting started.
Comments suppressed due to low confidence (1)

test/test.js:321

  • [nitpick] The variable name 'func' is ambiguous; consider renaming it to a more descriptive name like 'compiledTemplate' for clarity.
const func = template('foo bar', []);


for (const [id, entry] of Object.entries(target)) {
const keys = entry.keys || [];
console.log(`Compiling template: ${id}`);
Copy link

Copilot AI Jun 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider removing or gating the console.log statement to avoid unwanted logging in production builds.

Suggested change
console.log(`Compiling template: ${id}`);
if (process.env.NODE_ENV !== 'production') {
console.log(`Compiling template: ${id}`);
}

Copilot uses AI. Check for mistakes.
@cho45 cho45 merged commit fdb7f9f into master Jun 15, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants