-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrollup.config.mjs
More file actions
155 lines (141 loc) · 4.33 KB
/
Copy pathrollup.config.mjs
File metadata and controls
155 lines (141 loc) · 4.33 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
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
import resolve from "@rollup/plugin-node-resolve";
import glslify from "rollup-plugin-glslify";
import { shaderCompiler } from "@galacean/engine-shader-compiler/bundler/rollup";
import { binary2base64 } from "rollup-plugin-binary2base64";
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";
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/toolsBaker" ...
function toGlobalName(pkgName) {
return camelCase(pkgName);
}
const extensions = [".js", ".jsx", ".ts", ".tsx"];
const plugins = [
resolve({ extensions, preferBuiltins: true }),
glslify({
include: [/\.glsl$/]
}),
shaderCompiler({
filter: (id) => /\.(shader|shaderc)$/.test(id),
precompile: {
input: path.resolve(pkgsRoot, "baker/src/shader"),
output: path.resolve(pkgsRoot, "baker/compiledShaders"),
clean: true,
emitIndex: true
}
}),
swc(
defineRollupSwcOption({
include: /\.[mc]?[jt]sx?$/,
exclude: /node_modules/,
jsc: {
loose: true,
externalHelpers: true,
target: "es5",
baseUrl: process.cwd()
},
sourceMaps: true
})
),
binary2base64({
include: ["**/*.wasm"]
})
];
function makeRollupConfig(pkg) {
const externals = Object.keys(
Object.assign({}, pkg.pkgJson.dependencies, pkg.pkgJson.peerDependencies, pkg.pkgJson.devDependencies)
);
const entries = Object.fromEntries(
walk(path.join(pkg.location, "src"))
.filter((file) => /^(?!.*\.d\.ts$).*\.(ts|js)$/.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 globals = {};
externals.forEach((external) => {
globals[external] = toGlobalName(external);
});
globals["@galacean/engine"] = "Galacean";
// Use publishConfig paths for build output to avoid overwriting source files.
// During development, main/module/browser point to src/index.ts for direct consumption;
// publishConfig contains the actual dist output paths used for npm publishing.
const publish = pkg.pkgJson.publishConfig || {};
const mainOutput = publish.main || pkg.pkgJson.main;
const moduleOutput = publish.module || pkg.pkgJson.module;
const browserOutput = publish.browser || pkg.pkgJson.browser;
const config = [];
const input = path.join(pkg.location, "src", pkg.pkgJson.types ? "index.ts" : "index.js");
if (mainOutput) {
config.push({
input,
output: {
file: path.join(pkg.location, mainOutput),
format: "commonjs",
sourcemap: true,
inlineDynamicImports: true
},
external: externals,
plugins
});
}
if (moduleOutput) {
config.push({
input,
output: {
file: path.join(pkg.location, moduleOutput),
format: "es",
sourcemap: true,
inlineDynamicImports: true
},
external: externals,
plugins
});
}
if (browserOutput) {
config.push({
input,
output: {
file: path.join(pkg.location, browserOutput),
format: "umd",
name: toGlobalName(pkg.pkgJson.name),
globals: globals,
inlineDynamicImports: true
},
// 总包只 external @galacean/engine
external: pkg.pkgJson.name === "@galacean/tools" ? ["@galacean/engine"] : externals,
plugins: [...plugins, minify({ sourceMap: true })]
});
}
return config;
}
export default Promise.all(pkgs.map(makeRollupConfig).flat());