|
1 |
| -const path = require('path') |
2 |
| -const exec = require('child_process').exec |
3 |
| -const Express = require('express') |
4 |
| -const watch = require('node-watch') |
| 1 | +const path = require('path'); |
| 2 | +const exec = require('child_process').exec; |
| 3 | +const Express = require('express'); |
| 4 | +const watch = require('node-watch'); |
5 | 5 |
|
6 |
| -import fs from 'fs' |
7 |
| -import React from 'react' |
8 |
| -import { renderToString } from 'react-dom/server' |
9 |
| -import { ServerStyleSheet } from '..' |
10 |
| -import getExample from './example' |
| 6 | +import fs from 'fs'; |
| 7 | +import React from 'react'; |
| 8 | +import { renderToString } from 'react-dom/server'; |
| 9 | +import { ServerStyleSheet } from '..'; |
| 10 | +import getExample from './example'; |
11 | 11 |
|
12 |
| -const HTML = fs.readFileSync(__dirname + '/index.html').toString() |
| 12 | +const HTML = fs.readFileSync(__dirname + '/index.html').toString(); |
13 | 13 |
|
14 |
| -const srcPath = __dirname.split('/example')[0] + '/src' |
| 14 | +const srcPath = __dirname.split('/example')[0] + '/src'; |
15 | 15 |
|
16 | 16 | const hotBuild = () =>
|
17 | 17 | exec('npm run build:dist', (err, stdout, stderr) => {
|
18 |
| - if (err) throw err |
| 18 | + if (err) throw err; |
19 | 19 | if (stdout) {
|
20 |
| - console.log(`npm run build:dist --- ${stdout}`) |
| 20 | + console.log(`npm run build:dist --- ${stdout}`); |
21 | 21 | }
|
22 | 22 | if (stderr) {
|
23 |
| - console.log(`npm run build:dist --- ${stderr}`) |
| 23 | + console.log(`npm run build:dist --- ${stderr}`); |
24 | 24 | }
|
25 |
| - }) |
| 25 | + }); |
26 | 26 |
|
27 | 27 | watch(srcPath, filename => {
|
28 |
| - console.log(`${filename} file has changed`) |
29 |
| - hotBuild() |
30 |
| -}) |
| 28 | + console.log(`${filename} file has changed`); |
| 29 | + hotBuild(); |
| 30 | +}); |
31 | 31 |
|
32 |
| -const app = new Express() |
| 32 | +const app = new Express(); |
33 | 33 |
|
34 |
| -app.use(Express.static(__dirname)) |
35 |
| -app.use(Express.static('dist')) |
| 34 | +app.use(Express.static(__dirname)); |
| 35 | +app.use(Express.static('dist')); |
36 | 36 |
|
37 | 37 | app.get('/with-perf.html', (req, res) => {
|
38 |
| - res.sendFile(path.join(__dirname, 'with-perf.html')) |
39 |
| -}) |
| 38 | + res.sendFile(path.join(__dirname, 'with-perf.html')); |
| 39 | +}); |
40 | 40 |
|
41 | 41 | app.get('/ssr.html', (req, res) => {
|
42 |
| - const Example = getExample() |
| 42 | + const Example = getExample(); |
43 | 43 |
|
44 |
| - const sheet = new ServerStyleSheet() |
45 |
| - const html = renderToString(sheet.collectStyles(<Example />)) |
46 |
| - const css = sheet.getStyleTags() |
47 |
| - res.send( |
48 |
| - HTML.replace(/<!-- SSR:HTML -->/, html).replace(/<!-- SSR:CSS -->/, css) |
49 |
| - ) |
50 |
| -}) |
| 44 | + const sheet = new ServerStyleSheet(); |
| 45 | + const html = renderToString(sheet.collectStyles(<Example />)); |
| 46 | + const css = sheet.getStyleTags(); |
| 47 | + res.send(HTML.replace(/<!-- SSR:HTML -->/, html).replace(/<!-- SSR:CSS -->/, css)); |
| 48 | +}); |
51 | 49 |
|
52 | 50 | app.get('/', (req, res) => {
|
53 |
| - res.sendFile(path.join(__dirname, 'index.html')) |
54 |
| -}) |
| 51 | + res.sendFile(path.join(__dirname, 'index.html')); |
| 52 | +}); |
55 | 53 |
|
56 |
| -export default app |
| 54 | +export default app; |
0 commit comments