-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
100 lines (93 loc) · 3.04 KB
/
server.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
const express = require('express');
const rollup = require('express-middleware-rollup');
const sass = require('node-sass-middleware');
const buble = require('rollup-plugin-buble');
const fs = require('fs-extra');
const resolve = require('rollup-plugin-node-resolve');
const commonjs = require('rollup-plugin-commonjs');
const replace = require('rollup-plugin-replace');
const json = require('rollup-plugin-json');
const string = require('rollup-plugin-string');
const git = require('git-rev');
const path = require('path');
const PkgRoot = __dirname;
const StaticDir = 'static'; // must not be absolute since concatenated with `root` option
const StaticPath = path.join(PkgRoot, StaticDir);
fs.mkdirp(StaticPath).then(
() => fs.copy(path.join(PkgRoot, 'node_modules', 'font-awesome', 'fonts'), path.join(StaticPath, 'fonts'))
).then(
() => fs.copy(path.join(PkgRoot, 'node_modules', 'ffmpeg.js', 'ffmpeg-worker-mp4.js'), path.join(StaticPath, 'ffmpeg-worker-mp4.js'))
).then(
() => new Promise((res, rej) => git.short(res))
).then((gitrev) => {
// Javascript compilation
const js = rollup({
src: 'js',
dest: StaticDir,
root: PkgRoot,
prefix: '/',
serve: 'on-compile',
rollupOpts: {
plugins: [
// turn file contents into js strings which can be imported
string({ include: '**/*.md' }),
// allow importing json as ES6 modules
json(),
// replace patterns in files with new values
replace({
include: 'js/config.js',
delimiters: [ '<@', '@>' ],
values: {
TIMESTAMP: new Date().toISOString(),
GIT_REV: gitrev
}
}),
// ES6 -> ES5
buble({
exclude: [ 'node_modules/ffmpeg.js/**' ]
}),
// use Node resolution algorithm to find files in node_modules
resolve({
main: true,
browser: true,
extensions: [ '.js', '.json' ],
preferBuiltins: false,
}),
// CommonJS modules -> ES6 modules
commonjs({
extensions: [ '.js', '.json' ],
exclude: [ 'node_modules/ffmpeg.js/**' ],
namedExports: {
'node_modules/image-capture/lib/imagecapture.js': [ 'ImageCapture' ]
}
}),
]
}
});
// Css compilation
const css = sass({
src: 'sass',
dest: StaticDir,
root: PkgRoot,
prefix: '/',
outputStyle: 'extended',
includePaths: [
path.join(PkgRoot, 'node_modules')
]
});
// Static file server
const statics = express.static(StaticPath);
const server = express();
server.get('/', (req, res) => {
fs.copy(path.join(PkgRoot, 'index.html'), path.join(StaticPath, 'index.html')).then(
() => res.sendFile(path.join(PkgRoot, StaticDir, 'index.html'))
);
});
server.use(js);
server.use(css);
server.use(statics);
server.listen(3000);
}).catch(err => {
console.error(err)
process.exit(1);
});