Skip to content

Commit 98abfce

Browse files
committed
fix: export all types in types folder
There was an issue with the rollup config in which only two files: - src/index.ts - src/testing/index.ts were included as input for rollup. Rollup only traced references from these two files to generate the output bundle. In the types folder, there were exported types that weren't referred to by either of the two inputs. These types weren't bundled into the output. This PR aims to include all files in the types folder into the bundle output.
1 parent 9981939 commit 98abfce

File tree

5 files changed

+77
-5
lines changed

5 files changed

+77
-5
lines changed

package-lock.json

+4-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"cpy-cli": "^5.0.0",
8080
"dotenv-cli": "^7.3.0",
8181
"eslint": "^8.47.0",
82+
"fast-glob": "^3.3.2",
8283
"jest-fetch-mock": "^3.0.3",
8384
"magic-string": "^0.30.7",
8485
"npm-run-all": "^4.1.5",

rollup-multi-plugin.ts

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import type FastGlob from 'fast-glob'
2+
import fastGlob from 'fast-glob'
3+
import path from 'path'
4+
import type { Plugin } from 'rollup'
5+
6+
// This was taken from https://github.com/alfredosalzillo/rollup-plugin-multi-input
7+
// We maintain our copy here because rollup-plugin-multi-input has issues with exporting types
8+
9+
const pluginName = 'rollup-plugin-multi-input'
10+
11+
const isString = (value: unknown): value is string => typeof value === 'string'
12+
13+
/**
14+
* default multi-input Options
15+
* */
16+
const defaultOptions = {
17+
// `path.sep` is used for windows support
18+
relative: `src${path.sep}`,
19+
}
20+
21+
// extract the output file name from a file name
22+
const outputFileName = (filePath: string) => filePath.replace(/\.[^/.]+$/, '').replace(/\\/g, '/')
23+
24+
export type MultiInputOptions = {
25+
glob?: FastGlob.Options
26+
relative?: string
27+
transformOutputPath?: (path: string, fileName: string) => string
28+
}
29+
30+
/**
31+
* multiInput is a rollup plugin to use multiple entry point and preserve the directory
32+
* structure in the dist folder
33+
* */
34+
export const multiInput = (options: MultiInputOptions = defaultOptions): Plugin => {
35+
const { glob: globOptions, relative = defaultOptions.relative, transformOutputPath } = options
36+
return {
37+
name: pluginName,
38+
options(conf) {
39+
// flat to enable input to be a string or an array
40+
const inputs = [conf.input].flat()
41+
// separate globs inputs string from others to enable input to be a mixed array too
42+
const globs = inputs.filter(isString)
43+
const others = inputs.filter((value) => !isString(value))
44+
const normalizedGlobs = globs.map((glob) => glob.replace(/\\/g, '/'))
45+
// get files from the globs strings and return as a Rollup entries Object
46+
const entries = fastGlob.sync(normalizedGlobs, globOptions).map((name) => {
47+
const filePath = path.relative(relative, name)
48+
const isRelative = !filePath.startsWith(`..${path.sep}`)
49+
const relativeFilePath = isRelative ? filePath : path.relative(`.${path.sep}`, name)
50+
if (transformOutputPath) {
51+
return [outputFileName(transformOutputPath(relativeFilePath, name)), name]
52+
}
53+
return [outputFileName(relativeFilePath), name]
54+
})
55+
const input = Object.assign(
56+
{},
57+
Object.fromEntries(entries),
58+
// add no globs input to the result
59+
...others,
60+
)
61+
// return the new configuration with the glob input and the non string inputs
62+
return {
63+
...conf,
64+
input,
65+
}
66+
},
67+
}
68+
}

rollup.config.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import MagicString from 'magic-string'
44
import type { LogLevel, LogOrStringHandler, Plugin, RollupLog } from 'rollup'
55
import { RollupOptions } from 'rollup'
66
import pkg from './package.json' with { type: 'json' }
7+
import { multiInput } from './rollup-multi-plugin'
78

89
const normaliseEsmOutput = (): Plugin => {
910
const CJSFilenameRegex = /__filename/g
@@ -32,7 +33,7 @@ const normaliseEsmOutput = (): Plugin => {
3233
}
3334

3435
const config: RollupOptions = {
35-
input: ['src/index.ts', 'src/testing/index.ts'],
36+
input: ['src/index.ts', 'src/testing/index.ts', 'src/types/*.ts', '!src/types/*.spec.ts'],
3637
output: [
3738
{
3839
dir: 'dist',
@@ -62,6 +63,7 @@ const config: RollupOptions = {
6263
preferBuiltins: true,
6364
}),
6465
normaliseEsmOutput(),
66+
multiInput(),
6567
],
6668
external: [...Object.keys(pkg.dependencies), ...Object.keys(pkg.peerDependencies)],
6769
onLog(level: LogLevel, log: RollupLog, handler: LogOrStringHandler) {

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
"isolatedModules": true,
1313
"resolveJsonModule": true,
1414
},
15-
"include": ["src/**/*.ts", "tests/**/*.ts", "rollup.config.ts"],
15+
"include": ["src/**/*.ts", "tests/**/*.ts", "rollup-multi-plugin.ts", "rollup.config.ts"],
1616
"exclude": ["**/*.algo.ts"],
1717
}

0 commit comments

Comments
 (0)