-
Notifications
You must be signed in to change notification settings - Fork 30.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module: resolve format for all situations with module detection on
Co-authored-by: Geoffrey Booth <webadmin@geoffreybooth.com>
- Loading branch information
1 parent
c681f57
commit 5753afc
Showing
7 changed files
with
167 additions
and
53 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,28 @@ | ||
import { writeSync } from "node:fs"; | ||
|
||
export async function resolve(specifier, context, next) { | ||
const result = await next(specifier, context); | ||
if (specifier.startsWith("file://")) { | ||
writeSync(1, `Resolved format: ${result.format}\n`); | ||
} | ||
return result; | ||
} | ||
|
||
export async function load(url, context, next) { | ||
const output = await next(url, context); | ||
writeSync(1, `Loaded original format: ${output.format}\n`); | ||
|
||
let source = `${output.source}` | ||
|
||
// This is a very incomplete and naively done implementation for testing purposes only | ||
if (source?.includes('export default')) { | ||
source = source.replace('export default', 'module.exports ='); | ||
|
||
source += '\nconsole.log(`Evaluated format: ${this === undefined ? "module" : "commonjs"}`);'; | ||
|
||
output.source = source; | ||
output.format = 'commonjs'; | ||
} | ||
|
||
return output; | ||
} |
1 change: 1 addition & 0 deletions
1
test/fixtures/es-modules/package-without-type/imports-nonexistent.js
This file contains 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 @@ | ||
import "./does-not-exist.js"; |