Skip to content

Commit

Permalink
fix(core): enfore to esm if file extensions match
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Jan 19, 2025
1 parent 7019825 commit 8cec5da
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const exportFromMts = 'exportFromMts'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
6 changes: 5 additions & 1 deletion packages/integrate-module-bundler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { baz } from './subdirectory/index'
import { Component } from './component'
import './js-module'
import babelGeneratedDoubleDefault from './babel-generated-double-default'

import { exportFromMts } from './enforce-mts/index.mjs'
const { foo: fooWithQuery } = await import(`./foo.js?q=${Date.now()}`)

await test('file-type should work', () => {
Expand Down Expand Up @@ -66,3 +66,7 @@ await test('import default from babel-generated cjs file', () => {
await test('resolve cjs in type module package', () => {
assert.equal(common, 'common')
})

await test('resolve mts in type commonjs package', () => {
assert.equal(exportFromMts, 'exportFromMts')
})
30 changes: 14 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,22 +420,20 @@ pub fn create_resolve<'env>(
let ext = p.extension().and_then(|ext| ext.to_str());

let format = ext
.and_then(|ext| {
if ext == "cjs" || ext == "cts" || ext == "node" {
None
} else {
resolution
.package_json()
.and_then(|p| p.r#type.as_ref())
.and_then(|t| t.as_str())
.and_then(|format| {
if format == "module" {
Some("module".to_owned())
} else {
None
}
})
}
.and_then(|ext| match ext {
"cjs" | "cts" | "node" => None,
"mts" | "mjs" => Some("module".to_owned()),
_ => resolution
.package_json()
.and_then(|p| p.r#type.as_ref())
.and_then(|t| t.as_str())
.and_then(|format| {
if format == "module" {
Some("module".to_owned())
} else {
None
}
}),
})
.unwrap_or_else(|| "commonjs".to_owned());
tracing::debug!(path = ?p, format = ?format);
Expand Down

0 comments on commit 8cec5da

Please sign in to comment.