-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrollup.config.js
More file actions
123 lines (113 loc) · 3.98 KB
/
rollup.config.js
File metadata and controls
123 lines (113 loc) · 3.98 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
119
120
121
122
123
import alias from "@rollup/plugin-alias";
import color from "picocolors";
import maxmin from "maxmin";
import node_resolve from "@rollup/plugin-node-resolve";
import path from "node:path";
import replace from "@rollup/plugin-replace";
import strip_comments from "strip-comments";
import terser from "@rollup/plugin-terser";
import virtual from "@rollup/plugin-virtual";
import { fileURLToPath } from "node:url";
import { globSync as glob } from "glob";
const is_production = process.env.NODE_ENV === "production";
const global_plugins = optimize => [
node_resolve(),
optimize && remove_comments(),
optimize && trim_ws(),
bundle_size(),
alias({
entries: [
{
find: "@",
replacement: path.join(path.dirname(fileURLToPath(import.meta.url)), "src")
}
]
})
];
const configurations = glob("src/plugins/*/index.js").map(p => {
const plugin_name = path.basename(path.dirname(p));
return [
create_configuration({ plugin_name, input: `${plugin_name}.js`, format: "iife", optimize: false }),
create_configuration({ plugin_name, input: `${plugin_name}.es`, format: "es", optimize: false }),
is_production && create_configuration({ plugin_name, input: `${plugin_name}.js`, format: "iife", optimize: true }),
is_production && create_configuration({ plugin_name, input: `${plugin_name}.es`, format: "es", optimize: true })
].filter(Boolean);
}).flat();
export default configurations;
function create_configuration({ plugin_name, input, format, optimize }) {
const ext_format = format === "es" ? ".esm" : "";
const ext_min = optimize ? ".min" : "";
return {
input,
treeshake: "smallest",
output: {
file: `dist/${plugin_name}/alpinegear-${plugin_name}${ext_format}${ext_min}.js`,
format: format,
plugins: optimize && [terser({
output: {
comments: false
},
compress: {
passes: 5,
ecma: 2020,
drop_console: false,
drop_debugger: true,
pure_getters: true,
arguments: true,
unsafe_comps: true,
unsafe_math: true,
unsafe_methods: false
}
})]
},
plugins: [
...global_plugins(optimize),
replace({
preventAssignment: true,
values: {
"__DEV__": !optimize
}
}),
virtual({
[input]: format === "iife"
? `import { listen } from "src/utilities/utils.js";
import __${plugin_name} from "src/plugins/${plugin_name}/index.js";
listen(document, "alpine:init", () => Alpine.plugin(__${plugin_name}));`
: `import plugin from "src/plugins/${plugin_name}/index.js";
export default plugin;
export * from "src/plugins/${plugin_name}/index.js";`
})
]
};
}
function remove_comments() {
return {
name: "remove_comments",
transform(source) {
return {
code: strip_comments(source, {})
};
}
};
}
function trim_ws() {
return {
name: "trim_ws",
generateBundle(options, bundle) {
if (options.file.match(/\.js$/)) {
const key = path.basename(options.file);
bundle[key].code = bundle[key].code.trim();
}
}
};
}
function bundle_size() {
return {
name: "bundle_size",
generateBundle(options, bundle) {
const name = path.basename(options.file);
const size = maxmin(bundle[name].code, bundle[name].code, true);
this.info(`Produced '${color.cyan(name)}': ${size.slice(size.indexOf(' → ') + 3)}`);
}
};
}