@@ -3,11 +3,12 @@ import fs from "fs-extra"
3
3
import reporter from "gatsby-cli/lib/reporter"
4
4
import { createErrorFromString } from "gatsby-cli/lib/reporter/errors"
5
5
import { chunk , truncate } from "lodash"
6
- import webpack , { Stats } from "webpack"
6
+ import { build , watch } from "../utils/ webpack/bundle "
7
7
import * as path from "path"
8
8
9
9
import { emitter , store } from "../redux"
10
10
import { IWebpackWatchingPauseResume } from "../utils/start-server"
11
+ import webpack from "webpack"
11
12
import webpackConfig from "../utils/webpack.config"
12
13
import { structureWebpackErrors } from "../utils/webpack-error-utils"
13
14
import * as buildUtils from "./build-utils"
@@ -17,10 +18,10 @@ import { Span } from "opentracing"
17
18
import { IProgram , Stage } from "./types"
18
19
import { ROUTES_DIRECTORY } from "../constants"
19
20
import { PackageJson } from "../.."
20
- import type { GatsbyWorkerPool } from "../utils/worker/pool"
21
21
import { IPageDataWithQueryResult } from "../utils/page-data"
22
22
import { processNodeManifests } from "../utils/node-manifest"
23
23
24
+ import type { GatsbyWorkerPool } from "../utils/worker/pool"
24
25
type IActivity = any // TODO
25
26
26
27
export interface IBuildArgs extends IProgram {
@@ -37,8 +38,13 @@ export interface IBuildArgs extends IProgram {
37
38
keepPageRenderer : boolean
38
39
}
39
40
40
- let devssrWebpackCompiler : webpack . Compiler
41
- let devssrWebpackWatcher : IWebpackWatchingPauseResume
41
+ interface IBuildRendererResult {
42
+ rendererPath : string
43
+ stats : webpack . Stats
44
+ close : ReturnType < typeof watch > [ "close" ]
45
+ }
46
+
47
+ let devssrWebpackCompiler : webpack . Watching | undefined
42
48
let needToRecompileSSRBundle = true
43
49
export const getDevSSRWebpack = ( ) : {
44
50
devssrWebpackWatcher : IWebpackWatchingPauseResume
@@ -50,8 +56,8 @@ export const getDevSSRWebpack = (): {
50
56
}
51
57
52
58
return {
53
- devssrWebpackWatcher,
54
- devssrWebpackCompiler,
59
+ devssrWebpackWatcher : devssrWebpackCompiler as webpack . Watching ,
60
+ devssrWebpackCompiler : ( devssrWebpackCompiler as webpack . Watching ) . compiler ,
55
61
needToRecompileSSRBundle,
56
62
}
57
63
}
@@ -61,60 +67,33 @@ let newHash = ``
61
67
const runWebpack = (
62
68
compilerConfig ,
63
69
stage : Stage ,
64
- directory ,
65
- parentSpan ?: Span
66
- ) : Bluebird < {
67
- stats : Stats
68
- waitForCompilerClose : Promise < void >
69
- } > =>
70
- new Bluebird ( ( resolve , reject ) => {
71
- if ( ! process . env . GATSBY_EXPERIMENTAL_DEV_SSR || stage === `build-html` ) {
70
+ directory : string
71
+ ) : Promise < {
72
+ stats : webpack . Stats
73
+ close : ReturnType < typeof watch > [ "close" ]
74
+ } > => {
75
+ const isDevSSREnabledAndViable =
76
+ process . env . GATSBY_EXPERIMENTAL_DEV_SSR && stage === `develop-html`
77
+
78
+ return new Promise ( ( resolve , reject ) => {
79
+ if ( isDevSSREnabledAndViable ) {
72
80
const compiler = webpack ( compilerConfig )
73
- compiler . run ( ( err , stats ) => {
74
- let activity
75
- if ( process . env . GATSBY_EXPERIMENTAL_PRESERVE_WEBPACK_CACHE ) {
76
- activity = reporter . activityTimer (
77
- `Caching HTML renderer compilation` ,
78
- { parentSpan }
79
- )
80
- activity . start ( )
81
- }
82
-
83
- const waitForCompilerClose = new Promise < void > ( ( resolve , reject ) => {
84
- compiler . close ( error => {
85
- if ( activity ) {
86
- activity . end ( )
87
- }
88
-
89
- if ( error ) {
90
- return reject ( error )
91
- }
92
- return resolve ( )
93
- } )
94
- } )
95
81
96
- if ( err ) {
97
- return reject ( err )
98
- } else {
99
- return resolve ( { stats : stats as Stats , waitForCompilerClose } )
100
- }
101
- } )
102
- } else if (
103
- process . env . GATSBY_EXPERIMENTAL_DEV_SSR &&
104
- stage === `develop-html`
105
- ) {
106
- devssrWebpackCompiler = webpack ( compilerConfig )
107
- devssrWebpackCompiler . hooks . invalid . tap ( `ssr file invalidation` , ( ) => {
82
+ // because of this line we can't use our watch helper
83
+ // These things should use emitter
84
+ compiler . hooks . invalid . tap ( `ssr file invalidation` , ( ) => {
108
85
needToRecompileSSRBundle = true
109
86
} )
110
- devssrWebpackWatcher = devssrWebpackCompiler . watch (
87
+
88
+ const watcher = compiler . watch (
111
89
{
112
90
ignored : / n o d e _ m o d u l e s / ,
113
91
} ,
114
92
( err , stats ) => {
93
+ // this runs multiple times
115
94
needToRecompileSSRBundle = false
116
95
emitter . emit ( `DEV_SSR_COMPILATION_DONE` )
117
- devssrWebpackWatcher . suspend ( )
96
+ watcher . suspend ( )
118
97
119
98
if ( err ) {
120
99
return reject ( err )
@@ -132,59 +111,52 @@ const runWebpack = (
132
111
oldHash = newHash
133
112
134
113
return resolve ( {
135
- stats : stats as Stats ,
136
- waitForCompilerClose : Promise . resolve ( ) ,
114
+ stats : stats as webpack . Stats ,
115
+ close : ( ) =>
116
+ new Promise ( ( resolve , reject ) : void =>
117
+ watcher . close ( err => ( err ? reject ( err ) : resolve ( ) ) )
118
+ ) ,
137
119
} )
138
120
}
139
121
}
140
- ) as IWebpackWatchingPauseResume
122
+ )
123
+ devssrWebpackCompiler = watcher
124
+ } else {
125
+ build ( compilerConfig ) . then ( ( { stats, close } ) => {
126
+ resolve ( { stats, close } )
127
+ } )
141
128
}
142
129
} )
130
+ }
143
131
144
132
const doBuildRenderer = async (
145
- { directory } : IProgram ,
133
+ directory : string ,
146
134
webpackConfig : webpack . Configuration ,
147
- stage : Stage ,
148
- parentSpan ?: Span
149
- ) : Promise < { rendererPath : string ; waitForCompilerClose } > => {
150
- const { stats, waitForCompilerClose } = await runWebpack (
151
- webpackConfig ,
152
- stage ,
153
- directory ,
154
- parentSpan
155
- )
156
- if ( stats . hasErrors ( ) ) {
135
+ stage : Stage
136
+ ) : Promise < IBuildRendererResult > => {
137
+ const { stats, close } = await runWebpack ( webpackConfig , stage , directory )
138
+ if ( stats ?. hasErrors ( ) ) {
157
139
reporter . panic ( structureWebpackErrors ( stage , stats . compilation . errors ) )
158
140
}
159
141
160
- if (
161
- stage === `build-html` &&
162
- store . getState ( ) . html . ssrCompilationHash !== stats . hash
163
- ) {
164
- store . dispatch ( {
165
- type : `SET_SSR_WEBPACK_COMPILATION_HASH` ,
166
- payload : stats . hash as string ,
167
- } )
168
- }
169
-
170
142
// render-page.js is hard coded in webpack.config
171
143
return {
172
144
rendererPath : `${ directory } /${ ROUTES_DIRECTORY } render-page.js` ,
173
- waitForCompilerClose,
145
+ stats,
146
+ close,
174
147
}
175
148
}
176
149
177
150
export const buildRenderer = async (
178
151
program : IProgram ,
179
152
stage : Stage ,
180
153
parentSpan ?: IActivity
181
- ) : Promise < { rendererPath : string ; waitForCompilerClose } > => {
182
- const { directory } = program
183
- const config = await webpackConfig ( program , directory , stage , null , {
154
+ ) : Promise < IBuildRendererResult > => {
155
+ const config = await webpackConfig ( program , program . directory , stage , null , {
184
156
parentSpan,
185
157
} )
186
158
187
- return doBuildRenderer ( program , config , stage , parentSpan )
159
+ return doBuildRenderer ( program . directory , config , stage )
188
160
}
189
161
190
162
// TODO remove after v4 release and update cloud internals
0 commit comments