forked from doggodoge/bb-timer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
119 lines (112 loc) · 3.01 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
116
117
118
119
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import svelte from 'rollup-plugin-svelte';
import { terser } from 'rollup-plugin-terser';
import postcss from 'rollup-plugin-postcss';
import svg from 'rollup-plugin-svg';
import json from 'rollup-plugin-json';
import nodePolyfills from 'rollup-plugin-polyfill-node';
import copy from 'rollup-plugin-copy2';
import tar from 'tar';
import fs from 'fs';
import pkg from './package.json';
import crypto from 'crypto';
import autoPreprocess from 'svelte-preprocess';
import typescript from '@rollup/plugin-typescript';
const ignoredWarnings = [
'unused-export-let',
'css-unused-selector',
'module-script-reactive-declaration',
'a11y-no-onchange',
];
// Custom plugin to clean the dist folder before building
const clean = () => ({
buildStart() {
const dist = './dist/';
if (fs.existsSync(dist)) {
fs.readdirSync(dist).forEach(path => {
if (path.endsWith('.tar.gz')) {
fs.unlinkSync(dist + path);
}
});
}
},
});
// Custom plugin to hash the JS bundle and write it in the schema
const hash = () => ({
writeBundle() {
// Generate JS hash
const fileBuffer = fs.readFileSync('dist/plugin.min.js');
const hashSum = crypto.createHash('sha1');
hashSum.update(fileBuffer);
const hex = hashSum.digest('hex');
// Read and parse existing schema from dist folder
const schema = JSON.parse(fs.readFileSync('./dist/schema.json', 'utf8'));
// Write updated schema to dist folder, pretty printed as JSON again
const newSchema = {
...schema,
hash: hex,
version: pkg.version,
};
fs.writeFileSync('./dist/schema.json', JSON.stringify(newSchema, null, 2));
},
});
// Custom plugin to bundle up our files after building
const bundle = () => ({
async writeBundle() {
const bundleName = `${pkg.name}-${pkg.version}.tar.gz`;
return tar
.c({ gzip: true, cwd: 'dist' }, [
'plugin.min.js',
'schema.json',
'package.json',
])
.pipe(fs.createWriteStream(`dist/${bundleName}`));
},
});
export default {
input: 'index.js',
output: {
sourcemap: process.env.ROLLUP_WATCH ? 'inline' : false,
format: 'iife',
file: 'dist/plugin.min.js',
name: 'plugin',
globals: {
svelte: 'svelte',
'svelte/internal': 'svelte_internal',
},
},
external: ['svelte', 'svelte/internal'],
plugins: [
clean(),
svelte({
preprocess: autoPreprocess(),
emitCss: true,
onwarn: (warning, handler) => {
// Ignore some warnings
if (!ignoredWarnings.includes(warning.code)) {
handler(warning);
}
},
}),
typescript(),
postcss(),
commonjs(),
nodePolyfills(),
resolve({
preferBuiltins: true,
browser: true,
skip: ['svelte', 'svelte/internal'],
}),
svg({
base64: true,
}),
json(),
terser(),
copy({
assets: ['schema.json', 'package.json'],
}),
hash(),
bundle(),
],
};