-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
rollup.config.mjs
159 lines (142 loc) · 4.29 KB
/
rollup.config.mjs
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
152
153
154
155
156
157
158
159
import resolve from "@rollup/plugin-node-resolve";
import glslify from "rollup-plugin-glslify";
import { binary2base64 } from "rollup-plugin-binary2base64";
import commonjs from "@rollup/plugin-commonjs";
import miniProgramPlugin from "./rollup.miniprogram.plugin.mjs";
import { swc, defineRollupSwcOption, minify } from "rollup-plugin-swc3";
import camelCase from "camelcase";
import fs from "fs";
import path from "path";
import replace from "@rollup/plugin-replace";
import { string } from "rollup-plugin-string";
function walk(dir) {
let files = fs.readdirSync(dir);
files = files.map((file) => {
const filePath = path.join(dir, file);
const stats = fs.statSync(filePath);
if (stats.isDirectory()) return walk(filePath);
else if (stats.isFile()) return filePath;
});
return files.reduce((all, folderContents) => all.concat(folderContents), []);
}
const pkgsRoot = path.join(process.cwd(), "packages");
const pkgs = fs
.readdirSync(pkgsRoot)
.map((dir) => path.join(pkgsRoot, dir))
.filter((dir) => fs.statSync(dir).isDirectory())
.map((location) => {
return {
location: location,
pkgJson: JSON.parse(fs.readFileSync(path.resolve(location, "package.json"), { encoding: "utf-8" }))
};
});
// "@galacean/engine-toolkit" 、 "@galacean/engine-toolkit-controls" ...
function toGlobalName(pkgName) {
return camelCase(pkgName);
}
const extensions = [".js", ".jsx", ".ts", ".tsx"];
const mainFields = ["module", "main"];
const plugins = [
resolve({ extensions, preferBuiltins: true, mainFields }),
glslify({
include: [/\.glsl$/],
exclude: "**/packages/shaderlab/src/shaders/**"
}),
string({ include: ["**/*.shader", "**/packages/shaderlab/src/**/*.(glsl|gs)"] }),
swc(
defineRollupSwcOption({
include: /\.[mc]?[jt]sx?$/,
exclude: /node_modules/,
jsc: {
loose: true,
externalHelpers: true,
target: "es5"
},
sourceMaps: true
})
),
binary2base64({
include: ["**/*.wasm"]
}),
commonjs()
];
function makeRollupConfig(pkg) {
const externals = Object.keys(
Object.assign({}, pkg.pkgJson.dependencies, pkg.pkgJson.peerDependencies, pkg.pkgJson.devDependencies)
);
let globals = {
"@galacean/engine": "Galacean",
"@galacean/engine-xr": "Galacean.XR"
};
externals.forEach((external) => {
globals[external] = toGlobalName(external);
});
const entries = Object.fromEntries(
walk(path.join(pkg.location, "src"))
.filter((file) => /^(?!.*\.d\.ts$).*\.ts$/.test(file))
.map((item) => {
return [path.relative(path.join(pkg.location, "src"), item.replace(/\.[^/.]+$/, "")), item];
})
);
plugins.push(
replace({
preventAssignment: true,
__buildVersion: pkg.pkgJson.version
})
);
const umdConfig = pkg.pkgJson.umd;
const configs = [
{
input: entries,
output: {
dir: path.join(pkg.location, "dist", "es"),
format: "es",
sourcemap: true,
globals: globals
},
external: externals,
plugins
}
];
configs.push({
input: path.join(pkg.location, "src", "index.ts"),
output: {
file: path.join(pkg.location, pkg.pkgJson.main),
sourcemap: true,
format: "commonjs"
},
external: externals,
plugins
});
if (umdConfig) {
configs.push({
input: path.join(pkg.location, "src", "index.ts"),
output: {
file: path.join(pkg.location, "dist", "umd", "browser.js"),
format: "umd",
name: umdConfig.name,
globals: globals
},
external: Object.keys(umdConfig.globals ?? {}),
plugins: [...plugins, minify({ sourceMap: true })]
});
}
configs.push({
input: path.join(pkg.location, "src", "index.ts"),
output: {
file: path.join(pkg.location, "dist", "miniprogram.js"),
sourcemap: false,
format: "cjs"
},
external: Object.keys(Object.assign(pkg.pkgJson.dependencies ?? {}, pkg.pkgJson.peerDependencies ?? {}))
.concat("@galacean/engine-miniprogram-adapter")
.map((name) => `${name}/dist/miniprogram`),
plugins: [...plugins, ...miniProgramPlugin]
});
return configs;
}
const builderConfigs = pkgs.map(makeRollupConfig).flat();
builderConfigs.sort((_, b) => {
if (b.output.format === "umd") return -1
})
export default Promise.all(builderConfigs);