This repository was archived by the owner on Aug 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathapp.js
156 lines (116 loc) · 4.7 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
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
150
151
152
153
154
155
156
/* global Intl, IntlPolyfill */
'use strict';
var config = require('./config');
// -- Configure JavaScript Runtime ---------------------------------------------
var hasNativePromise = !!global.Promise;
require('es6-shim');
// es6-shim provides a Promise implementation, but it's not very good.
if (!hasNativePromise) {
global.Promise = require('promise')
}
var areIntlLocalesSupported = require('intl-locales-supported');
if (global.Intl) {
// Determine if the built-in `Intl` has the locale data we need.
if (!areIntlLocalesSupported(config.availableLocales)) {
// `Intl` exists, but it doesn't have the data we need, so load the
// polyfill and replace the constructors with need with the polyfill's.
require('intl');
Intl.NumberFormat = IntlPolyfill.NumberFormat;
Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat;
}
} else {
// No `Intl`, so use and load the polyfill.
global.Intl = require('intl');
}
// -- FormatJS Libs ------------------------------------------------------------
global.React = require('react/addons');
global.ReactIntl = require('react-intl');
global.Handlebars = require('handlebars');
global.HandlebarsIntl = require('handlebars-intl');
global.dust = require('dustjs-linkedin');
global.DustIntl = require('dust-intl');
global.DustIntl.registerWith(global.dust);
// -----------------------------------------------------------------------------
var path = require('path');
var express = require('express');
var expstate = require('express-state');
var pathToRegexp = require('path-to-regexp');
var hbs = require('./lib/hbs');
var middleware = require('./middleware');
var routes = require('./routes');
// -- Configure Express App ----------------------------------------------------
var app = module.exports = express();
expstate.extend(app);
app.set('name', 'FormatJS');
app.set('port', config.port);
app.set('polyfill service', config.polyfillService);
app.set('available locales', config.availableLocales);
app.set('default locale', 'en-US');
app.set('state namespace', 'APP');
app.enable('strict routing');
app.enable('case sensitive routing');
app.engine(hbs.extname, hbs.engine);
app.set('view engine', hbs.extname);
app.set('views', config.dirs.views);
if (app.get('env') === 'development') {
// This will watch files during development and automatically re-build.
app.watcher = require('./lib/watcher');
}
// -- Middleware ---------------------------------------------------------------
var router = express.Router({
caseSensitive: app.get('case sensitive routing'),
strict : app.get('strict routing')
});
if (app.get('env') === 'development') {
app.use(middleware.logger('tiny'));
}
app.use(middleware.compress());
app.use(middleware.favicon(path.join('./public/favicon.ico')));
app.use(middleware.static(path.join(config.dirs.build, 'client'), {maxage: '1m'}));
app.use(router);
app.use(middleware.slash());
// -- Routes -------------------------------------------------------------------
var route = router.route.bind(router);
router.use(middleware.fixBadSafari);
router.use(middleware.intl);
router.use(middleware.polyfills);
route('/guide/').get(routes.redirect('/guides/'));
route('/react/').get(routes.redirect('https://github.com/yahoo/react-intl'));
route('/ember/').get(routes.redirect('https://github.com/yahoo/ember-intl'));
routes.home(route('/'));
routes.about(route('/about/'));
routes.guides(route('/guides/:guide?/'));
routes.integrations(route('/integrations/'));
routes.github(route('/github/'));
routes.react(route('/react/v1/'));
routes.handlebars(route('/handlebars/'));
routes.dust(route('/dust/'));
app.getRoute = function (routeName) {
var layer = router.stack.find(function (layer) {
return layer.route && layer.route.name === routeName;
});
if (!layer) {
throw new Error('No route name: ' + routeName);
}
return layer.route;
};
app.getPathTo = function (routeName, context) {
var toPath = pathToRegexp.compile(app.getRoute(routeName).path);
return toPath(context);
};
// -- Locals -------------------------------------------------------------------
Object.assign(app.locals, {
brand : app.get('name'),
tagline: 'Internationalize your web apps on the client & server.',
min: app.get('env') === 'production' ? '.min' : '',
menuItems : ['guides', 'integrations', 'github'].map(app.getRoute),
footMenuItems: ['home', 'about', 'guides', 'integrations', 'github'].map(app.getRoute),
helpers: {
pathTo: function (name, options) {
return app.getPathTo(name, options.hash);
}
}
});
if (app.get('env') === 'production') {
app.locals.analytics = config.ga;
}