-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathrollup.config.ts
77 lines (72 loc) · 2.34 KB
/
rollup.config.ts
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
import commonjs from "@rollup/plugin-commonjs";
import resolve from "@rollup/plugin-node-resolve";
import terser from "@rollup/plugin-terser";
import typescript from "@rollup/plugin-typescript";
import { importAsString } from 'rollup-plugin-string-import';
import pkg from "./package.json" assert { type: 'json' };
function wrapWithBackground(svg){
return `background: url("data:image/svg+xml,${svg}") no-repeat center center;`;
}
function regexEncode(svg){
svg = svg.replace(/>\s{1,}</g, `><`);
svg = svg.replace(/\s{2,}/g, ` `);
return svg.replace(/[\r\n%#()<>?[\\\]^`{|}]/g, encodeURIComponent)
}
function doublesToSingles(svg){
return svg.replace(/"/g, "'")
}
function preprocessFile(content: string, fileName: string): string {
if (fileName.endsWith(".css")) {
// remove comments/newlines
return content.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, "").replace(/\r\n/g, "");
}
else if (fileName.endsWith(".svg")) {
// removes comments/newlines while also encoding the svg and adding some scaffolding to import it
return wrapWithBackground(regexEncode(doublesToSingles(content)));
}
return "";
}
export default [
{
input: "src/index.ts",
output: [
{ file: pkg.main, format: "cjs", exports: "named" },
{ file: pkg.module, format: "es", exports: "named" }
],
plugins: [
resolve(),
importAsString({
include: ["**/*.css", "**/*.svg"],
transform: preprocessFile,
}),
typescript(),
commonjs({ include: ["node_modules/**"] })
],
onwarn(message, warn) {
if (message.code === 'NON_EXISTENT_EXPORT') { return; }
if (message.code === 'CIRCULAR_DEPENDENCY') { return; }
if (message.code === 'SOURCEMAP_ERROR') { return; }
warn(message);
}
},
{
input: "src/global.ts",
output: [ { file: pkg.unpkg, format: "iife", exports: "named" } ],
plugins: [
resolve(),
importAsString({
include: ["**/*.css", "**/*.svg"],
transform: preprocessFile,
}),
typescript(),
terser({output: {comments: false}}),
commonjs({ include: ["node_modules/**"] })
],
onwarn(message, warn) {
if (message.code === 'NON_EXISTENT_EXPORT') { return; }
if (message.code === 'CIRCULAR_DEPENDENCY') { return; }
if (message.code === 'SOURCEMAP_ERROR') { return; }
warn(message);
}
}
];