-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathrollup.config.dts.ts
More file actions
66 lines (59 loc) · 2.15 KB
/
Copy pathrollup.config.dts.ts
File metadata and controls
66 lines (59 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import fs from 'fs';
import {dts} from 'rollup-plugin-dts';
import type {Plugin, RollupOptions} from 'rollup';
// Type declarations from these dependencies are inlined into the bundled `.d.ts`
// output rather than left as bare `import`s, so consumers don't need the packages
// installed to typecheck against mapbox-gl. Everything else stays external.
const inlinedLibraries = [
'@mapbox/mapbox-gl-supported',
'@mapbox/point-geometry',
'@mapbox/tiny-sdf',
'@mapbox/vector-tile',
'geojson',
'gl-matrix',
'kdbush',
'pbf',
'potpack',
];
function packageName(id: string): string {
const parts = id.split('/');
return id.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0];
}
function external(id: string): boolean {
// Relative/absolute imports are part of our own graph and must be bundled.
if (id.startsWith('.') || id.startsWith('/')) return false;
return !inlinedLibraries.includes(packageName(id));
}
// gl-matrix ships its types wrapped in an ambient `declare module "gl-matrix" {...}`
// block, which rollup-plugin-dts can't treat as a real module. Strip the wrapper so
// its exports become visible for inlining.
function unwrapGlMatrix(): Plugin {
return {
name: 'unwrap-gl-matrix',
load(id) {
if (!id.endsWith('gl-matrix/index.d.ts')) return null;
const code = fs.readFileSync(id, 'utf8');
return code.replace(/declare module "gl-matrix" \{/, '').replace(/\}\s*$/, '');
}
};
}
function dtsConfig(input: string, file: string, umd: boolean): RollupOptions {
return {
input,
output: {
file,
format: 'es',
// The UMD entry declares a global `mapboxgl` namespace for script-tag users.
footer: umd ? 'export as namespace mapboxgl;' : undefined,
},
external,
plugins: [
unwrapGlMatrix(),
dts({respectExternal: true, tsconfig: './tsconfig.browser.json'}),
],
};
}
export default (): RollupOptions[] => [
dtsConfig('src/index.ts', 'dist/mapbox-gl.d.ts', true),
dtsConfig('src/index.esm.ts', 'dist/esm/mapbox-gl.d.ts', false),
];