-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollup.config.js
More file actions
118 lines (109 loc) · 2.55 KB
/
rollup.config.js
File metadata and controls
118 lines (109 loc) · 2.55 KB
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
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { createServer } from 'http-server';
import opener from 'opener';
import svelte from 'rollup-plugin-svelte';
import css from 'rollup-plugin-css-only';
import nodeResolve from '@rollup/plugin-node-resolve';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const {
PROD = false,
SERVE = false,
OPEN = false
} = process.env;
const destination = path.join(__dirname, 'dist');
function clean() {
let first = true;
return {
name: 'clean',
buildStart() {
if (!first) return;
first = false;
if (fs.existsSync(destination)) {
fs.rmSync(destination, { recursive: true });
}
}
};
}
function copyDir(srcDir, destDir) {
fs.mkdirSync(destDir, { recursive: true });
for (const file of fs.readdirSync(srcDir)) {
const srcFile = path.resolve(srcDir, file);
if (srcFile === destDir) continue;
const destFile = path.resolve(destDir, file);
const stat = fs.statSync(srcFile);
if (stat.isDirectory()) {
copyDir(srcFile, destFile)
} else {
fs.copyFileSync(srcFile, destFile)
}
}
}
function copyPublicDir() {
let first = true;
return {
name: 'copy-public-dir',
writeBundle() {
if (!first) return;
first = false;
console.log('Copy public dir...');
copyDir(path.resolve(__dirname, 'public'), destination);
}
};
}
function serve() {
const port = 8083;
const options = {
root: destination,
// gzip: true,
// https: {
// cert: 'cert.pem',
// key: 'key.pem'
// },
};
return {
name: 'serve',
writeBundle() {
if (global.server) return;
console.log('Start server...');
const server = createServer(options);
server.listen(port, '0.0.0.0', function () {
const protocol = options.https ? 'https://' : 'http://';
const url = protocol + '127.0.0.1:' + port;
console.log('Server running at ' + url);
if (OPEN) opener(url);
});
global.server = server;
}
};
}
export default {
input: 'src/index.js',
output: {
format: 'iife',
file: path.join(destination, 'bundle.js'),
sourcemap: !PROD
},
plugins: [
clean(),
svelte({
include: 'src/components/**/*.svelte',
compilerOptions: {
dev: !PROD
}
}),
css({
output: 'bundle.css'
}),
nodeResolve({
browser: true
}),
copyPublicDir(),
SERVE && serve(),
],
watch: {
clearScreen: false
}
};