Skip to content

Commit 2e3c8e1

Browse files
chore: migrate context-mill to ESM (#125)
- Add "type": "module" to package.json - Add engines field requiring Node >=20.11.0 (for import.meta.dirname) - Convert all require()/module.exports to import/export across 15 files - Replace __dirname with import.meta.dirname in build.js and dev-server.js - Fix test files that mixed require() with ESM imports All 42 tests pass. Full build (82 skills) completes successfully. Generated-By: PostHog Code Task-Id: 6d2d9459-9c69-4d58-9937-ca35bf9ecea4
1 parent 5d85a83 commit 2e3c8e1

15 files changed

Lines changed: 53 additions & 53 deletions

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "@posthog/context-mill",
33
"version": "1.2.0",
44
"private": true,
5+
"type": "module",
56
"description": "PostHog Context Mill: assembling and packaging context for AI agents and LLMs",
67
"scripts": {
78
"build": "node scripts/build.js",
@@ -16,6 +17,9 @@
1617
"archiver": "^7.0.1",
1718
"vitest": "^2.0.0"
1819
},
20+
"engines": {
21+
"node": ">=20.11.0"
22+
},
1923
"dependencies": {
2024
"gray-matter": "^4.0.3",
2125
"js-yaml": "^4.1.1"

scripts/build.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
* - Individual skill ZIPs ({skill-id}.zip)
1010
*/
1111

12-
const fs = require('fs');
13-
const path = require('path');
14-
const yaml = require('js-yaml');
15-
const archiver = require('archiver');
16-
const { generateAllSkills, loadSkillsConfig, fetchDoc } = require('./lib/skill-generator');
17-
const { generateMarketplace } = require('./lib/marketplace-generator');
18-
const { REPO_URL } = require('./lib/constants');
12+
import fs from 'fs';
13+
import path from 'path';
14+
import yaml from 'js-yaml';
15+
import archiver from 'archiver';
16+
import { generateAllSkills, loadSkillsConfig, fetchDoc } from './lib/skill-generator.js';
17+
import { generateMarketplace } from './lib/marketplace-generator.js';
18+
import { REPO_URL } from './lib/constants.js';
1919

2020
const BUILD_VERSION = process.env.BUILD_VERSION || 'dev';
2121

@@ -148,7 +148,7 @@ async function main() {
148148
console.log('Building resources...');
149149
console.log(`Version: ${BUILD_VERSION}\n`);
150150

151-
const repoRoot = path.join(__dirname, '..');
151+
const repoRoot = path.join(import.meta.dirname, '..');
152152
const configDir = path.join(repoRoot, 'transformation-config');
153153
const distDir = path.join(repoRoot, 'dist');
154154
const skillsDir = path.join(distDir, 'skills');

scripts/dev-server.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@
1515
* pnpm run dev:local-resources (and update wrangler --var flag)
1616
*/
1717

18-
const http = require('http');
19-
const fs = require('fs');
20-
const path = require('path');
21-
const { spawn } = require('child_process');
18+
import http from 'http';
19+
import fs from 'fs';
20+
import path from 'path';
21+
import { spawn } from 'child_process';
2222

2323
const PORT = process.env.PORT || 8765;
24-
const DIST_DIR = path.join(__dirname, '..', 'dist');
24+
const DIST_DIR = path.join(import.meta.dirname, '..', 'dist');
2525
const SKILLS_ZIP_PATH = path.join(DIST_DIR, 'skills-mcp-resources.zip');
2626
const SKILLS_DIR = path.join(DIST_DIR, 'skills');
2727

2828
// Directories to watch for changes
2929
const WATCH_DIRS = [
30-
path.join(__dirname, '..', 'llm-prompts'),
31-
path.join(__dirname, '..', 'transformation-config'),
32-
path.join(__dirname, '..', 'mcp-commands'),
33-
path.join(__dirname, '..', 'basics'),
30+
path.join(import.meta.dirname, '..', 'llm-prompts'),
31+
path.join(import.meta.dirname, '..', 'transformation-config'),
32+
path.join(import.meta.dirname, '..', 'mcp-commands'),
33+
path.join(import.meta.dirname, '..', 'basics'),
3434
];
3535

3636
let isRebuilding = false;
@@ -53,7 +53,7 @@ function rebuild() {
5353

5454
const buildProcess = spawn('npm', ['run', 'build'], {
5555
stdio: 'inherit',
56-
cwd: path.join(__dirname, '..'),
56+
cwd: path.join(import.meta.dirname, '..'),
5757
env: { ...process.env, SKILLS_BASE_URL: localSkillsUrl },
5858
shell: true
5959
});
@@ -83,11 +83,11 @@ function setupWatchers() {
8383

8484
WATCH_DIRS.forEach(dir => {
8585
if (!fs.existsSync(dir)) {
86-
console.log(` ⚠️ ${path.relative(path.join(__dirname, '..'), dir)} (not found, skipping)`);
86+
console.log(` ⚠️ ${path.relative(path.join(import.meta.dirname, '..'), dir)} (not found, skipping)`);
8787
return;
8888
}
8989

90-
console.log(` 📁 ${path.relative(path.join(__dirname, '..'), dir)}`);
90+
console.log(` 📁 ${path.relative(path.join(import.meta.dirname, '..'), dir)}`);
9191

9292
// Watch recursively
9393
fs.watch(dir, { recursive: true }, (eventType, filename) => {
@@ -200,7 +200,7 @@ async function main() {
200200
await new Promise((resolve) => {
201201
const buildProcess = spawn('npm', ['run', 'build'], {
202202
stdio: 'inherit',
203-
cwd: path.join(__dirname, '..'),
203+
cwd: path.join(import.meta.dirname, '..'),
204204
env: { ...process.env, SKILLS_BASE_URL: localSkillsUrl },
205205
shell: true
206206
});

scripts/lib/constants.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,4 @@
22
* Shared constants for build scripts
33
*/
44

5-
const REPO_URL = 'https://github.com/PostHog/context-mill';
6-
7-
module.exports = {
8-
REPO_URL,
9-
};
5+
export const REPO_URL = 'https://github.com/PostHog/context-mill';

scripts/lib/example-processor.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
* Reads configuration from transformation-config/skip-patterns.yaml
66
*/
77

8-
const fs = require('fs');
9-
const path = require('path');
10-
const yaml = require('js-yaml');
11-
const { composePlugins, ignoreLinePlugin, ignoreFilePlugin, ignoreBlockPlugin } = require('../plugins/index');
12-
const { REPO_URL } = require('./constants');
8+
import fs from 'fs';
9+
import path from 'path';
10+
import yaml from 'js-yaml';
11+
import { composePlugins, ignoreLinePlugin, ignoreFilePlugin, ignoreBlockPlugin } from '../plugins/index.js';
12+
import { REPO_URL } from './constants.js';
1313

1414
/**
1515
* Load skip patterns from YAML config
@@ -182,7 +182,7 @@ function processExample({ examplePath, displayName, id, repoRoot, skipPatterns,
182182
*/
183183
const defaultPlugins = [ignoreFilePlugin, ignoreBlockPlugin, ignoreLinePlugin];
184184

185-
module.exports = {
185+
export {
186186
loadSkipPatterns,
187187
mergeSkipPatterns,
188188
processExample,

scripts/lib/marketplace-generator.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
* Configuration is driven by transformation-config/marketplace.yaml.
77
*/
88

9-
const fs = require('fs');
10-
const path = require('path');
11-
const yaml = require('js-yaml');
9+
import fs from 'fs';
10+
import path from 'path';
11+
import yaml from 'js-yaml';
1212

1313
/**
1414
* Load marketplace config from YAML
@@ -253,4 +253,4 @@ function generateMarketplace({ skills, tempDir, version, outputDir, configDir })
253253
return { marketplaceDir, pluginCount: allPluginNames.length, skillCount: allSkillEntries.length };
254254
}
255255

256-
module.exports = { generateMarketplace };
256+
export { generateMarketplace };

scripts/lib/skill-generator.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
* - Commandments (based on tags)
99
*/
1010

11-
const fs = require('fs');
12-
const path = require('path');
13-
const yaml = require('js-yaml');
14-
const matter = require('gray-matter');
15-
const { processExample, loadSkipPatterns, mergeSkipPatterns, defaultPlugins } = require('./example-processor');
11+
import fs from 'fs';
12+
import path from 'path';
13+
import yaml from 'js-yaml';
14+
import matter from 'gray-matter';
15+
import { processExample, loadSkipPatterns, mergeSkipPatterns, defaultPlugins } from './example-processor.js';
1616

1717
/**
1818
* Load YAML config file
@@ -684,7 +684,7 @@ async function generateAllSkills({
684684
}));
685685
}
686686

687-
module.exports = {
687+
export {
688688
loadSkillsConfig,
689689
loadCommandments,
690690
loadSkillTemplate,

scripts/lib/tests/skill-config-loader.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { join } from 'path';
44
import { tmpdir } from 'os';
55
import yaml from 'js-yaml';
66

7-
const { loadSkillsConfig } = require('../skill-generator.js');
7+
import { loadSkillsConfig } from '../skill-generator.js';
88

99
function createFixture(tree, baseDir) {
1010
for (const [name, content] of Object.entries(tree)) {

scripts/lib/tests/skill-generator-references-folder.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { mkdirSync, writeFileSync, readFileSync, existsSync, mkdtempSync, rmSync
33
import { join } from 'path';
44
import { tmpdir } from 'os';
55

6-
const { expandSkillGroups, generateSkill } = require('../skill-generator.js');
6+
import { expandSkillGroups, generateSkill } from '../skill-generator.js';
77

88
function createFixture(tree, baseDir) {
99
for (const [name, content] of Object.entries(tree)) {

scripts/lib/tests/skill-group-expander.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { mkdirSync, writeFileSync, mkdtempSync, rmSync } from 'fs';
33
import { join } from 'path';
44
import { tmpdir } from 'os';
55

6-
const { expandSkillGroups } = require('../skill-generator.js');
6+
import { expandSkillGroups } from '../skill-generator.js';
77

88
function createFixture(tree, baseDir) {
99
for (const [name, content] of Object.entries(tree)) {

0 commit comments

Comments
 (0)