Skip to content

Commit 3f315a7

Browse files
committed
feat: copy .d.ts files
1 parent 5c69945 commit 3f315a7

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"chalk": "^4.1.0",
6060
"concurrently": "^6.0.0",
6161
"cosmiconfig": "^7.0.0",
62+
"cpy": "^8.1.2",
6263
"cross-env": "^7.0.3",
6364
"cross-spawn": "^7.0.3",
6465
"doctoc": "^2.0.0",

src/__tests__/utils.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ jest.mock('cosmiconfig', () => {
88
return {...cosmiconfigExports, cosmiconfigSync: jest.fn()}
99
})
1010

11-
let whichSyncMock, readPkgUpSyncMock
11+
jest.mock('cpy')
12+
13+
let whichSyncMock, readPkgUpSyncMock, cpy
1214

1315
beforeEach(() => {
1416
jest.resetModules()
1517
whichSyncMock = require('which').sync
1618
readPkgUpSyncMock = require('read-pkg-up').sync
19+
cpy = require('cpy')
1720
})
1821

1922
test('package is the package.json', () => {
@@ -160,15 +163,21 @@ test('hasLocalConfiguration returns true if a local config found and it is empty
160163
expect(require('../utils').hasLocalConfig('module')).toBe(true)
161164
})
162165

163-
test('should generate typescript definitions into provided folder', () => {
166+
test('should generate typescript definitions into provided folder', async () => {
164167
whichSyncMock.mockImplementationOnce(() => require.resolve('../'))
165168
const {sync: crossSpawnSyncMock} = require('cross-spawn')
166-
require('../utils').generateTypeDefs('destination folder')
169+
await require('../utils').generateTypeDefs('destination folder')
167170
expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1)
168171
const args = crossSpawnSyncMock.mock.calls[0][1]
169172
const outDirIndex = args.findIndex(arg => arg === '--outDir') + 1
170173

171174
expect(args[outDirIndex]).toBe('destination folder')
175+
176+
expect(cpy).toHaveBeenCalledTimes(1)
177+
expect(cpy).toHaveBeenCalledWith('**/*.d.ts', '../dist', {
178+
cwd: '/blah/src',
179+
parents: true,
180+
})
172181
})
173182

174183
function mockPkg({package: pkg = {}, path = '/blah/package.json'}) {

src/scripts/build/babel.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ if (noTypeDefinitions) {
5454
args = args.filter(a => a !== '--no-ts-defs')
5555
}
5656

57-
function go() {
57+
async function go() {
5858
let result = spawn.sync(
5959
resolveBin('@babel/cli', {executable: 'babel'}),
6060
[
@@ -73,18 +73,22 @@ function go() {
7373

7474
if (hasTypescript && !noTypeDefinitions) {
7575
console.log('Generating TypeScript definitions')
76-
result = generateTypeDefs(pathToOutDir)
77-
console.log('TypeScript definitions generated')
76+
result = await generateTypeDefs(pathToOutDir)
7877
if (result.status !== 0) return result.status
78+
console.log('TypeScript definitions generated')
7979
}
8080

8181
// because babel will copy even ignored files, we need to remove the ignored files
8282
const ignoredPatterns = (parsedArgs.ignore || builtInIgnore)
8383
.split(',')
8484
.map(pattern => path.join(pathToOutDir, pattern))
85+
86+
// type def files are compiled to an empty file and they're useless
87+
// so we'll get rid of those too.
88+
const typeDefCompiledFiles = path.join(pathToOutDir, '**/*.d.js')
8589
const ignoredFiles = ignoredPatterns.reduce(
8690
(all, pattern) => [...all, ...glob.sync(pattern)],
87-
[],
91+
[typeDefCompiledFiles],
8892
)
8993
ignoredFiles.forEach(ignoredFile => {
9094
rimraf.sync(ignoredFile)

src/utils.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const fs = require('fs')
22
const path = require('path')
3+
const cpy = require('cpy')
34
const spawn = require('cross-spawn')
45
const rimraf = require('rimraf')
56
const mkdirp = require('mkdirp')
@@ -169,8 +170,8 @@ function hasLocalConfig(moduleName, searchOptions = {}) {
169170
return result !== null
170171
}
171172

172-
function generateTypeDefs(outputDir) {
173-
return spawn.sync(
173+
async function generateTypeDefs(outputDir) {
174+
const result = spawn.sync(
174175
resolveBin('typescript', {executable: 'tsc'}),
175176
// prettier-ignore
176177
[
@@ -181,6 +182,10 @@ function generateTypeDefs(outputDir) {
181182
],
182183
{stdio: 'inherit'},
183184
)
185+
if (result.status !== 0) return result
186+
187+
await cpy('**/*.d.ts', '../dist', {cwd: fromRoot('src'), parents: true})
188+
return result
184189
}
185190

186191
function getRollupInputs() {

0 commit comments

Comments
 (0)