forked from google/amp-pwa-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
110 lines (86 loc) · 2.99 KB
/
app.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
/**
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const fs = require('fs');
const path = require('path');
const express = require('express');
const nunjucks = require('nunjucks');
const rollup = require('rollup');
const sass = require('node-sass');
const closure = require('google-closure-compiler-js');
// -----------------------------------------------------------------------------
// Compile static assets, which are inlined in our HTML
function compileCSS(path) {
return Promise.resolve(sass.renderSync({
file: path,
outputStyle: 'compressed'
}).css);
}
function compileJS(path) {
// Resolve imports using RollupJS, and convert to ES5 using Closure Compiler.
return rollup.rollup({entry: path})
.then(bundle => bundle.generate({format: 'iife'}).code)
.then(code => closure.compile({jsCode: [{src: code}]}).compiledCode);
}
const ASSETS = {};
['styles/app_shell.scss', 'styles/core.scss',
'service_worker/service_worker.js', 'scripts/app_shell.js'].forEach(file => {
let compile = (file.split('.')[1] == 'js') ? compileJS : compileCSS;
compile(file).then(code => { ASSETS[file] = code; });
});
// -----------------------------------------------------------------------------
// Express App.
const pages = fs.readdirSync('pages');
const app = express();
nunjucks.configure('.', {autoescape: true, express: app});
app.use(function(req, res, next) {
res.locals.assets = ASSETS;
res.locals.request = req;
next();
});
app.use(express.static('static'));
for (let p of pages) {
let url = '/' + p.replace('.html', '').replace('index', '');
app.get(url, function(req, res) { res.render('pages/' + p); });
}
app.get('/_/offline', function(req, res) {
res.render('templates/offline.html');
});
app.get('/_/app_shell', function(req, res) {
res.render('templates/app_shell.html');
});
app.get('/_/install_serviceworker', function(req, res) {
res.render('templates/install_serviceworker.html');
});
app.get('/service_worker.js', function(req, res) {
res.setHeader('content-type', 'text/javascript');
res.write(ASSETS['service_worker/service_worker.js']);
res.end();
});
app.get('/_/latest_article', function(req, res) {
// TODO(plegner)
res.end();
});
app.get('/_/add_subscription', function(req, res) {
// TODO(plegner)
res.end();
});
app.get('/_/remove_subscription', function(req, res) {
// TODO(plegner)
res.end();
});
app.listen(8080, function() {
console.log('The API server is running on port 8080.');
});