-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathdevelop.js
214 lines (188 loc) · 5.75 KB
/
develop.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* @flow weak */
require('node-cjsx').transform()
import detect from 'detect-port'
import Hapi from 'hapi'
import Boom from 'boom'
import React from 'react'
import ReactDOMServer from 'react-dom/server'
import webpack from 'webpack'
import Negotiator from 'negotiator'
import parsePath from 'parse-filepath'
import _ from 'lodash'
import webpackRequire from 'webpack-require'
import WebpackPlugin from 'hapi-webpack-plugin'
import opn from 'opn'
import fs from 'fs'
import glob from 'glob'
import rl from 'readline'
const rlInterface = rl.createInterface({
input: process.stdin,
output: process.stdout,
})
import globPages from './glob-pages'
import webpackConfig from './webpack.config'
const debug = require('debug')('gatsby:application')
function startServer (program, launchPort) {
const directory = program.directory
const serverPort = launchPort || program.port
// Load pages for the site.
return globPages(directory, (err, pages) => {
const compilerConfig = webpackConfig(program, directory, 'develop', program.port)
const compiler = webpack(compilerConfig.resolve())
let HTMLPath = `${directory}/html`
// Check if we can't find an html component in root of site.
if (glob.sync(`${HTMLPath}.*`).length === 0) {
HTMLPath = '../isomorphic/html'
}
const htmlCompilerConfig = webpackConfig(program, directory, 'develop-html', program.port)
webpackRequire(htmlCompilerConfig.resolve(), require.resolve(HTMLPath), (error, factory) => {
if (error) {
console.log(`Failed to require ${directory}/html.js`)
error.forEach((e) => {
console.log(e)
})
process.exit()
}
const HTML = factory()
debug('Configuring develop server')
const server = new Hapi.Server()
server.connection({
host: program.host,
port: serverPort,
})
server.route({
method: 'GET',
path: '/html/{path*}',
handler: (request, reply) => {
if (request.path === 'favicon.ico') {
return reply(Boom.notFound())
}
try {
const htmlElement = React.createElement(
HTML, {
body: '',
}
)
let html = ReactDOMServer.renderToStaticMarkup(htmlElement)
html = `<!DOCTYPE html>\n${html}`
return reply(html)
} catch (e) {
console.log(e.stack)
throw e
}
},
})
server.route({
method: 'GET',
path: '/{path*}',
handler: {
directory: {
path: `${program.directory}/pages`,
listing: false,
index: false,
},
},
})
server.ext('onRequest', (request, reply) => {
const negotiator = new Negotiator(request.raw.req)
// Try to map the url path to match an actual path of a file on disk.
const parsed = parsePath(request.path)
const page = _.find(pages, (p) => p.path === (`${parsed.dirname}/`))
let absolutePath = `${program.directory}/pages`
let path
if (page) {
path = `/${parsePath(page.requirePath).dirname}/${parsed.basename}`
absolutePath += `/${parsePath(page.requirePath).dirname}/${parsed.basename}`
} else {
path = request.path
absolutePath += request.path
}
let isFile = false
try {
isFile = fs.lstatSync(absolutePath).isFile()
} catch (e) {
// Ignore.
}
// If the path matches a file, return that.
if (isFile) {
request.setUrl(path)
reply.continue()
// Let people load the bundle.js directly.
} else if (request.path === '/bundle.js') {
reply.continue()
} else if (negotiator.mediaType() === 'text/html') {
request.setUrl(`/html${request.path}`)
reply.continue()
} else {
reply.continue()
}
})
const assets = {
noInfo: true,
reload: true,
publicPath: compilerConfig._config.output.publicPath,
}
const hot = {
hot: true,
quiet: true,
noInfo: true,
host: program.host,
headers: {
'Access-Control-Allow-Origin': '*',
},
stats: {
colors: true,
},
}
return server.register({
register: WebpackPlugin,
options: {
compiler,
assets,
hot,
},
}, (er) => {
if (er) {
console.log(er)
process.exit()
}
server.start((e) => {
if (e) {
if (e.code === 'EADDRINUSE') {
// eslint-disable-next-line max-len
console.log(`Unable to start Gatsby on port ${serverPort} as there's already a process listing on that port.`)
} else {
console.log(e)
}
process.exit()
} else {
if (program.open) {
opn(server.info.uri)
}
console.log('Listening at:', server.info.uri)
}
})
})
})
})
}
module.exports = (program) => {
detect(program.port, (err, _port) => {
if (err) {
console.error(err)
process.exit()
}
if (program.port !== _port) {
// eslint-disable-next-line max-len
const question = `Something is already running at port ${program.port} \nWould you like to run the app at another port instead? [Y/n] `
return rlInterface.question(question, (answer) => {
let launchPort = program.port
if (answer.length === 0 || answer.match(/^yes|y$/i)) {
launchPort = _port
}
return startServer(program, launchPort)
})
}
return startServer(program)
})
}