|
| 1 | +import { pipeline } from "node:stream/promises" |
| 2 | +import { join, relative } from "node:path" |
| 3 | +import { readFileSync } from "node:fs" |
| 4 | +import { series, src, dest } from "gulp" |
| 5 | +import Vinyl from "vinyl" |
| 6 | +import { rollup } from "rollup" |
| 7 | +import ts from "typescript" |
| 8 | + |
| 9 | +import commonjs from "rollup-plugin-commonjs" |
| 10 | +import external from "rollup-plugin-auto-external" |
| 11 | +import typescript from "rollup-plugin-typescript2" |
| 12 | +import size from "rollup-plugin-bundle-size" |
| 13 | + |
| 14 | +/** |
| 15 | + * Main build task. Bundles the library code using Rollup, |
| 16 | + * emits TypeScript declaration files using tsc. |
| 17 | + */ |
| 18 | +export const build = series(bundle, decls) |
| 19 | + |
| 20 | +export async function bundle() { |
| 21 | + const bundle = await rollup({ |
| 22 | + input: "src/components/semiotic.ts", |
| 23 | + context: "window", |
| 24 | + plugins: [ |
| 25 | + external(), |
| 26 | + typescript(), |
| 27 | + commonjs({ include: "node_modules/**" }), |
| 28 | + size() |
| 29 | + ] |
| 30 | + }) |
| 31 | + await bundle.write({ format: "cjs", file: "dist/semiotic.js" }) |
| 32 | + await bundle.write({ format: "esm", file: "dist/semiotic.module.js" }) |
| 33 | +} |
| 34 | + |
| 35 | +/** Emit publish-ready TypeScript declaration files to the dist folder. */ |
| 36 | +export function decls() { |
| 37 | + return pipeline( |
| 38 | + src(["src/components/**/*.ts", "src/components/**/*.tsx"]), |
| 39 | + declarations, |
| 40 | + // flatten folder structure before writing to file system |
| 41 | + async function* stripPath(source) { |
| 42 | + const root = join(process.cwd(), "src", "components") |
| 43 | + for await (const file of source) { |
| 44 | + const clone = file.clone({ contents: false }) |
| 45 | + yield Object.assign(clone, { path: relative(root, file.path) }) |
| 46 | + } |
| 47 | + }, |
| 48 | + dest("dist") |
| 49 | + ) |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Stream transformer that uses TypeScript compiler to extract DTS contents. |
| 54 | + * Produces Vinyl files that can be consumed afterwards. |
| 55 | + */ |
| 56 | +async function* declarations(source) { |
| 57 | + const config = { |
| 58 | + allowJs: true, |
| 59 | + declaration: true, |
| 60 | + emitDeclarationOnly: true |
| 61 | + } |
| 62 | + const host = ts.createCompilerHost(config) |
| 63 | + const roots = new Map() |
| 64 | + const output = new Set() |
| 65 | + host.writeFile = (fileName, contents) => { |
| 66 | + const path = relative(process.cwd(), fileName) |
| 67 | + output.add(new Vinyl({ path, contents: Buffer.from(contents) })) |
| 68 | + } |
| 69 | + host.readFile = (fileName) => { |
| 70 | + return ( |
| 71 | + roots.get(fileName)?.contents.toString() ?? readFileSync(fileName, "utf8") |
| 72 | + ) |
| 73 | + } |
| 74 | + for await (const file of source) { |
| 75 | + roots.set(relative(process.cwd(), file.path), file) |
| 76 | + } |
| 77 | + ts.createProgram(Array.from(roots.keys()), config, host).emit() |
| 78 | + for (const file of output) yield file |
| 79 | +} |
0 commit comments