Skip to content

Commit e6933a7

Browse files
committed
fix(TS): correctly generate type defs for scoped pacakges
1 parent 44f1969 commit e6933a7

File tree

3 files changed

+94
-51
lines changed

3 files changed

+94
-51
lines changed

src/config/rollup.config.js

+29-47
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const {
77
nodeResolve,
88
} = require('@rollup/plugin-node-resolve')
99
const replace = require('@rollup/plugin-replace')
10-
const glob = require('glob')
1110
const camelcase = require('lodash.camelcase')
1211
const {terser} = require('rollup-plugin-terser')
1312
const nodeBuiltIns = require('rollup-plugin-node-builtins')
@@ -21,7 +20,8 @@ const {
2120
hasDep,
2221
hasTypescript,
2322
parseEnv,
24-
fromRoot,
23+
getRollupInputs,
24+
getRollupOutput,
2525
uniq,
2626
writeExtraEntry,
2727
} = require('../utils')
@@ -51,27 +51,6 @@ const deps = Object.keys(pkg.dependencies || {})
5151
const peerDeps = Object.keys(pkg.peerDependencies || {})
5252
const defaultExternal = umd ? peerDeps : deps.concat(peerDeps)
5353

54-
const input = glob.sync(
55-
fromRoot(
56-
process.env.BUILD_INPUT ||
57-
(hasTypescript ? 'src/index.{js,ts,tsx}' : 'src/index.js'),
58-
),
59-
)
60-
const codeSplitting = input.length > 1
61-
62-
if (
63-
codeSplitting &&
64-
uniq(input.map(single => path.basename(single))).length !== input.length
65-
) {
66-
throw new Error(
67-
'Filenames of code-splitted entries should be unique to get deterministic output filenames.' +
68-
`\nReceived those: ${input}.`,
69-
)
70-
}
71-
72-
const filenameSuffix = process.env.BUILD_FILENAME_SUFFIX || ''
73-
const filenamePrefix =
74-
process.env.BUILD_FILENAME_PREFIX || (isPreact ? 'preact/' : '')
7554
const globals = parseEnv(
7655
'BUILD_GLOBALS',
7756
isPreact ? Object.assign(defaultGlobals, {preact: 'preact'}) : defaultGlobals,
@@ -102,30 +81,6 @@ function externalPredicate(id) {
10281
return isDep || (!isRelative && !path.isAbsolute(id)) || isNodeModule
10382
}
10483

105-
const filename = [
106-
pkg.name,
107-
filenameSuffix,
108-
`.${format}`,
109-
minify ? '.min' : null,
110-
'.js',
111-
]
112-
.filter(Boolean)
113-
.join('')
114-
115-
const dirpath = path.join(...[filenamePrefix, 'dist'].filter(Boolean))
116-
117-
const output = [
118-
{
119-
name,
120-
...(codeSplitting
121-
? {dir: path.join(dirpath, format)}
122-
: {file: path.join(dirpath, filename)}),
123-
format: esm ? 'es' : format,
124-
exports: esm ? 'named' : 'auto',
125-
globals,
126-
},
127-
]
128-
12984
const useBuiltinConfig =
13085
!hasFile('.babelrc') &&
13186
!hasFile('.babelrc.js') &&
@@ -150,6 +105,33 @@ const extensions = hasTypescript
150105
? [...nodeResolveDefaults.extensions, '.ts', '.tsx']
151106
: nodeResolveDefaults.extensions
152107

108+
const input = getRollupInputs()
109+
const codeSplitting = input.length > 1
110+
111+
if (
112+
codeSplitting &&
113+
uniq(input.map(single => path.basename(single))).length !== input.length
114+
) {
115+
throw new Error(
116+
'Filenames of code-splitted entries should be unique to get deterministic output filenames.' +
117+
`\nReceived those: ${input}.`,
118+
)
119+
}
120+
121+
const {dirpath, filename} = getRollupOutput()
122+
123+
const output = [
124+
{
125+
name,
126+
...(codeSplitting
127+
? {dir: path.join(dirpath, format)}
128+
: {file: path.join(dirpath, filename)}),
129+
format: esm ? 'es' : format,
130+
exports: esm ? 'named' : 'auto',
131+
globals,
132+
},
133+
]
134+
153135
module.exports = {
154136
input: codeSplitting ? input : input[0],
155137
output,

src/scripts/build/rollup.js

+31-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const {
1212
writeExtraEntry,
1313
hasTypescript,
1414
generateTypeDefs,
15+
getRollupInputs,
16+
getRollupOutput,
1517
} = require('../../utils')
1618

1719
const crossEnv = resolveBin('cross-env')
@@ -86,11 +88,36 @@ function go() {
8688
result = generateTypeDefs(fromRoot('dist'))
8789
if (result.status !== 0) return result.status
8890

91+
const rollupInputs = getRollupInputs()
92+
const typeDefFiles = rollupInputs.map(input => {
93+
return input
94+
.replace(path.join(process.cwd(), 'src'), 'dist')
95+
.replace(/\.(t|j)sx?$/, '.d.ts')
96+
})
97+
8998
for (const format of formats) {
90-
const [formatFile] = glob.sync(fromRoot(`dist/*.${format}.js`))
91-
const {name} = path.parse(formatFile)
92-
// make a .d.ts file for every generated file that re-exports index.d.ts
93-
fs.writeFileSync(fromRoot('dist', `${name}.d.ts`), 'export * from ".";\n')
99+
const {dirpath, filename} = getRollupOutput(format)
100+
101+
const isCodesplitting = rollupInputs.length > 1
102+
103+
const outputs = isCodesplitting
104+
? glob.sync(fromRoot(path.join(dirpath, format, '*.js')))
105+
: [fromRoot(path.join(dirpath, filename))]
106+
107+
for (const output of outputs) {
108+
const {name, dir} = path.parse(output)
109+
const typeDef = isCodesplitting
110+
? typeDefFiles.find(f => path.basename(f) === `${name}.d.ts`)
111+
: 'dist/index.d.ts'
112+
const relativePath = path
113+
.join(path.relative(dir, process.cwd()), typeDef)
114+
.replace(/\.d\.ts$/, '')
115+
// make a .d.ts file for every generated file that re-exports index.d.ts
116+
fs.writeFileSync(
117+
path.join(dir, `${name}.d.ts`),
118+
`export * from "${relativePath}";\n`,
119+
)
120+
}
94121
}
95122

96123
// because typescript generates type defs for ignored files, we need to

src/utils.js

+34
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const rimraf = require('rimraf')
55
const mkdirp = require('mkdirp')
66
const arrify = require('arrify')
77
const has = require('lodash.has')
8+
const glob = require('glob')
89
const readPkgUp = require('read-pkg-up')
910
const which = require('which')
1011
const {cosmiconfigSync} = require('cosmiconfig')
@@ -182,6 +183,37 @@ function generateTypeDefs(outputDir) {
182183
)
183184
}
184185

186+
function getRollupInputs() {
187+
const buildInputGlob =
188+
process.env.BUILD_INPUT ||
189+
(hasTypescript ? 'src/index.{js,ts,tsx}' : 'src/index.js')
190+
const input = glob.sync(fromRoot(buildInputGlob))
191+
if (!input.length) {
192+
throw new Error(`Unable to find files with this glob: ${buildInputGlob}`)
193+
}
194+
return input
195+
}
196+
197+
function getRollupOutput(format = process.env.BUILD_FORMAT) {
198+
const minify = parseEnv('BUILD_MINIFY', false)
199+
const filenameSuffix = process.env.BUILD_FILENAME_SUFFIX || ''
200+
const filename = [
201+
pkg.name,
202+
filenameSuffix,
203+
`.${format}`,
204+
minify ? '.min' : null,
205+
'.js',
206+
]
207+
.filter(Boolean)
208+
.join('')
209+
210+
const isPreact = parseEnv('BUILD_PREACT', false)
211+
const filenamePrefix =
212+
process.env.BUILD_FILENAME_PREFIX || (isPreact ? 'preact/' : '')
213+
const dirpath = path.join(...[filenamePrefix, 'dist'].filter(Boolean))
214+
return {dirpath, filename}
215+
}
216+
185217
module.exports = {
186218
appDirectory,
187219
fromRoot,
@@ -207,4 +239,6 @@ module.exports = {
207239
uniq,
208240
writeExtraEntry,
209241
generateTypeDefs,
242+
getRollupInputs,
243+
getRollupOutput,
210244
}

0 commit comments

Comments
 (0)