-
Notifications
You must be signed in to change notification settings - Fork 0
feat: npm improvements - import maps, tree shaking, and dev/prod mode #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This PR implements a comprehensive set of npm improvement features for the Husk build system: ## Phase 1: Core NPM Integration Fixes - ✅ Task 1.1: Make package.json generation default behavior in build command - ✅ Task 1.2: Fix import path generation for npm packages - ✅ Task 1.3: Implement consistent module format across targets - ✅ Task 1.4: Fix package resolution for CommonJS/ESM interop ## Phase 2: Enhanced NPM Features - ✅ Task 2.1: Enhanced exports field resolution for subpath imports - ✅ Task 2.2: Better module type detection (ESM vs CommonJS) - ✅ Task 2.3: TypeScript definitions support (.d.ts files) ## Phase 3: Advanced Build Features - ✅ Task 3.1: Wire up target configuration from husk.toml - ✅ Task 3.2: Platform-specific imports (browser/node/deno/bun) - ✅ Task 3.3: External dependencies configuration ## Phase 4: Modern JavaScript Features - ✅ Task 4.1: Import maps support for browser targets - ✅ Task 4.2: Tree shaking hints with /*#__PURE__*/ annotations - ✅ Task 4.3: Development vs Production mode ## Key Changes: - Modified build command to generate package.json by default (use --skip-package-json to opt out) - Added smart package.json regeneration that only updates when dependencies change - Implemented proper CommonJS/ESM interop with require() vs import handling - Added support for package.json exports field with subpath imports - Implemented platform-specific code generation based on target - Added external dependencies configuration to exclude packages from bundling - Implemented import maps for browser targets - Added tree shaking annotations for better dead code elimination - Added development/production mode support with environment-specific optimizations ## Testing: - Added comprehensive integration tests covering all npm features - Tests run in both local and CI environments - All features have been validated with real npm packages 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
Add dev, import_map, and tree_shaking fields to fix compilation error in tests.
When building a complete project (as opposed to just transpiling), the generated JavaScript now automatically calls the main() function at the end of the file. This ensures that Husk programs execute correctly when run with Node.js or in the browser. The transpiler now: - Checks if a main() function is defined in the AST - Adds a call to main() at the end of the generated JavaScript - Includes a comment to make it clear this is an auto-generated call This fixes the issue where built JavaScript files would define functions but not execute anything when run.
- Add transpile_to_js_with_entry_validation function to validate main() in entry files - Fix transpiler to call async main() functions (was only checking regular functions) - Add comprehensive tests for entry validation and main() call generation - Ensure non-entry files don't require main() function 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
- Add module format-aware export generation in transpiler - Functions marked as 'pub' now export with CommonJS syntax when targeting node-cjs - Add test-specific transpiler constructor for module format testing - Enable and fix test_transpile_node_cjs_target test - Ensure require() is used for Node.js built-ins in CommonJS mode 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
|
@claude review this PR please |
|
Claude finished @fcoury's task —— View job PR Review: NPM Improvements - Import Maps, Tree Shaking, and Dev/Prod ModeI've completed a thorough review of this substantial PR implementing three key npm improvement features. Here's my comprehensive feedback: ✅ Strengths1. Code Quality & Architecture
2. Feature ImplementationImport Maps (config.rs:114, transpiler.rs:1141-1162)
Tree Shaking (transpiler.rs:1520-1569)
Dev/Prod Mode (transpiler.rs:1571-1603)
3. Test Coverage
|
|
Claude finished @fcoury's task —— View job PR Review: npm improvements - import maps, tree shaking, and dev/prod modeTodo List:
Overall AssessmentThis is a well-implemented PR that adds significant value to the Husk build system. The three new features (import maps, tree shaking hints, and dev/prod mode) are properly integrated with comprehensive test coverage. The implementation follows good software engineering practices with proper separation of concerns. ✅ Strengths1. Excellent Test Coverage
2. Clean Configuration Integration
3. Robust Implementation
4. Smart Feature Interactions
🔍 Areas for Improvement1. Minor: Tree Shaking Logic
|
- Add URL validation for import maps to prevent security issues - Validate URLs start with http://, https://, /, ./, or ../ - Add comprehensive tests for URL validation - Return configuration errors for invalid URLs - Adjust pure functions list for tree shaking correctness - Remove Math.random (has RNG state side effects) - Remove JSON.parse/stringify (can throw exceptions) - Add explicit exclusion for JSON.* methods - Clean up repository - Remove backup files (*.bak) - Remove test files from root directory - Remove test project directories - Fix all compilation warnings - Remove unused import in config_test.rs - Fix unused variables in transpiler_target_modes_test.rs All high-priority security and correctness issues from PR review have been addressed. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
- Add configuration examples to README - Import maps with CDN and local path examples - Tree shaking configuration options - Development mode features and output - Multiple target configurations - Document feature interactions and precedence - Tree shaking automatically disabled in dev mode - Import maps take precedence over package resolution - External dependencies work with import maps - Platform-specific behaviors - Add import map security considerations - URL validation and allowed protocols - Best practices for secure usage - Version pinning and HTTPS recommendations - Examples of secure vs insecure configurations Addresses documentation feedback from PR #24 review. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
- Replace string matching with HashSet for O(1) pure function lookups - Add null/undefined checks to runtime type assertions in dev mode - Improve performance of tree shaking pure annotations - Fix compilation warnings by removing unused imports 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
Adds support for importing default exports from npm packages using the `external::package::default` syntax. This allows importing packages like Express that export as default. - Modified transpiler to detect and handle `external::package::default` pattern - Updated semantic analyzer to register package name for default imports - Added Express example app demonstrating the new import syntax - Removed old dist files that are no longer needed Example usage: `use external::express::default;` 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
Adds comprehensive tests for the new default export import features: - Tests for external::package::default syntax - Tests for package::default syntax - Verifies correct transpilation to ES module default imports 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
Updates the test output files after running update.sh --force to match the actual compiler output. The tests now correctly verify: - Interpreter mode errors for external packages - Proper transpilation to ES module default imports 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
Updates the Express example to document the current limitation where external packages have Unknown type and don't support method calls in the semantic analyzer. - Simplified main.husk to show what works - Added comprehensive README explaining the limitation - Documented potential future improvements The import syntax works correctly and generates proper ES modules, but full Express usage requires type system enhancements. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
- Modified parser to handle 'self' parameter without type annotation in extern methods - Updated semantic analyzer to filter out 'self' when registering extern impl methods - Fixed extern mod processing to correctly include module prefix in type and method names - Updated express example to use proper type names with module prefix - Fixed method call handling to not prepend object for extern types This allows extern type declarations to use self parameter syntax like regular impl blocks, making the API more consistent and natural for declaring external object-oriented APIs. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
The transpiler was incorrectly treating extern method calls like app.get()
as array indexing because "get" is a builtin array method. Added special
handling to check argument count - array.get() takes 1 arg, so methods
with more args fall through to default method call syntax.
This fixes the Express.js example where app.get("/", handler) was being
transpiled as app["/"] instead of app.get("/", handler).
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <[email protected]>
- Moved package.json generation from project root to dist folder - Changed main entry point from "dist/index.js" to "main.js" - Updated npm install instructions to reflect new location - Removed unused test files from express-app example This makes the dist folder self-contained and deployable without requiring files from the parent directory. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
Fixed failing test that was expecting "build/index.js" as the main entry point. The test now correctly expects "main.js" to match our recent change to generate package.json in the dist folder. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
- Added tests for parsing extern impl blocks with self parameters - Added tests for mixed self and non-self methods - Added tests for self with multiple parameters - Added tests for method chaining patterns with self - All tests verify that self is correctly parsed as the first parameter These tests ensure the self parameter support in extern declarations works correctly for various use cases. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
…arameters - Add comprehensive tests for extern impl blocks with self parameters - Add tests for mixed self/non-self methods and method chaining - Create detailed documentation for extern declarations feature - Update language features index to include extern declarations - Tests cover parsing, semantic analysis, and transpilation of extern self params 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
- Add URL validation for import maps to prevent security issues - Validate URLs start with http://, https://, /, ./, or ../ - Add comprehensive tests for URL validation - Return configuration errors for invalid URLs - Adjust pure functions list for tree shaking correctness - Remove Math.random (has RNG state side effects) - Remove JSON.parse/stringify (can throw exceptions) - Add explicit exclusion for JSON.* methods - Clean up repository - Remove backup files (*.bak) - Remove test files from root directory - Remove test project directories - Fix all compilation warnings - Remove unused import in config_test.rs - Fix unused variables in transpiler_target_modes_test.rs All high-priority security and correctness issues from PR review have been addressed. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
- Add configuration examples to README - Import maps with CDN and local path examples - Tree shaking configuration options - Development mode features and output - Multiple target configurations - Document feature interactions and precedence - Tree shaking automatically disabled in dev mode - Import maps take precedence over package resolution - External dependencies work with import maps - Platform-specific behaviors - Add import map security considerations - URL validation and allowed protocols - Best practices for secure usage - Version pinning and HTTPS recommendations - Examples of secure vs insecure configurations Addresses documentation feedback from PR #24 review. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
Summary
This PR implements the remaining npm improvement features for the Husk build system:
husk.toml/*#__PURE__*/annotations for better dead code eliminationdevflagChanges
1. Import Maps Support
import_mapconfiguration toTargetConfig2. Tree Shaking Hints
tree_shakingflag toTargetConfig/*#__PURE__*/annotations to:new Date())3. Development vs Production Mode
devflag toTargetConfigTesting
Added comprehensive integration tests in
tests/integration/test_npm_features.shthat verify:All tests pass locally and in CI.
Example Usage
🤖 Generated with Claude Code
Co-Authored-By: Claude [email protected]