-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrollup-plugin-virtual-project.js
82 lines (68 loc) · 2.44 KB
/
rollup-plugin-virtual-project.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
import fs from 'fs';
import path from 'path';
import glslify from 'glslify';
export default function virtualProject() {
let pathRegex = /([^\.]+)\/virtual-project/;
return {
name: 'rollup-plugin-virtual-project',
resolveId ( source ) {
if (source.match(pathRegex)) {
// this signals that Rollup should not ask other plugins or check
// the file system to find this id
return source;
}
return null; // other ids should be handled as usually
},
load ( id ) {
let matches = id.match(pathRegex);
if (matches) {
let absoluteDir = matches[1];
const relativeDir = `.${absoluteDir}`;
let project = {
name: absoluteDir
};
const files = fs.readdirSync(relativeDir);
const glsl = files.filter(f => /.(glsl|frag)$/.test(f)).reduce((acc, _file) => {
const file = path.join(relativeDir, _file);
let filename = path.parse(file).name;
if (['shader', 'frag'].indexOf(filename) !== -1) {
filename = 'main';
}
acc[filename] = {
file: file,
glsl: glslify(file, {basedir: './'}),
};
this.addWatchFile(path.join(absoluteDir, _file));
//this.emit('file', path.join(__dirname, file)); // watch file
return acc;
}, {});
project.shaders = glsl;
const configFile = `${relativeDir}/config.json`;
let config = null;
if (fs.existsSync(configFile)) {
config = fs.readFileSync(configFile, 'utf8');
config = JSON.parse(config);
this.addWatchFile(path.join(absoluteDir, 'config.json'));
//this.emit('file', path.join(__dirname, configFile));
}
project.config = config;
const drawFile = `${relativeDir}/draw.js`;
let hasDrawFile = fs.existsSync(drawFile);
if (hasDrawFile) {
this.addWatchFile(path.join(absoluteDir, 'draw.js'));
}
let moduleSource = [];
if (hasDrawFile) {
moduleSource.push(`import draw from '${absoluteDir}/draw.js'`);
}
moduleSource.push(`let project = ${JSON.stringify(project)};`)
if (hasDrawFile) {
moduleSource.push(`project.createDraw = draw;`);
}
moduleSource.push(`export default project;`);
return moduleSource.join('\n');
}
return null; // other ids should be handled as usually
}
};
}