-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.js
127 lines (116 loc) · 3.02 KB
/
rollup.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
120
121
122
123
124
125
126
127
import fs from "node:fs"
import { each } from "./src/lib/util/index.js"
import { createIndex, resolvePath } from "./src/lib/util/node.js"
import alias from "@rollup/plugin-alias"
import commonjs from "@rollup/plugin-commonjs"
import { nodeResolve } from "@rollup/plugin-node-resolve"
import terser from "@rollup/plugin-terser"
export default function (banner, pkg) {
function _({ src, dest, name: _name }) {
const name = `${pkg.name}${_name ? `-${_name}` : ""}.js`
const config = {
name,
input: src.startsWith(".") ? src : `./src/${src}`,
output: {
file: dest.startsWith(".") ? dest : `./dist/${dest}`,
strict: true,
// sourcemap: true,
format: "es",
banner: banner(name),
},
plugins: [
alias({
entries: [
{
find: /^\@\/src\//,
replacement: `${resolvePath("./src", import.meta)}/`,
},
{
find: /^\@\/lib\//,
replacement: `${resolvePath("./src/lib", import.meta)}/`,
},
{
find: /^\@\/data\//,
replacement: `${resolvePath("./src/data", import.meta)}/`,
},
{
find: /^\@\/util\//,
replacement: `${resolvePath("./src/lib/util", import.meta)}/`,
},
{
find: /^serialize-javascript$/,
replacement: resolvePath(
"./src/lib/serialize-javascript/index.js",
import.meta
),
},
],
}),
commonjs(),
nodeResolve({
preferBuiltins: false,
}),
],
}
if (!process?.env?.DISABLE_MINIFIER) config.plugins.push(terser())
return config
}
createIndex(resolvePath("./src/data/block", import.meta), {
suffixToRead: ".json.js",
})
const output = {}
// bundle.js
output.bundled = _({
src: "script/index.js",
dest: "bundle.min.js",
name: "bundle",
})
// single script
output.scripts = []
each(
fs.readdirSync("./src/script", {
withFileTypes: true,
}),
(dirent) => {
if (dirent.isDirectory()) {
const name = dirent.name
output.scripts.push(
_({
src: `script/${name}/index.js`,
dest: `${name}.min.js`,
name,
})
)
}
}
)
// libraries
output.libraries = []
each(
fs.readdirSync("./src/lib", {
withFileTypes: true,
}),
(dirent) => {
if (dirent.isDirectory()) {
const name = dirent.name
if (name === "database" || name === "option-manager") {
output.libraries.push(
_({
src: `lib/${name}/index.js`,
dest: `${name}.min.js`,
name,
})
)
}
}
}
)
// test
// if (process?.env?.NODE_ENV === "development")
// output.test = _({
// src: "script/test/index.js",
// dest: "test.min.js",
// name: "test",
// })
return output
}