-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: treat
default
condition as esm or cjs (#657)
Co-authored-by: Jiachi Liu <inbox@huozhi.im>
- Loading branch information
Showing
14 changed files
with
299 additions
and
45 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
67 changes: 67 additions & 0 deletions
67
test/integration/default-default-export-different-ext/index.test.ts
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,67 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { | ||
getFileNamesFromDirectory, | ||
createJob, | ||
getFileContents, | ||
} from '../../testing-utils' | ||
|
||
describe('integration - default-default-export-different-ext', () => { | ||
const { distDir } = createJob({ directory: __dirname }) | ||
|
||
it('should not export esm module in cjs file', async () => { | ||
const distFiles = await getFileNamesFromDirectory(distDir) | ||
expect(distFiles).toEqual([ | ||
'a.cjs', | ||
'a.d.ts', | ||
'a.js', | ||
'b.cjs', | ||
'b.d.ts', | ||
'b.js', | ||
'c.cjs', | ||
]) | ||
|
||
const contents = await getFileContents(distDir) | ||
// TODO: add to contents | ||
for (const file in contents) { | ||
if (file.includes('.d.')) { | ||
delete contents[file] | ||
} | ||
} | ||
expect(contents).toMatchInlineSnapshot(` | ||
{ | ||
"a.cjs": "var b_cjs = require('./b.cjs'); | ||
const a = 'a'; | ||
exports.a = a; | ||
Object.keys(b_cjs).forEach(function (k) { | ||
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, { | ||
enumerable: true, | ||
get: function () { return b_cjs[k]; } | ||
}); | ||
}); | ||
", | ||
"a.js": "export * from './b.js'; | ||
const a = 'a'; | ||
export { a }; | ||
", | ||
"b.cjs": "const b = 'b'; | ||
exports.b = b; | ||
", | ||
"b.js": "const b = 'b'; | ||
export { b }; | ||
", | ||
"c.cjs": "var a_cjs = require('./a.cjs'); | ||
const c = \`c\${a_cjs.a}\`; | ||
exports.c = c; | ||
", | ||
} | ||
`) | ||
}) | ||
}) |
32 changes: 32 additions & 0 deletions
32
test/integration/default-default-export-different-ext/package.json
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,32 @@ | ||
{ | ||
"name": "default-default-esm", | ||
"type": "module", | ||
"exports": { | ||
"./a": { | ||
"import": { | ||
"types": "./dist/a.d.ts", | ||
"default": "./dist/a.js" | ||
}, | ||
"require": { | ||
"types": "./dist/a.d.cts", | ||
"default": "./dist/a.cjs" | ||
}, | ||
"default": "./dist/a.cjs" | ||
}, | ||
"./b": { | ||
"import": { | ||
"types": "./dist/b.d.ts", | ||
"default": "./dist/b.js" | ||
}, | ||
"require": { | ||
"types": "./dist/b.d.cts", | ||
"default": "./dist/b.cjs" | ||
}, | ||
"default": "./dist/b.cjs" | ||
}, | ||
"./c": { | ||
"default": "./dist/c.cjs" | ||
} | ||
}, | ||
"private": true | ||
} |
2 changes: 2 additions & 0 deletions
2
test/integration/default-default-export-different-ext/src/a.ts
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,2 @@ | ||
export * from './b' | ||
export const a = 'a' |
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 @@ | ||
export const b = 'b' |
3 changes: 3 additions & 0 deletions
3
test/integration/default-default-export-different-ext/src/c.ts
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,3 @@ | ||
import { a } from './a' | ||
|
||
export const c = `c${a}` |
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,54 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { | ||
getFileNamesFromDirectory, | ||
createJob, | ||
getFileContents, | ||
} from '../../testing-utils' | ||
|
||
describe('integration - default-default-export', () => { | ||
const { distDir } = createJob({ directory: __dirname }) | ||
|
||
it('should not export esm module in cjs file', async () => { | ||
const distFiles = await getFileNamesFromDirectory(distDir) | ||
expect(distFiles).toEqual([ | ||
'a.cjs', | ||
'a.d.ts', | ||
'a.js', | ||
'b.cjs', | ||
'b.d.ts', | ||
'b.js', | ||
'c.js', | ||
]) | ||
const contents = await getFileContents(distDir) | ||
expect(contents['a.cjs']).toMatchInlineSnapshot(` | ||
"var b_cjs = require('./b.cjs'); | ||
const a = 'a'; | ||
exports.a = a; | ||
Object.keys(b_cjs).forEach(function (k) { | ||
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, { | ||
enumerable: true, | ||
get: function () { return b_cjs[k]; } | ||
}); | ||
}); | ||
" | ||
`) | ||
expect(contents['a.js']).toMatchInlineSnapshot(` | ||
"export * from './b.js'; | ||
const a = 'a'; | ||
export { a }; | ||
" | ||
`) | ||
expect(contents['c.js']).toMatchInlineSnapshot(` | ||
"import { a } from './a.js'; | ||
const c = \`c\${a}\`; | ||
export { c }; | ||
" | ||
`) | ||
}) | ||
}) |
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,32 @@ | ||
{ | ||
"name": "default-default-esm", | ||
"type": "module", | ||
"exports": { | ||
"./a": { | ||
"import": { | ||
"types": "./dist/a.d.ts", | ||
"default": "./dist/a.js" | ||
}, | ||
"require": { | ||
"types": "./dist/a.d.cts", | ||
"default": "./dist/a.cjs" | ||
}, | ||
"default": "./dist/a.js" | ||
}, | ||
"./b": { | ||
"import": { | ||
"types": "./dist/b.d.ts", | ||
"default": "./dist/b.js" | ||
}, | ||
"require": { | ||
"types": "./dist/b.d.cts", | ||
"default": "./dist/b.cjs" | ||
}, | ||
"default": "./dist/b.js" | ||
}, | ||
"./c": { | ||
"default": "./dist/c.js" | ||
} | ||
}, | ||
"private": true | ||
} |
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,2 @@ | ||
export * from './b' | ||
export const a = 'a' |
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 @@ | ||
export const b = 'b' |
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,3 @@ | ||
import { a } from './a' | ||
|
||
export const c = `c${a}` |
Oops, something went wrong.