Skip to content
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

fix(core): enfore to esm if file extensions match #55

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading