-
-
Notifications
You must be signed in to change notification settings - Fork 61
Fixes #79 Resolved ERR_MODULE_NOT_FOUND during Vitest ESM resolution by adding explicit .js extensions to ESM imports. #106
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
Open
ankitrraj
wants to merge
6
commits into
StabilityNexus:main
Choose a base branch
from
ankitrraj:fix/wallet-component-esm-imports
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2b11dc0
docs: Add fix documentation and script for wallet-svelte-component ES…
ankitrraj 3d94563
docs: Add step-by-step guide for fixing wallet-svelte-component upstream
ankitrraj 3f749f6
docs: Add comprehensive summary of ESM fix work
ankitrraj d344c71
fix: Handle mixed quote styles in import statements
ankitrraj a988636
docs: Remove verbose documentation files
ankitrraj ff13137
fixed
ankitrraj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| /** | ||
| * Post-build script to fix ESM imports by adding .js extensions | ||
| * | ||
| * This script walks through the dist directory and adds .js extensions | ||
| * to all relative imports to ensure compatibility with strict Node.js ESM. | ||
| * | ||
| * Usage: node fix-imports-script.mjs [dist-directory] | ||
| */ | ||
|
|
||
| import { readdir, readFile, writeFile } from 'fs/promises'; | ||
| import { join, dirname } from 'path'; | ||
| import { fileURLToPath } from 'url'; | ||
|
|
||
| const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
|
|
||
| // Get dist directory from command line argument or use default | ||
| const distDir = process.argv[2] || join(__dirname, 'node_modules', 'wallet-svelte-component', 'dist'); | ||
|
|
||
| console.log(`🔧 Fixing ESM imports in: ${distDir}\n`); | ||
|
|
||
| let filesFixed = 0; | ||
| let importsFixed = 0; | ||
|
|
||
| /** | ||
| * Recursively process all .js files in a directory | ||
| */ | ||
| async function fixImportsInDirectory(dir) { | ||
| try { | ||
| const files = await readdir(dir, { withFileTypes: true }); | ||
|
|
||
| for (const file of files) { | ||
| const fullPath = join(dir, file.name); | ||
|
|
||
| if (file.isDirectory()) { | ||
| await fixImportsInDirectory(fullPath); | ||
| } else if (file.name.endsWith('.js')) { | ||
| await fixImportsInFile(fullPath); | ||
| } | ||
| } | ||
| } catch (error) { | ||
| console.error(`❌ Error processing directory ${dir}:`, error.message); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Fix imports in a single JavaScript file | ||
| */ | ||
| async function fixImportsInFile(filePath) { | ||
| try { | ||
| let content = await readFile(filePath, 'utf-8'); | ||
| const originalContent = content; | ||
| let fileImportCount = 0; | ||
|
|
||
| // Pattern 1: export * from './path' | ||
| content = content.replace( | ||
| /export\s+\*\s+from\s+['"](\.[^'"]+)(?<!\.js|\.svelte|\.json)['"]/g, | ||
| (match, path) => { | ||
| fileImportCount++; | ||
| return `export * from '${path}.js'`; | ||
| } | ||
| ); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| // Pattern 2: export { ... } from './path' | ||
| content = content.replace( | ||
| /export\s+{[^}]+}\s+from\s+['"](\.[^'"]+)(?<!\.js|\.svelte|\.json)['"]/g, | ||
| (match, path) => { | ||
| fileImportCount++; | ||
| const quote = match.includes('"') && match.lastIndexOf('"') > match.lastIndexOf("'") ? '"' : "'"; | ||
| const exportPart = match.substring(0, match.lastIndexOf(quote)); | ||
| return `${exportPart}${quote}${path}.js${quote}`; | ||
| } | ||
| ); | ||
|
|
||
| // Pattern 3: import ... from './path' | ||
| content = content.replace( | ||
| /import\s+.*?from\s+['"](\.[^'"]+)(?<!\.js|\.svelte|\.json)['"]/g, | ||
| (match, path) => { | ||
| fileImportCount++; | ||
| const quote = match.includes('"') && match.lastIndexOf('"') > match.lastIndexOf("'") ? '"' : "'"; | ||
| const importPart = match.substring(0, match.lastIndexOf(quote)); | ||
| return `${importPart}${quote}${path}.js${quote}`; | ||
| } | ||
| ); | ||
|
|
||
| // Pattern 4: import './path' | ||
| content = content.replace( | ||
| /import\s+['"](\.[^'"]+)(?<!\.js|\.svelte|\.json)['"]/g, | ||
| (match, path) => { | ||
| fileImportCount++; | ||
| return `import '${path}.js'`; | ||
| } | ||
| ); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| // Only write if changes were made | ||
| if (content !== originalContent) { | ||
| await writeFile(filePath, content, 'utf-8'); | ||
| filesFixed++; | ||
| importsFixed += fileImportCount; | ||
| console.log(`✅ Fixed ${fileImportCount} imports in: ${filePath}`); | ||
| } | ||
| } catch (error) { | ||
| console.error(`❌ Error processing file ${filePath}:`, error.message); | ||
| } | ||
| } | ||
|
|
||
| // Run the script | ||
| (async () => { | ||
| try { | ||
| console.log('Starting import fixes...\n'); | ||
| await fixImportsInDirectory(distDir); | ||
| console.log(`\n🎉 Complete! Fixed ${importsFixed} imports across ${filesFixed} files.`); | ||
| } catch (error) { | ||
| console.error('❌ Script failed:', error); | ||
| process.exit(1); | ||
| } | ||
| })(); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Architectural concern: Patching node_modules is not sustainable.
The default path targets
node_modules/wallet-svelte-component/dist, meaning this script modifies an installed dependency. Changes to node_modules are lost on reinstall, requiring this script to run after everynpm install.Consider these more maintainable alternatives:
wallet-svelte-componentpackage to ship with proper ESM imports