-
Notifications
You must be signed in to change notification settings - Fork 310
/
rollup.config.js
130 lines (119 loc) · 2.97 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
import fs from 'fs';
import path from 'path';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import commonjs from '@rollup/plugin-commonjs';
import { readDirSyncRecursive } from './build/lib/readdir.js';
const nodeVersion = parseInt(process.version.substring(1));
if (isNaN(nodeVersion) || nodeVersion < 20) {
console.error('need node >= v20');
process.exit(1);
}
const outPath = 'out';
function wgslPlugin() {
return {
name: 'wgsl-plugin',
transform(code, id) {
if (id.endsWith('.wgsl')) {
return {
code: `export default \`${code}\`;`,
map: { mappings: '' },
};
}
},
};
}
function makeRelativeToCWD(id) {
return path.relative(process.cwd(), path.normalize(id)).replaceAll('\\', '/');
}
function filenamePlugin() {
return {
name: 'filename-plugin',
transform(code, id) {
return {
code: code.replaceAll(
'__DIRNAME__',
() => `${JSON.stringify(makeRelativeToCWD(path.dirname(id)))}`
),
map: { mappings: '' },
};
},
};
}
/**
* Given a path like sample/foo/main.ts then, if an index.html doesn't exist
* in the same folder, generate a redirect index.html in the out folder.
* Note:
* `samples/name/index.html` is a redirect (generated)
* `sample/name/index.html` is the live sample (the iframe's src)
*/
function writeRedirect(filename) {
const sampleName = path.basename(path.dirname(filename));
const dirname = path.join(outPath, 'samples', sampleName);
const filepath = path.join(dirname, 'index.html');
fs.mkdirSync(dirname, { recursive: true });
console.log('created', filepath);
fs.writeFileSync(
filepath,
`\
<!DOCTYPE html>
<html>
<head>
<meta
http-equiv="refresh"
content="0;URL='../../?sample=${path.basename(path.dirname(filename))}'"
></meta>
</head>
</html>
`
);
}
const sampleFiles = readDirSyncRecursive('sample');
// Generate redirects for all samples
sampleFiles
.filter((n) => n.endsWith('/index.html'))
.forEach((n) => writeRedirect(n));
const samplePlugins = [
wgslPlugin(),
nodeResolve(),
commonjs(),
typescript({ tsconfig: './sample/tsconfig.json' }),
];
// add a rollup rule for each sample
const samples = sampleFiles
.filter((n) => n.endsWith('/main.ts') || n.endsWith('/worker.ts'))
.map((filename) => {
return {
input: filename,
output: [
{
file: `${outPath}/${filename.replace(/\.ts$/, '.js')}`,
format: 'esm',
sourcemap: true,
},
],
plugins: samplePlugins,
};
});
export default [
{
input: 'src/main.ts',
output: [
{
file: `${outPath}/main.js`,
format: 'esm',
sourcemap: true,
},
],
plugins: [
nodeResolve(),
commonjs(),
filenamePlugin(),
typescript({ tsconfig: './src/tsconfig.json' }),
],
watch: {
clearScreen: false,
},
},
...samples,
];