Skip to content

Conversation

@konard
Copy link
Member

@konard konard commented Sep 10, 2025

Summary

Implements experimental freeze() function that allows freezing dependency versions after first execution by modifying the source code using regex replacements.

Key Features

  • Version Freezing: freeze('lodash') gets the latest version from npm and replaces the call with use('[email protected]')
  • Cross-platform: Works with both ESM (.mjs) and CommonJS (.cjs) modules
  • Syntax Preservation: Maintains await keyword when present
  • Accurate File Detection: Uses stack trace analysis to find and modify the correct source file
  • Error Handling: Comprehensive error handling for various edge cases

Usage Examples

ESM (ES Modules):

import { use, freeze } from 'use-m';

// Before first run:
const _ = await freeze('lodash');

// After first run, file is automatically modified to:
const _ = await use('[email protected]');

CommonJS:

const { use, freeze } = require('use-m');

(async () => {
  // Before first run:
  const chalk = await freeze('chalk');
  
  // After first run, file is automatically modified to:
  const chalk = await use('[email protected]');
})();

Implementation Details

  • Uses npm view <package> version to fetch latest version
  • Employs regex pattern matching to find freeze() calls in source files
  • Captures stack traces at function call time (before async operations) for accurate caller context
  • Handles both file:// URLs (ESM) and absolute paths (CommonJS) from stack traces
  • Preserves existing code formatting and await keywords
  • Supports all package naming conventions including scoped packages

Testing

  • ✅ ESM freeze functionality with lodash and simple-git
  • ✅ CommonJS freeze functionality with chalk
  • ✅ Existing test suite passes (Node.js tests)
  • ✅ No breaking changes to existing functionality

Closes #23

🤖 Generated with Claude Code

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #23
@konard konard self-assigned this Sep 10, 2025
konard and others added 2 commits September 10, 2025 07:46
Add experimental freeze function that allows freezing dependency versions
after first execution by modifying source code using regex replacements.

Features:
- freeze('lodash') gets latest version from npm and replaces with use('[email protected]')
- Works with both ESM (.mjs) and CommonJS (.cjs) modules
- Supports await syntax preservation
- Captures caller context from stack traces for accurate file modification
- Includes comprehensive error handling for edge cases

Examples:
- const _ = await freeze('lodash') → const _ = await use('[email protected]')
- Works with scoped packages like @scope/package

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@konard konard changed the title [WIP] Freeze version function (experiment) Implement freeze() function for version freezing (issue #23) Sep 10, 2025
@konard konard marked this pull request as ready for review September 10, 2025 05:00
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.

Freeze version function (experiment)

2 participants