|
| 1 | +# PR Title |
| 2 | +Fix ESM import failure in v4.0.1 (#158) |
| 3 | + |
| 4 | +# PR Description |
| 5 | + |
| 6 | +## 🐛 Fix for Issue #158 - ESM Import Failure |
| 7 | + |
| 8 | +This PR resolves the ESM import failure in v4.0.1 where imports like `import { Component, toNative, Vue } from 'vue-facing-decorator'` would fail with: |
| 9 | + |
| 10 | +``` |
| 11 | +SyntaxError: The requested module 'vue-facing-decorator' does not provide an export named 'Component' |
| 12 | +``` |
| 13 | + |
| 14 | +### Root Cause |
| 15 | +The package declared `"type": "commonjs"` but shipped ESM files with `export` statements, causing Node.js to fail when parsing them in ESM contexts. |
| 16 | + |
| 17 | +### Solution |
| 18 | +Implemented a proper dual CommonJS/ESM package structure following Node.js best practices: |
| 19 | + |
| 20 | +1. **Dual Package Structure**: Added separate `package.json` files in `dist/esm/` and `dist/cjs/` directories to override module types |
| 21 | +2. **ESM Import Resolution**: Created postbuild script that adds `.js` extensions to relative imports in ESM files (required by Node.js ESM) |
| 22 | +3. **Cross-platform Compatibility**: Updated build scripts to use `npx` and added `cross-env` for Windows support |
| 23 | + |
| 24 | +### Changes Made |
| 25 | +- ✅ **package.json**: Updated build scripts, added postbuild step, added cross-env dependency, bumped to v4.0.2 |
| 26 | +- ✅ **scripts/postbuild.js**: New script that creates package.json files and fixes import extensions |
| 27 | +- ✅ **tsconfig/*.json**: Added explicit `moduleResolution: "node"` for consistency |
| 28 | +- ✅ **Cross-platform**: Fixed Windows compatibility issues in build scripts |
| 29 | + |
| 30 | +### Verification |
| 31 | +- ✅ ESM imports work: `import { Component, toNative, Vue } from 'vue-facing-decorator'` |
| 32 | +- ✅ CommonJS imports still work: `const { Component, toNative, Vue } = require('vue-facing-decorator')` |
| 33 | +- ✅ No breaking changes for existing users |
| 34 | +- ✅ Follows Node.js dual package guidelines |
| 35 | + |
| 36 | +### Testing |
| 37 | +The fix has been tested with both ESM and CommonJS imports to ensure compatibility. |
| 38 | + |
| 39 | +Closes #158 |
0 commit comments