-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathstart-webpack-server.ts
135 lines (121 loc) · 3.85 KB
/
start-webpack-server.ts
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
import openurl from "better-opn"
import report from "gatsby-cli/lib/reporter"
import chalk from "chalk"
import { Compiler } from "webpack"
import { Stage } from "../commands/types"
import {
reportWebpackWarnings,
structureWebpackErrors,
} from "../utils/webpack-error-utils"
import { printDeprecationWarnings } from "../utils/print-deprecation-warnings"
import { showExperimentNotices } from "../utils/show-experiment-notice"
import { printInstructions } from "../utils/print-instructions"
import { prepareUrls } from "../utils/prepare-urls"
import { startServer, IWebpackWatchingPauseResume } from "../utils/start-server"
import { WebsocketManager } from "../utils/websocket-manager"
import { IBuildContext } from "./"
import {
markWebpackStatusAsPending,
markWebpackStatusAsDone,
} from "../utils/webpack-status"
import { emitter } from "../redux"
export async function startWebpackServer({
program,
app,
workerPool,
store,
}: Partial<IBuildContext>): Promise<{
compiler: Compiler
websocketManager: WebsocketManager
webpackWatching: IWebpackWatchingPauseResume
}> {
if (!program || !app || !store) {
report.panic(`Missing required params`)
}
let {
compiler,
webpackActivity,
websocketManager,
cancelDevJSNotice,
webpackWatching,
} = await startServer(program, app, workerPool)
webpackWatching.suspend()
compiler.hooks.invalid.tap(`log compiling`, function () {
if (!webpackActivity) {
// mark webpack as pending if we are not in the middle of compilation already
// when input is invalidated during compilation, webpack will automatically
// run another compilation round before triggering `done` event
report.pendingActivity({ id: `webpack-develop` })
markWebpackStatusAsPending()
}
})
compiler.hooks.watchRun.tapAsync(`log compiling`, function (_, done) {
if (!webpackActivity) {
// there can be multiple `watchRun` events before receiving single `done` event
// webpack will not emit assets or `done` event until all pending invalidated
// inputs were compiled
webpackActivity = report.activityTimer(`Re-building development bundle`, {
id: `webpack-develop`,
})
webpackActivity.start()
}
done()
})
let isFirstCompile = true
return new Promise(resolve => {
compiler.hooks.done.tapAsync(`print gatsby instructions`, async function (
stats,
done
) {
if (cancelDevJSNotice) {
cancelDevJSNotice()
}
const urls = prepareUrls(
program.https ? `https` : `http`,
program.host,
program.proxyPort
)
const isSuccessful = !stats.hasErrors()
if (isSuccessful && isFirstCompile) {
// Show notices to users about potential experiments/feature flags they could
// try.
showExperimentNotices()
printInstructions(
program.sitePackageJson.name || `(Unnamed package)`,
urls
)
printDeprecationWarnings()
if (program.open) {
try {
await openurl(urls.localUrlForBrowser)
} catch {
console.log(
`${chalk.yellow(
`warn`
)} Browser not opened because no browser was found`
)
}
}
}
isFirstCompile = false
if (webpackActivity) {
if (stats.hasWarnings()) {
reportWebpackWarnings(stats.compilation.warnings, report)
}
if (!isSuccessful) {
const errors = structureWebpackErrors(
Stage.Develop,
stats.compilation.errors
)
webpackActivity.panicOnBuild(errors)
}
webpackActivity.end()
webpackActivity = null
}
markWebpackStatusAsDone()
done()
emitter.emit(`COMPILATION_DONE`, stats)
resolve({ compiler, websocketManager, webpackWatching })
})
})
}