-
Notifications
You must be signed in to change notification settings - Fork 11
/
rollup.config.js
115 lines (109 loc) · 3.13 KB
/
rollup.config.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import typescript from "@rollup/plugin-typescript";
import commonjs from "@rollup/plugin-commonjs";
import nodeResolve from "@rollup/plugin-node-resolve";
import terser from "@rollup/plugin-terser";
import { dts } from "rollup-plugin-dts";
import html, { makeHtmlAttributes } from "@rollup/plugin-html";
import { readFileSync } from "fs";
import css from "rollup-plugin-import-css";
import pkg from "./package.json" assert { type: "json" };
const { name, description, version, repository, license, keywords } = pkg;
export default [
{
input: "./src/index.ts",
external: (id) =>
!id.startsWith("\0") && !id.startsWith(".") && !id.startsWith("/"),
plugins: [
typescript({
outputToFilesystem: true,
tsconfig: "./tsconfig.json",
}),
nodeResolve(),
commonjs({
ignoreGlobal: true,
}),
terser({
compress: {
passes: 2,
},
}),
],
output: [
{
exports: "named",
format: "cjs",
sourcemap: true,
file: "dist/index.cjs",
},
{
format: "esm",
sourcemap: true,
file: "dist/index.mjs",
},
],
},
{
input: "./dist/types/index.d.ts",
output: [{ file: "dist/index.d.ts", format: "es" }],
plugins: [dts()],
},
{
input: "./demo/demo.js",
output: {
dir: "./public",
format: "esm",
entryFileNames: "assets/bundle-[hash].js",
},
plugins: [
nodeResolve(),
commonjs({
ignoreGlobal: true,
}),
css(),
html({
title: `${name} demo`,
template: ({ attributes, files, meta, publicPath, title }) => {
const repo = repository.replace("git+", "").replace(".git", "");
const scripts = (files.js || [])
.map(({ fileName }) => {
const attrs = makeHtmlAttributes(attributes.script);
return `<script src="${publicPath}${fileName}"${attrs}></script>`;
})
.join("\n");
const links = (files.css || [])
.map(({ fileName }) => {
const attrs = makeHtmlAttributes(attributes.link);
return `<link href="${publicPath}${fileName}" rel="stylesheet"${attrs}>`;
})
.join("\n");
const metas = meta
.map((input) => {
const attrs = makeHtmlAttributes(input);
return `<meta${attrs}>`;
})
.join("\n");
const replacements = {
scripts,
links,
title,
name,
description,
metas,
version: `v${version}`,
repo,
releases: `${repo}/releases`,
licenseUrl: `${repo}/blob/main/LICENSE`,
npm: `https://npmjs.org/package/${name}`,
license,
keywords: keywords.join(", "),
};
let template = readFileSync("./demo/index.tpl.html", "utf-8");
Object.entries(replacements).forEach(([key, value]) => {
template = template.replaceAll(`{{ ${key} }}`, value);
});
return template;
},
}),
],
},
];