-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.js
151 lines (138 loc) · 3.13 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { nodeResolve } from "@rollup/plugin-node-resolve";
import { extname } from "path";
import prettier from "prettier";
import cpy from "rollup-plugin-cpy";
import html from "rollup-plugin-html2";
import smartAsset from "rollup-plugin-smart-asset";
import serve from "rollup-plugin-serve";
import styles from "rollup-plugin-styles";
import { minify } from "terser";
const smartAssetOpts = {
url: "copy",
extensions: [
".svg",
".gif",
".png",
".jpg",
".glb",
".woff",
".woff2",
".CUBE",
],
publicPath: "/assets/",
assetsPath: "assets/",
};
// VERY HACKY
const excludeAssetsFromHTML = {
[Symbol.iterator]: () => {
return ["assets/*"][Symbol.iterator]();
},
has: (name) => {
return !name || name.startsWith("assets/");
},
};
const removeDuplicateCSSinJS = () => ({
generateBundle(_, bundle) {
delete bundle["3dstyles.js"];
},
});
const namedAssets = () => ({
generateBundle(_, bundle) {
Object.entries(bundle).forEach(
([name, item]) =>
item.type === "asset" && !item.name && (item.name = name),
);
},
});
const prettifyOrMinifyCode = () => {
const confFile = prettier.resolveConfigFile();
const conf = confFile.then(prettier.resolveConfig);
return {
async renderChunk(code, item) {
if (!item.fileName || item.fileName === "vendor.js") {
// We just want to minify our vendor file
const { code: minCode, map } = await minify(
{
"vendor.js": code,
},
{
sourceMap: {
filename: "vendor.js",
url: "vendor.js.map",
},
toplevel: true,
},
);
return { code: minCode, map };
}
if (code) {
return {
code: prettier.format(code, {
...(await conf),
filepath: item.fileName,
}),
};
}
},
async generateBundle(_, bundle) {
const items = Object.values(bundle);
for (const item of items) {
if (!item.fileName || item.fileName === "vendor.js") {
continue;
}
const fileType = extname(item.fileName);
if ([".html", ".css", ".js"].includes(fileType)) {
const codeKey = "code" in item ? "code" : "source";
item[codeKey] = prettier.format(item[codeKey], {
...(await conf),
filepath: item.fileName,
});
}
}
},
};
};
const settings = {
input: ["js/3ds.js", "css/3dstyles.css"],
output: {
assetFileNames: "[name][extname]",
dir: "dist/",
format: "esm",
chunkFileNames: "[name].js",
manualChunks: (id) => {
if (id.includes("three") || id.includes("node_modules")) {
return "vendor";
}
},
},
plugins: [
nodeResolve(),
styles({
mode: ["extract", "3dstyles.css"],
url: { hash: false, publicPath: "/assets/" },
}),
smartAsset(smartAssetOpts),
cpy({
files: "./node_modules/three/examples/js/libs/draco/gltf/*",
dest: "dist/draco",
}),
removeDuplicateCSSinJS(),
namedAssets(),
html({
entries: {
"3ds": {
type: "module",
},
},
template: "template.html",
fileName: "index.html",
exclude: excludeAssetsFromHTML,
minify: false,
}),
prettifyOrMinifyCode(),
],
};
if (process.argv.includes("-s") || process.argv.includes("--serve")) {
settings.plugins.push(serve("dist"));
}
export default settings;