Skip to content

Commit ded90ac

Browse files
Fix TypeScript emit target (#629)
* Fix TypeScript including build artefacts * Fix declaration files emitted into wrong directory
1 parent 0832977 commit ded90ac

7 files changed

Lines changed: 96 additions & 1 deletion

File tree

src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,10 +562,14 @@ function createConfig(options, entry, format, writeMeta) {
562562
typescript({
563563
typescript: require('typescript'),
564564
cacheRoot: `./node_modules/.cache/.rts2_cache_${format}`,
565+
useTsconfigDeclarationDir: true,
565566
tsconfigDefaults: {
566567
compilerOptions: {
567568
sourceMap: options.sourcemap,
568569
declaration: true,
570+
declarationDir: dirname(
571+
pkg.types || pkg.typings || options.output,
572+
),
569573
jsx: 'react',
570574
jsxFactory:
571575
// TypeScript fails to resolve Fragments when jsxFactory
@@ -574,6 +578,7 @@ function createConfig(options, entry, format, writeMeta) {
574578
? undefined
575579
: options.jsx || 'h',
576580
},
581+
files: options.entries,
577582
},
578583
tsconfig: options.tsconfig,
579584
tsconfigOverride: {

test/__snapshots__/index.test.js.snap

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,6 +2332,64 @@ exports[`fixtures build shebang with microbundle 5`] = `
23322332
"
23332333
`;
23342334
2335+
exports[`fixtures build ts-declaration with microbundle 1`] = `
2336+
"Used script: microbundle
2337+
2338+
Directory tree:
2339+
2340+
ts-declaration
2341+
dist
2342+
index.esm.js
2343+
index.esm.js.map
2344+
index.js
2345+
index.js.map
2346+
index.umd.js
2347+
index.umd.js.map
2348+
node_modules
2349+
package.json
2350+
src
2351+
index.tsx
2352+
tsconfig.json
2353+
types
2354+
index.d.ts
2355+
2356+
2357+
Build \\"tsDeclarations\\" to dist:
2358+
55 B: index.js.gz
2359+
33 B: index.js.br
2360+
61 B: index.esm.js.gz
2361+
45 B: index.esm.js.br
2362+
172 B: index.umd.js.gz
2363+
126 B: index.umd.js.br"
2364+
`;
2365+
2366+
exports[`fixtures build ts-declaration with microbundle 2`] = `6`;
2367+
2368+
exports[`fixtures build ts-declaration with microbundle 3`] = `
2369+
"function n(){return 42}export{n as foo};
2370+
//# sourceMappingURL=index.esm.js.map
2371+
"
2372+
`;
2373+
2374+
exports[`fixtures build ts-declaration with microbundle 4`] = `
2375+
"exports.foo=function(){return 42};
2376+
//# sourceMappingURL=index.js.map
2377+
"
2378+
`;
2379+
2380+
exports[`fixtures build ts-declaration with microbundle 5`] = `
2381+
"!function(e,n){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?n(exports):\\"function\\"==typeof define&&define.amd?define([\\"exports\\"],n):n((e=e||self).tsDeclarations={})}(this,function(e){e.foo=function(){return 42}});
2382+
//# sourceMappingURL=index.umd.js.map
2383+
"
2384+
`;
2385+
2386+
exports[`fixtures build ts-declaration with microbundle 6`] = `1`;
2387+
2388+
exports[`fixtures build ts-declaration with microbundle 7`] = `
2389+
"export declare function foo(): number;
2390+
"
2391+
`;
2392+
23352393
exports[`fixtures build ts-jsx with microbundle 1`] = `
23362394
"Used script: microbundle build --jsx 'React.createElement'
23372395
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "ts-declarations",
3+
"main": "dist/index.js",
4+
"types": "types/index.d.ts",
5+
"source": "src/index.tsx"
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function foo() {
2+
return 42;
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerOptions": {
3+
"noEmit": true,
4+
"allowJs": true
5+
}
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export declare function foo(): number;

test/index.test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,29 @@ describe('fixtures', () => {
6363
expect(files.length).toMatchSnapshot();
6464
// we don't realy care about the content of a sourcemap
6565
files
66-
.filter(file => !/\.map$/.test(file))
66+
.filter(
67+
file =>
68+
!/\.map$/.test(file) &&
69+
!fs.lstatSync(resolve(dist, file)).isDirectory(),
70+
)
6771
.sort(file => (/modern/.test(file) ? 1 : 0))
6872
.forEach(file => {
6973
expect(
7074
fs.readFileSync(resolve(dist, file)).toString('utf8'),
7175
).toMatchSnapshot();
7276
});
77+
78+
// TypeScript declaration files
79+
const types = resolve(`${fixturePath}/types`);
80+
if (fs.existsSync(types)) {
81+
const declarations = fs.readdirSync(types);
82+
expect(declarations.length).toMatchSnapshot();
83+
declarations.forEach(file => {
84+
expect(
85+
fs.readFileSync(resolve(types, file)).toString('utf8'),
86+
).toMatchSnapshot();
87+
});
88+
}
7389
},
7490
TEST_TIMEOUT,
7591
);

0 commit comments

Comments
 (0)