-
Notifications
You must be signed in to change notification settings - Fork 133
/
Gulpfile.mjs
79 lines (74 loc) · 2.41 KB
/
Gulpfile.mjs
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
67
68
69
70
71
72
73
74
75
76
77
78
79
import { pipeline } from "node:stream/promises"
import { join, relative } from "node:path"
import { readFileSync } from "node:fs"
import { series, src, dest } from "gulp"
import Vinyl from "vinyl"
import { rollup } from "rollup"
import ts from "typescript"
import commonjs from "rollup-plugin-commonjs"
import external from "rollup-plugin-auto-external"
import typescript from "rollup-plugin-typescript2"
import size from "rollup-plugin-bundle-size"
/**
* Main build task. Bundles the library code using Rollup,
* emits TypeScript declaration files using tsc.
*/
export const build = series(bundle, decls)
export async function bundle() {
const bundle = await rollup({
input: "src/components/semiotic.ts",
context: "window",
plugins: [
external(),
typescript(),
commonjs({ include: "node_modules/**" }),
size()
]
})
await bundle.write({ format: "cjs", file: "dist/semiotic.js" })
await bundle.write({ format: "esm", file: "dist/semiotic.module.js" })
}
/** Emit publish-ready TypeScript declaration files to the dist folder. */
export function decls() {
return pipeline(
src(["src/components/**/*.ts", "src/components/**/*.tsx"]),
declarations,
// flatten folder structure before writing to file system
async function* stripPath(source) {
const root = join(process.cwd(), "src", "components")
for await (const file of source) {
const clone = file.clone({ contents: false })
yield Object.assign(clone, { path: relative(root, file.path) })
}
},
dest("dist")
)
}
/**
* Stream transformer that uses TypeScript compiler to extract DTS contents.
* Produces Vinyl files that can be consumed afterwards.
*/
async function* declarations(source) {
const config = {
allowJs: true,
declaration: true,
emitDeclarationOnly: true
}
const host = ts.createCompilerHost(config)
const roots = new Map()
const output = new Set()
host.writeFile = (fileName, contents) => {
const path = relative(process.cwd(), fileName)
output.add(new Vinyl({ path, contents: Buffer.from(contents) }))
}
host.readFile = (fileName) => {
return (
roots.get(fileName)?.contents.toString() ?? readFileSync(fileName, "utf8")
)
}
for await (const file of source) {
roots.set(relative(process.cwd(), file.path), file)
}
ts.createProgram(Array.from(roots.keys()), config, host).emit()
for (const file of output) yield file
}