-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrollup.config.js
149 lines (127 loc) · 5.43 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
import { readFileSync } from 'node:fs';
import * as path from "node:path";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import { terser } from "rollup-plugin-terser";
import copy from "rollup-plugin-copy";
import fg from 'fast-glob';
import { SourceMapConsumer, SourceNode } from 'source-map'
function build(name, input, dst, file, copyFiles, watchInclude) {
const format = "iife";
const info = {
input,
output: {
format,
file: path.join(dst, file),
sourcemap: true,
name
},
plugins: [
{
name: "stringer",
resolveId(id, importer) {
if (id === "webgpu_inspector_core_func") {
return id;
} else if (id === "webgpu_recorder_core_func") {
return id;
}
},
async load(id) {
if (id === "webgpu_inspector_core_func") {
const corePath = path.join(dst, 'webgpu_inspector.js');
this.addWatchFile(corePath);
this.addWatchFile(corePath + ".map");
let code = readFileSync(corePath, 'utf-8');
let codeMap = JSON.parse(readFileSync(corePath + ".map", 'utf-8'));
const consumer = await new SourceMapConsumer(codeMap);
const originalSrc = SourceNode.fromStringWithSourceMap(code, consumer);
const srcPath = "src/webgpu_inspector_core_func";
const newSrc = new SourceNode(1, 1, srcPath, [
new SourceNode(1, 1, srcPath, "export default function() { "),
originalSrc,
new SourceNode(1, 36, srcPath, " };")
]);
newSrc.setSourceContent(srcPath, "export default function() { ${code} };");
const generated = newSrc.toStringWithSourceMap({ file: srcPath });
return {
code: generated.code,
map: JSON.parse(generated.map.toString()),
};
} else if (id === "webgpu_recorder_core_func") {
const corePath = path.join(dst, 'webgpu_recorder.js');
this.addWatchFile(corePath);
this.addWatchFile(corePath + ".map");
const code = readFileSync(corePath, 'utf-8');
let codeMap = JSON.parse(readFileSync(corePath + ".map", 'utf-8'));
const consumer = await new SourceMapConsumer(codeMap);
const originalSrc = SourceNode.fromStringWithSourceMap(code, consumer);
const srcPath = "src/webgpu_recorder_core_func";
const newSrc = new SourceNode(1, 1, srcPath, [
new SourceNode(1, 1, srcPath, "export default function() { "),
originalSrc,
new SourceNode(1, 36, srcPath, " };")
]);
newSrc.setSourceContent(srcPath, "export default function() { ${code} };");
const generated = newSrc.toStringWithSourceMap({ file: srcPath });
return {
code: generated.code,
map: JSON.parse(generated.map.toString()),
};
}
}
},
nodeResolve(),
terser({
ecma: 2020,
compress: {
module: true,
toplevel: true,
keep_classnames: true,
unsafe_arrows: true,
drop_console: false,
drop_debugger: false
},
output: { quote_style: 1 }
})
]
};
if (copyFiles) {
info.plugins.push(copy(copyFiles));
}
if (watchInclude) {
info.plugins.push({
name: 'watch-external',
async buildStart() {
const files = await fg(watchInclude);
for (const file of files) {
this.addWatchFile(file);
}
}
});
}
return info;
}
const builds = [];
const versions = ["chrome", "firefox"];
for (const version of versions) {
const copyFiles = {
targets: [
{ src: `src/extension/${version}/manifest.json`, dest: `extensions/${version}` },
{ src: `src/extension/webgpu_inspector_devtools.html`, dest: `extensions/${version}` },
{ src: `src/extension/webgpu_inspector_devtools.js`, dest: `extensions/${version}` },
{ src: `src/extension/webgpu_inspector_panel.css`, dest: `extensions/${version}` },
{ src: `src/extension/webgpu_inspector_panel.html`, dest: `extensions/${version}` },
{ src: `src/extension/res`, dest: `extensions/${version}` },
]
};
builds.push(
build("__webgpu_recorder", 'webgpu_recorder/webgpu_recorder.js', `extensions/${version}`, '/webgpu_recorder.js'),
build("__webgpu_inspector", 'src/webgpu_inspector.js', `extensions/${version}`, '/webgpu_inspector.js'),
build("__webgpu_inspector_loader", 'src/webgpu_inspector_loader.js', `extensions/${version}`, '/webgpu_inspector_loader.js'),
build("__webgpu_recorder_loader", 'src/webgpu_recorder_loader.js', `extensions/${version}`, '/webgpu_recorder_loader.js'),
build("__webgpu_inspector_worker", 'src/webgpu_inspector_worker.js', `extensions/${version}`, '/webgpu_inspector_worker.js'),
build("__webgpu_inspector_window", 'src/devtools/inspector_window.js', `extensions/${version}`, '/webgpu_inspector_window.js'),
build("__background", 'src/extension/background.js', `extensions/${version}`, '/background.js'),
build("__content_script", 'src/extension/content_script.js', `extensions/${version}`, 'content_script.js', copyFiles, "src/extension/**/*"),
);
}
export default builds;