-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
371 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`Node10 and Node16 Default Exports Types > Mixed Declaration Types 1`] = ` | ||
[ | ||
[ | ||
"index.d.cts", | ||
[ | ||
{ | ||
"code": "export type { A, B, C, Options }", | ||
"end": 259, | ||
"exports": " A, B, C, Options", | ||
"names": [ | ||
"A", | ||
"B", | ||
"C", | ||
"Options", | ||
], | ||
"specifier": undefined, | ||
"start": 227, | ||
"type": "named", | ||
}, | ||
], | ||
[], | ||
], | ||
[ | ||
"index.d.ts", | ||
[ | ||
{ | ||
"code": "export type { A, B, C, Options }", | ||
"end": 259, | ||
"exports": " A, B, C, Options", | ||
"names": [ | ||
"A", | ||
"B", | ||
"C", | ||
"Options", | ||
], | ||
"specifier": undefined, | ||
"start": 227, | ||
"type": "named", | ||
}, | ||
], | ||
[], | ||
], | ||
] | ||
`; | ||
|
||
exports[`Node10 and Node16 Default Exports Types > Re-Export Types 1`] = ` | ||
[ | ||
[ | ||
"index.d.cts", | ||
[], | ||
[ | ||
{ | ||
"code": "export { A, B, CC as C, CC } from './types.cjs'", | ||
"end": 86, | ||
"exports": " A, B, CC as C, CC", | ||
"names": [ | ||
"A", | ||
"B", | ||
"C", | ||
"CC", | ||
], | ||
"specifier": "./types.cjs", | ||
"start": 39, | ||
"type": "named", | ||
}, | ||
{ | ||
"code": "export { Options }", | ||
"end": 177, | ||
"exports": " Options", | ||
"name": "Options", | ||
"names": [ | ||
"Options", | ||
], | ||
"specifier": undefined, | ||
"start": 159, | ||
"type": "named", | ||
}, | ||
], | ||
], | ||
[ | ||
"index.d.ts", | ||
[], | ||
[ | ||
{ | ||
"code": "export { A, B, CC as C, CC } from './types.js'", | ||
"end": 84, | ||
"exports": " A, B, CC as C, CC", | ||
"names": [ | ||
"A", | ||
"B", | ||
"C", | ||
"CC", | ||
], | ||
"specifier": "./types.js", | ||
"start": 38, | ||
"type": "named", | ||
}, | ||
{ | ||
"code": "export { Options }", | ||
"end": 175, | ||
"exports": " Options", | ||
"name": "Options", | ||
"names": [ | ||
"Options", | ||
], | ||
"specifier": undefined, | ||
"start": 157, | ||
"type": "named", | ||
}, | ||
], | ||
], | ||
[ | ||
"types.d.cts", | ||
[ | ||
{ | ||
"code": "export type { A, B, C, C as CC, Options }", | ||
"end": 200, | ||
"exports": " A, B, C, C as CC, Options", | ||
"names": [ | ||
"A", | ||
"B", | ||
"C", | ||
"CC", | ||
"Options", | ||
], | ||
"specifier": undefined, | ||
"start": 159, | ||
"type": "named", | ||
}, | ||
], | ||
[], | ||
], | ||
[ | ||
"types.d.ts", | ||
[ | ||
{ | ||
"code": "export type { A, B, C, C as CC, Options }", | ||
"end": 200, | ||
"exports": " A, B, C, C as CC, Options", | ||
"names": [ | ||
"A", | ||
"B", | ||
"C", | ||
"CC", | ||
"Options", | ||
], | ||
"specifier": undefined, | ||
"start": 159, | ||
"type": "named", | ||
}, | ||
], | ||
[], | ||
], | ||
] | ||
`; |
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,89 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { build } from "../src"; | ||
import { resolve } from "pathe"; | ||
import { fileURLToPath } from "node:url"; | ||
import { readdir, readFile } from "node:fs/promises"; | ||
import { type ESMExport, findExports, findTypeExports } from "mlly"; | ||
|
||
describe("Node10 and Node16 Default Exports Types", () => { | ||
const dtsFiles = /\.d\.(c)?ts$/; | ||
|
||
async function readDtsFiles( | ||
dist: string, | ||
): Promise<[name: string, types: ESMExport[], exports: ESMExport[]][]> { | ||
const files = await readdir(dist).then((files) => | ||
files.filter((f) => dtsFiles.test(f)).map((f) => [f, resolve(dist, f)]), | ||
); | ||
return await Promise.all( | ||
files.map(async ([name, path]) => { | ||
const content = await readFile(path, "utf8"); | ||
return [name, findTypeExports(content), findExports(content)]; | ||
}), | ||
); | ||
} | ||
|
||
it("Mixed Declaration Types", async () => { | ||
const root = resolve( | ||
fileURLToPath(import.meta.url), | ||
"../cjs-types-fixture/mixed-declarations", | ||
); | ||
await build(root, false); | ||
const files = await readDtsFiles(resolve(root, "dist")); | ||
expect(files).toHaveLength(2); | ||
for (const [name, types, exports] of files) { | ||
expect(exports).toHaveLength(0); | ||
expect(types).toHaveLength(1); | ||
expect( | ||
types.find((e) => e.names.includes("default")), | ||
`${name} should not have a default export`, | ||
).toBeUndefined(); | ||
} | ||
expect(files).toMatchSnapshot(); | ||
}); | ||
|
||
it("Re-Export Types", async () => { | ||
const warnings: string[] = []; | ||
const root = resolve( | ||
fileURLToPath(import.meta.url), | ||
"../cjs-types-fixture/reexport-types", | ||
); | ||
await build(root, false, { | ||
hooks: { | ||
"rollup:options": (_, options) => { | ||
const _onwarn = options.onwarn; | ||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type | ||
options.onwarn = (warning, handler) => { | ||
if (warning.code === "EMPTY_BUNDLE") { | ||
warnings.push(warning.message); | ||
return; | ||
} | ||
return _onwarn?.(warning, handler); | ||
}; | ||
}, | ||
}, | ||
}); | ||
expect(warnings).toHaveLength(2); | ||
expect(warnings[0]).toBe('Generated an empty chunk: "types".'); | ||
expect(warnings[1]).toBe('Generated an empty chunk: "types".'); | ||
const files = await readDtsFiles(resolve(root, "dist")); | ||
expect(files).toHaveLength(4); | ||
for (const [name, types, exports] of files) { | ||
if (name.startsWith("types")) { | ||
expect(exports).toHaveLength(0); | ||
expect(types).toHaveLength(1); | ||
expect( | ||
types.find((e) => e.names.includes("default")), | ||
`${name} should not have a default export`, | ||
).toBeUndefined(); | ||
} else { | ||
expect(exports).toHaveLength(2); | ||
expect(types).toHaveLength(0); | ||
expect( | ||
exports.find((e) => e.names.includes("default")), | ||
`${name} should not have a default export`, | ||
).toBeUndefined(); | ||
} | ||
} | ||
expect(files).toMatchSnapshot(); | ||
}); | ||
}); |
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,12 @@ | ||
import { defineBuildConfig } from "../../../src"; | ||
|
||
export default defineBuildConfig({ | ||
entries: ["./index.ts"], | ||
declaration: true, | ||
clean: true, | ||
// avoid exit code 1 on warnings | ||
failOnWarn: false, | ||
rollup: { | ||
emitCJS: 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,18 @@ | ||
export interface A { | ||
name: string; | ||
} | ||
export interface B { | ||
name: string; | ||
} | ||
export interface C { | ||
name: string; | ||
} | ||
export interface Options { | ||
a?: A; | ||
b?: B; | ||
c?: C; | ||
} | ||
|
||
export default function plugin(options: Options = {}): Options { | ||
return options; | ||
} |
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,24 @@ | ||
{ | ||
"name": "cjs-types-mixed-declarations-fixture", | ||
"version": "0.0.0", | ||
"private": "true", | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"import": { | ||
"types": "./dist/index.d.mts", | ||
"default": "./dist/index.d.mjs" | ||
}, | ||
"require": { | ||
"types": "./dist/index.d.cts", | ||
"default": "./dist/index.d.cjs" | ||
} | ||
} | ||
}, | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
] | ||
} |
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,12 @@ | ||
import { defineBuildConfig } from "../../../src"; | ||
|
||
export default defineBuildConfig({ | ||
entries: ["./index.ts", "./types.ts"], | ||
declaration: true, | ||
clean: true, | ||
// avoid exit code 1 on warnings | ||
failOnWarn: false, | ||
rollup: { | ||
emitCJS: 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,7 @@ | ||
import type { Options } from "./types"; | ||
|
||
export type * from "./types"; | ||
|
||
export default function plugin(options: Options = {}): Options { | ||
return options; | ||
} |
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,37 @@ | ||
{ | ||
"name": "cjs-types-reexport-types-fixture", | ||
"version": "0.0.0", | ||
"private": "true", | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"import": { | ||
"types": "./dist/index.d.mts", | ||
"default": "./dist/index.d.mjs" | ||
}, | ||
"require": { | ||
"types": "./dist/index.d.cts", | ||
"default": "./dist/index.d.cjs" | ||
} | ||
}, | ||
"./types": { | ||
"types": { | ||
"import": "./dist/types.d.mts", | ||
"require": "./dist/types.d.cts" | ||
} | ||
} | ||
}, | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts", | ||
"typesVersions": { | ||
"*": { | ||
"types": [ | ||
"./dist/types.d.ts" | ||
] | ||
} | ||
}, | ||
"files": [ | ||
"dist" | ||
] | ||
} |
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,15 @@ | ||
export interface A { | ||
name: string; | ||
} | ||
export interface B { | ||
name: string; | ||
} | ||
export interface C { | ||
name: string; | ||
} | ||
export type { C as CC }; | ||
export interface Options { | ||
a?: A; | ||
b?: B; | ||
c?: C; | ||
} |