|
| 1 | +# Windows Path Fix |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document describes the Windows path separator fix implemented in `esbuild-fix-imports-plugin` to ensure cross-platform compatibility. |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +When building on Windows, Node.js path operations (like `path.relative`, `path.join`) return Windows backslashes (`\`). These backslashes were being inserted directly into import specifiers in the generated output files, causing module loading to fail in ESM/CJS environments. |
| 10 | + |
| 11 | +### Example of the Issue |
| 12 | + |
| 13 | +**Before the fix (Windows):** |
| 14 | + |
| 15 | +```javascript |
| 16 | +// Generated output contained: |
| 17 | +import { data } from "..\utils\foo"; |
| 18 | +require("..\\utils\\foo"); |
| 19 | +``` |
| 20 | + |
| 21 | +**After the fix:** |
| 22 | + |
| 23 | +```javascript |
| 24 | +// All output now uses POSIX separators: |
| 25 | +import { data } from "../utils/foo"; |
| 26 | +require("../utils/foo"); |
| 27 | +``` |
| 28 | + |
| 29 | +## Root Cause |
| 30 | + |
| 31 | +1. **Node.js path functions**: `path.relative()`, `path.join()`, etc. return `\\` on Windows |
| 32 | +2. **Direct insertion**: These strings were inserted directly into import/require statements |
| 33 | +3. **ESM/CJS requirement**: Import specifiers must use POSIX separators (`/`) regardless of platform |
| 34 | + |
| 35 | +## Solution |
| 36 | + |
| 37 | +### POSIX Normalization Helpers |
| 38 | + |
| 39 | +Three utility functions were added to `src/fixAliasPlugin.ts`: |
| 40 | + |
| 41 | +```typescript |
| 42 | +/** |
| 43 | + * Converts Windows backslashes to POSIX forward slashes |
| 44 | + */ |
| 45 | +const toPosix = (p: string) => p.replace(/\\/g, "/"); |
| 46 | + |
| 47 | +/** |
| 48 | + * Normalizes import paths by converting to POSIX and cleaning up redundant segments |
| 49 | + */ |
| 50 | +const normalizeImportPath = (p: string) => |
| 51 | + toPosix(p) |
| 52 | + .replace(/\/\.\//g, "/") |
| 53 | + .replace(/(^|[^:])\/\/+/g, "$1/"); |
| 54 | + |
| 55 | +/** |
| 56 | + * Ensures relative imports have proper ./ or ../ prefix |
| 57 | + */ |
| 58 | +const ensureDotRelative = (p: string) => { |
| 59 | + if (p.startsWith("./") || p.startsWith("../")) { |
| 60 | + return p; |
| 61 | + } |
| 62 | + if (p.startsWith("/")) { |
| 63 | + return `.${p}`; |
| 64 | + } |
| 65 | + return `./${p}`; |
| 66 | +}; |
| 67 | +``` |
| 68 | + |
| 69 | +### Updated Functions |
| 70 | + |
| 71 | +1. **`getPathWithoutExtension`**: Now returns POSIX-normalized paths |
| 72 | +2. **Entry file matching**: Uses POSIX normalization for consistent comparison |
| 73 | +3. **`cleanPath`**: Replaced with `normalizeImportPath` for better normalization |
| 74 | +4. **`replaceAliasInPath`**: All import specifier construction now uses POSIX normalization |
| 75 | + |
| 76 | +## Testing |
| 77 | + |
| 78 | +### Automated Tests |
| 79 | + |
| 80 | +The fix includes comprehensive tests to verify Windows path handling: |
| 81 | + |
| 82 | +- **`test/src/windows-path-test.ts`**: Basic Windows path functionality |
| 83 | +- **`test/src/windows-simulation-test.ts`**: Simulates various Windows path scenarios |
| 84 | +- **`test/src/backslash-detector.ts`**: Detects any remaining backslashes |
| 85 | + |
| 86 | +### Manual Testing |
| 87 | + |
| 88 | +Run the comprehensive Windows path test: |
| 89 | + |
| 90 | +```bash |
| 91 | +node test-windows-comprehensive.js |
| 92 | +``` |
| 93 | + |
| 94 | +This script tests: |
| 95 | + |
| 96 | +- Windows-style relative paths (`..\utils\foo`) |
| 97 | +- Mixed separators (`..\utils/foo`) |
| 98 | +- Redundant path segments (`..\utils\.\foo`) |
| 99 | +- Absolute paths (`C:\project\src\utils\foo`) |
| 100 | +- Complex nested paths |
| 101 | +- Edge cases |
| 102 | + |
| 103 | +### CI/CD Testing |
| 104 | + |
| 105 | +GitHub Actions workflow (`.github/workflows/windows-test.yml`) includes: |
| 106 | + |
| 107 | +- **Windows-specific testing**: Runs on `windows-latest` runners |
| 108 | +- **Cross-platform validation**: Tests on Ubuntu, macOS, and Windows |
| 109 | +- **Backslash detection**: PowerShell script to verify no backslashes in output |
| 110 | +- **Artifact preservation**: Saves test outputs for inspection |
| 111 | + |
| 112 | +## Verification |
| 113 | + |
| 114 | +### Build Output Inspection |
| 115 | + |
| 116 | +After building, verify that all generated files use POSIX separators: |
| 117 | + |
| 118 | +```bash |
| 119 | +# Check for any remaining backslashes in import statements |
| 120 | +grep -r "from.*\\\\" test/dist/ || echo "✅ No backslashes in ESM imports" |
| 121 | +grep -r "require.*\\\\" test/dist/ || echo "✅ No backslashes in CJS requires" |
| 122 | +``` |
| 123 | + |
| 124 | +### Runtime Testing |
| 125 | + |
| 126 | +The plugin's output should work correctly on all platforms: |
| 127 | + |
| 128 | +```bash |
| 129 | +# Test ESM output |
| 130 | +node test/dist/esm/index.mjs |
| 131 | + |
| 132 | +# Test CJS output |
| 133 | +node test/dist/cjs/index.cjs |
| 134 | +``` |
| 135 | + |
| 136 | +## Benefits |
| 137 | + |
| 138 | +1. **Cross-platform compatibility**: Works consistently on Windows, macOS, and Linux |
| 139 | +2. **ESM/CJS compliance**: All import specifiers use valid POSIX separators |
| 140 | +3. **Module loading reliability**: No more import failures due to backslashes |
| 141 | +4. **Maintainability**: Centralized path normalization logic |
| 142 | + |
| 143 | +## Migration |
| 144 | + |
| 145 | +### For Existing Users |
| 146 | + |
| 147 | +No breaking changes - the fix is backward compatible and improves reliability. |
| 148 | + |
| 149 | +### For Contributors |
| 150 | + |
| 151 | +When adding new path manipulation logic: |
| 152 | + |
| 153 | +1. **Always use POSIX helpers**: Use `toPosix()`, `normalizeImportPath()`, `ensureDotRelative()` |
| 154 | +2. **Test on Windows**: Ensure CI includes Windows testing |
| 155 | +3. **Avoid direct path insertion**: Never insert Node.js path results directly into import specifiers |
| 156 | + |
| 157 | +## Related Issues |
| 158 | + |
| 159 | +- Fixes import specifier generation on Windows |
| 160 | +- Ensures consistent output across platforms |
| 161 | +- Maintains compatibility with existing alias configurations |
| 162 | +- Preserves all existing plugin functionality |
0 commit comments