Use CommonJS for server modules
This commit is contained in:
parent
c476954c99
commit
503b24e8d0
|
@ -1,4 +1,4 @@
|
|||
export default [
|
||||
module.exports = [
|
||||
'goodjsproject',
|
||||
'thisoneisevil'
|
||||
]
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import fs from 'fs'
|
||||
import invariant from 'invariant'
|
||||
import webpack from 'webpack'
|
||||
const fs = require('fs')
|
||||
const invariant = require('invariant')
|
||||
const webpack = require('webpack')
|
||||
|
||||
const createBundle = (webpackStats) => {
|
||||
const { publicPath, assetsByChunkName } = webpackStats
|
||||
|
@ -35,7 +35,7 @@ const createBundle = (webpackStats) => {
|
|||
* An express middleware that sets req.manifest from the build manifest
|
||||
* in the given file. Should be used in production to get consistent hashes.
|
||||
*/
|
||||
export const assetsManifest = (webpackManifestFile) => {
|
||||
const assetsManifest = (webpackManifestFile) => {
|
||||
let manifest
|
||||
try {
|
||||
manifest = JSON.parse(fs.readFileSync(webpackManifestFile, 'utf8'))
|
||||
|
@ -58,7 +58,7 @@ export const assetsManifest = (webpackManifestFile) => {
|
|||
* An express middleware that sets req.bundle from the build
|
||||
* info in the given stats file. Should be used in production.
|
||||
*/
|
||||
export const staticAssets = (webpackStatsFile) => {
|
||||
const staticAssets = (webpackStatsFile) => {
|
||||
let stats
|
||||
try {
|
||||
stats = JSON.parse(fs.readFileSync(webpackStatsFile, 'utf8'))
|
||||
|
@ -84,7 +84,7 @@ export const staticAssets = (webpackStatsFile) => {
|
|||
* latest result from a running webpack compiler (i.e. using
|
||||
* webpack-dev-middleware). Should only be used in dev.
|
||||
*/
|
||||
export const devAssets = (webpackCompiler) => {
|
||||
const devAssets = (webpackCompiler) => {
|
||||
let bundle
|
||||
webpackCompiler.plugin('done', (stats) => {
|
||||
bundle = createBundle(stats.toJson())
|
||||
|
@ -106,7 +106,7 @@ export const devAssets = (webpackCompiler) => {
|
|||
* Creates a webpack compiler that automatically inlines the
|
||||
* webpack dev runtime in all entry points.
|
||||
*/
|
||||
export const createDevCompiler = (webpackConfig, webpackRuntimeModuleID) =>
|
||||
const createDevCompiler = (webpackConfig, webpackRuntimeModuleID) =>
|
||||
webpack({
|
||||
...webpackConfig,
|
||||
entry: prependModuleID(
|
||||
|
@ -138,3 +138,10 @@ const prependModuleID = (webpackEntry, moduleID) => {
|
|||
|
||||
throw new Error('Invalid webpack entry object')
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
assetsManifest,
|
||||
staticAssets,
|
||||
devAssets,
|
||||
createDevCompiler
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import 'isomorphic-fetch'
|
||||
import { createStack, createFetch, header, base, query, parseJSON, onResponse } from 'http-client'
|
||||
import invariant from 'invariant'
|
||||
require('isomorphic-fetch')
|
||||
const { createStack, createFetch, header, base, query, parseJSON, onResponse } = require('http-client')
|
||||
const invariant = require('invariant')
|
||||
|
||||
const CloudflareKey = process.env.CLOUDFLARE_KEY
|
||||
const CloudflareEmail = process.env.CLOUDFLARE_EMAIL
|
||||
|
@ -37,19 +37,19 @@ const commonStack = createStack(
|
|||
getResult()
|
||||
)
|
||||
|
||||
export const getZones = (domainName) =>
|
||||
const getZones = (domainName) =>
|
||||
createFetch(
|
||||
commonStack,
|
||||
createNameQuery(domainName)
|
||||
)('/zones')
|
||||
|
||||
export const getZoneAnalyticsDashboard = (zone, since, until) =>
|
||||
const getZoneAnalyticsDashboard = (zone, since, until) =>
|
||||
createFetch(
|
||||
commonStack,
|
||||
createRangeQuery(since, until)
|
||||
)(`/zones/${zone.id}/analytics/dashboard`)
|
||||
|
||||
export const getAnalyticsDashboards = (domainNames, since, until) =>
|
||||
const getAnalyticsDashboards = (domainNames, since, until) =>
|
||||
Promise.all(
|
||||
domainNames.map(domainName => getZones(domainName))
|
||||
).then(
|
||||
|
@ -73,3 +73,9 @@ const reduceResults = (target, results) => {
|
|||
|
||||
return target
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getZones,
|
||||
getZoneAnalyticsDashboard,
|
||||
getAnalyticsDashboards
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import React from 'react'
|
||||
import { renderToStaticMarkup } from 'react-dom/server'
|
||||
import { getAnalyticsDashboards } from './Cloudflare'
|
||||
import HomePage from './components/HomePage'
|
||||
const React = require('react')
|
||||
const { renderToStaticMarkup } = require('react-dom/server')
|
||||
const { getAnalyticsDashboards } = require('./Cloudflare')
|
||||
const HomePage = require('./components/HomePage')
|
||||
|
||||
const OneMinute = 1000 * 60
|
||||
const ThirtyDays = OneMinute * 60 * 24 * 30
|
||||
|
@ -19,7 +19,7 @@ const fetchStats = (callback) => {
|
|||
}
|
||||
}
|
||||
|
||||
export const sendHomePage = (req, res, next) => {
|
||||
const sendHomePage = (req, res, next) => {
|
||||
const chunks = [ 'vendor', 'home' ]
|
||||
const props = {
|
||||
styles: req.bundle.getStyleAssets(chunks),
|
||||
|
@ -41,3 +41,7 @@ export const sendHomePage = (req, res, next) => {
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
sendHomePage
|
||||
}
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
import path from 'path'
|
||||
const path = require('path')
|
||||
|
||||
export const id = 1
|
||||
export const port = parseInt(process.env.PORT, 10) || 5000
|
||||
export const webpackConfig = require('../../webpack.config')
|
||||
export const statsFile = path.resolve(__dirname, '../../stats.json')
|
||||
export const publicDir = path.resolve(__dirname, '../../public')
|
||||
export const manifestFile = path.resolve(publicDir, '__assets__/chunk-manifest.json')
|
||||
export const timeout = parseInt(process.env.TIMEOUT, 10) || 20000
|
||||
export const maxAge = process.env.MAX_AGE || '365d'
|
||||
exports.id = 1
|
||||
exports.port = parseInt(process.env.PORT, 10) || 5000
|
||||
exports.webpackConfig = require('../../webpack.config')
|
||||
exports.statsFile = path.resolve(__dirname, '../../stats.json')
|
||||
exports.publicDir = path.resolve(__dirname, '../../public')
|
||||
exports.manifestFile = path.resolve(exports.publicDir, '__assets__/chunk-manifest.json')
|
||||
exports.timeout = parseInt(process.env.TIMEOUT, 10) || 20000
|
||||
exports.maxAge = process.env.MAX_AGE || '365d'
|
||||
|
||||
export const registryURL = process.env.REGISTRY_URL || 'https://registry.npmjs.org'
|
||||
export const bowerBundle = process.env.BOWER_BUNDLE || '/bower.zip'
|
||||
export const redirectTTL = process.env.REDIRECT_TTL || 500
|
||||
export const autoIndex = !process.env.DISABLE_INDEX
|
||||
export const redisURL = process.env.REDIS_URL
|
||||
exports.registryURL = process.env.REGISTRY_URL || 'https://registry.npmjs.org'
|
||||
exports.bowerBundle = process.env.BOWER_BUNDLE || '/bower.zip'
|
||||
exports.redirectTTL = process.env.REDIRECT_TTL || 500
|
||||
exports.autoIndex = !process.env.DISABLE_INDEX
|
||||
exports.redisURL = process.env.REDIS_URL
|
||||
|
||||
export blacklist from '../PackageBlacklist'
|
||||
exports.blacklist = require('../PackageBlacklist')
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import redis from 'redis'
|
||||
import onFinished from 'on-finished'
|
||||
const redis = require('redis')
|
||||
const onFinished = require('on-finished')
|
||||
|
||||
const URLFormat = /^\/((?:@[^\/@]+\/)?[^\/@]+)(?:@([^\/]+))?(\/.*)?$/
|
||||
|
||||
export const logStats = (redisURL) => {
|
||||
const logStats = (redisURL) => {
|
||||
const redisClient = redis.createClient(redisURL)
|
||||
|
||||
return (req, res, next) => {
|
||||
|
@ -25,3 +25,7 @@ export const logStats = (redisURL) => {
|
|||
next()
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
logStats
|
||||
}
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
import React, { PropTypes } from 'react'
|
||||
const React = require('react')
|
||||
|
||||
const PropTypes = React.PropTypes
|
||||
|
||||
class HomePage extends React.Component {
|
||||
static propTypes = {
|
||||
|
@ -43,4 +45,4 @@ class HomePage extends React.Component {
|
|||
}
|
||||
}
|
||||
|
||||
export default HomePage
|
||||
module.exports = HomePage
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
/*eslint-disable no-console*/
|
||||
import http from 'http'
|
||||
import cors from 'cors'
|
||||
import throng from 'throng'
|
||||
import morgan from 'morgan'
|
||||
import express from 'express'
|
||||
import devErrorHandler from 'errorhandler'
|
||||
import WebpackDevServer from 'webpack-dev-server'
|
||||
import { createRequestHandler } from 'express-unpkg'
|
||||
import * as DefaultServerConfig from './ServerConfig'
|
||||
import { assetsManifest, staticAssets, devAssets, createDevCompiler } from './AssetsUtils'
|
||||
import { sendHomePage } from './MainController'
|
||||
import { logStats } from './StatsUtils'
|
||||
const http = require('http')
|
||||
const cors = require('cors')
|
||||
const throng = require('throng')
|
||||
const morgan = require('morgan')
|
||||
const express = require('express')
|
||||
const devErrorHandler = require('errorhandler')
|
||||
const WebpackDevServer = require('webpack-dev-server')
|
||||
const { createRequestHandler } = require('express-unpkg')
|
||||
const DefaultServerConfig = require('./ServerConfig')
|
||||
const { assetsManifest, staticAssets, devAssets, createDevCompiler } = require('./AssetsUtils')
|
||||
const { sendHomePage } = require('./MainController')
|
||||
const { logStats } = require('./StatsUtils')
|
||||
|
||||
export const createRouter = (config = {}) => {
|
||||
const createRouter = (config = {}) => {
|
||||
const router = express.Router()
|
||||
|
||||
router.get('/', sendHomePage)
|
||||
|
@ -31,7 +31,7 @@ const errorHandler = (err, req, res, next) => {
|
|||
next(err)
|
||||
}
|
||||
|
||||
export const createServer = (config) => {
|
||||
const createServer = (config) => {
|
||||
const app = express()
|
||||
|
||||
app.disable('x-powered-by')
|
||||
|
@ -67,7 +67,7 @@ export const createServer = (config) => {
|
|||
return server
|
||||
}
|
||||
|
||||
export const createDevServer = (config) => {
|
||||
const createDevServer = (config) => {
|
||||
const webpackConfig = config.webpackConfig
|
||||
const compiler = createDevCompiler(
|
||||
webpackConfig,
|
||||
|
@ -107,7 +107,7 @@ export const createDevServer = (config) => {
|
|||
return server
|
||||
}
|
||||
|
||||
export const startServer = (serverConfig) => {
|
||||
const startServer = (serverConfig) => {
|
||||
const config = {
|
||||
...DefaultServerConfig,
|
||||
...serverConfig
|
||||
|
@ -122,6 +122,12 @@ export const startServer = (serverConfig) => {
|
|||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createServer,
|
||||
createDevServer,
|
||||
startServer
|
||||
}
|
||||
|
||||
if (require.main === module)
|
||||
throng({
|
||||
start: (id) => startServer({ id }),
|
||||
|
|
Loading…
Reference in New Issue