This repository has been archived by the owner on May 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreact4xp.gradle
293 lines (240 loc) · 11.3 KB
/
react4xp.gradle
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
////////////////////////////////////////////////////////////////////////////////
// Config section - handles react4xp.properties //
// and uses those values to both configure build tasks here as well as //
// write a JSON config file that's used at runtime //
// (also see react4xp-buildconstants under packages/constants //
////////////////////////////////////////////////////////////////////////////////
// Resolves the project folder root
def ROOT = project.projectDir.toString()
// Set react4xp build mode from react4xp.properties,
// overridable by CLI arg, e.g -Pdev etc.:
// If none are set, 'production' is default.
project.ext.react4xp = {}
file("react4xp.properties").withReader { reader ->
project.ext.react4xp = new Properties()
project.ext.react4xp.load(reader)
}
project.ext.react4xp.BUILD_ENV = "production"
if (project.hasProperty("dev") || project.hasProperty("development")) {
project.ext.react4xp.BUILD_ENV = "development"
} else if (project.hasProperty("prod") || project.hasProperty("production")) {
project.ext.react4xp.BUILD_ENV = 'production'
} else if (project.ext.react4xp.buildEnv != null) {
if (project.ext.react4xp.buildEnv != 'production' && project.ext.react4xp.buildEnv != 'development') {
throw new IllegalStateException("Illegal react4xp.buildEnv argument: '${project.ext.react4xp.buildEnv}'. Use 'production' or 'development'.")
}
project.ext.react4xp.BUILD_ENV = project.ext.react4xp.buildEnv
}
if (project.hasProperty("link") || project.hasProperty("linked") || project.ext.react4xp.BUILD_ENV == "development") {
project.ext.react4xp.NPMLINKED = true
}
project.ext.react4xp.verbose = project.hasProperty("verbose") || (project.ext.react4xp.BUILD_ENV == 'development')
println "React4xp build mode: ${project.ext.react4xp.BUILD_ENV}"
if (project.ext.react4xp.nashornPolyfillsSource != null) {
project.ext.react4xp.NASHORNPOLYFILLS_SOURCE = project.ext.react4xp.nashornPolyfillsSource
}
project.ext.react4xp.masterConfigFileName = project.ext.react4xp.masterConfigFileName != null ? project.ext.react4xp.masterConfigFileName : "build/react4xp_constants.json"
project.ext.react4xp.outputFileName = ROOT + '/' + project.ext.react4xp.masterConfigFileName
project.ext.react4xp.buildRuntimeClient = project.ext.react4xp.buildRuntimeClient != null && project.ext.react4xp.buildRuntimeClient.toBoolean()
project.ext.react4xp.buildExternals = project.ext.react4xp.buildExternals != null && project.ext.react4xp.buildExternals.toBoolean()
project.ext.react4xp.overwriteConstantsFile = project.ext.react4xp.overwriteConstantsFile != null && project.ext.react4xp.overwriteConstantsFile.toBoolean()
if (project.ext.react4xp.ssrLazyload != null) {
project.ext.react4xp.SSR_LAZYLOAD = project.ext.react4xp.ssrLazyload.toBoolean()
if (project.ext.react4xp.verbose) {
println "react4xp.properties.ssrLazyload=${project.ext.react4xp.ssrLazyload} --> SSR_LAZYLOAD=${project.ext.react4xp.SSR_LAZYLOAD}"
}
} else {
project.ext.react4xp.SSR_LAZYLOAD = (project.ext.react4xp.BUILD_ENV == 'development')
if (project.ext.react4xp.verbose) {
println "Because BUILD_ENV='${project.ext.react4xp.BUILD_ENV}' --> SSR_LAZYLOAD=${project.ext.react4xp.SSR_LAZYLOAD}"
}
}
if (project.ext.react4xp.ssrSettings != null) {
project.ext.react4xp.SSR_ENGINE_SETTINGS = project.ext.react4xp.ssrSettings
if (project.ext.react4xp.verbose) {
println "react4xp.properties.ssrSettings=${project.ext.react4xp.ssrSettings} --> SSR_ENGINE_SETTINGS=${project.ext.react4xp.SSR_ENGINE_SETTINGS}"
}
} else {
//project.ext.react4xp.SSR_ENGINE_SETTINGS = null
if (project.ext.react4xp.verbose) {
println "SSR_ENGINE_SETTINGS: none"
}
}
if (project.ext.react4xp.ssrMaxThreads != null) {
project.ext.react4xp.SSR_MAX_THREADS = project.ext.react4xp.ssrMaxThreads
if (project.ext.react4xp.verbose) {
println "react4xp.properties.ssrMaxThreads=${project.ext.react4xp.ssrMaxThreads} --> SSR_MAX_THREADS=${project.ext.react4xp.SSR_MAX_THREADS}"
}
} else {
project.ext.react4xp.SSR_MAX_THREADS = 0
if (project.ext.react4xp.verbose) {
println "SSR_MAX_THREADS: unlimited"
}
}
if (project.ext.react4xp.verbose) {
println "React4xp project config: ${project.ext.react4xp}"
}
// Build the master config JSON file and the copy:
task config_react4xp(type: NodeTask) {
group 'React4xp'
description 'Build the master config JSON file and its copy'
script = file('node_modules/react4xp/bin/buildConstants.js')
// react4xp-buildconstants comes with react4xp@^0.2.0
args = [ROOT, JsonOutput.toJson(JsonOutput.toJson(project.ext.react4xp))]
}
config_react4xp.inputs.file("react4xp.properties")
config_react4xp.outputs.file(project.ext.react4xp.masterConfigFileName)
config_react4xp.dependsOn += 'processResources'
// Necessary placeholder, will be filled during build
def CONFIG = {}
task config_tasks {
// After the above script has run and created the config file, use the constructed values from the script to update the configuration of the next task(s):
doLast {
// Read the file content into an object
def REACT4XP_TASKS = [
"react4xpComponents",
"react4xpExternals",
"react4xpClient",
"react4xpNashornPolyfills"
]
def configFile = new File(project.ext.react4xp.masterConfigFileName)
CONFIG = new JsonSlurper().parseText(configFile.text)
REACT4XP_TASKS.each {
tasks["${it}"].configure {
inputs.dir(CONFIG.SRC_SITE)
inputs.dir(CONFIG.SRC_R4X)
outputs.dir(CONFIG.BUILD_R4X)
}
}
}
}
config_tasks.dependsOn += 'config_react4xp'
// Config is done. Actual build steps follow:
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Compile:
task react4xpComponents(type: NodeTask) {
group 'React4xp'
description 'Compile the parent projects react components'
// 1 MANDATORY STEP: react4xp/buildComponents compiles the components added in this project into runnable/renderable components. See react4xp/buildComponents docs.
script = file('node_modules/webpack/bin/webpack.js')
args = [
'--config', 'node_modules/react4xp/buildComponents/webpack.config.js',
'--color',
'--env', 'VERBOSE=' + project.ext.react4xp.verbose,
'--env', 'ENTRY_DIRS=' + project.ext.react4xp.entryDirs,
'--env', 'CHUNK_DIRS=' + project.ext.react4xp.chunkDirs,
'--env', 'ROOT="' + ROOT + '"'
]
if (project.ext.react4xp.overrideComponentWebpack != null) {
args.addAll(['--env', 'OVERRIDE_COMPONENT_WEBPACK=' + project.ext.react4xp.overrideComponentWebpack])
}
// Pretty if chatty
if (project.ext.react4xp.verbose) {
args.addAll(['--progress'])
}
// Finally, and mandatorily: tells all of the webpack steps here where to find the react4xp master config file that was built during the config_react4xp task
args.addAll(['--env', 'REACT4XP_CONFIG_FILE=' + project.ext.react4xp.masterConfigFileName])
/*
if (project.ext.react4xp.verbose) {
println "react4xpComponents task - args:\n"
}*/
inputs.file(project.ext.react4xp.outputFileName)
inputs.file("package.json")
inputs.file("package-lock.json")
}
react4xpComponents.dependsOn += 'config_tasks'
jar.dependsOn += "react4xpComponents"
task react4xpExternals(type: NodeTask) {
group 'React4xp'
description 'Compile the externals (react and react-dom'
// 1 MANDATORY STEP: react4xp/buildComponents compiles the components added in this project into runnable/renderable components. See react4xp/buildComponents docs.
script = file('node_modules/webpack/bin/webpack.js')
args = [
'--config', 'node_modules/react4xp/externals/webpack.config.js',
'--color',
'--env', 'VERBOSE=' + project.ext.react4xp.verbose,
'--env', 'ENTRY_DIRS=' + project.ext.react4xp.entryDirs,
'--env', 'CHUNK_DIRS=' + project.ext.react4xp.chunkDirs,
'--env', 'ROOT="' + ROOT + '"'
]
// Pretty if chatty
if (project.ext.react4xp.verbose) {
args.addAll(['--progress'])
}
// Finally, and mandatorily: tells all of the webpack steps here where to find the react4xp master config file that was built during the config_react4xp task
args.addAll(['--env', 'REACT4XP_CONFIG_FILE=' + project.ext.react4xp.masterConfigFileName])
/* if (project.ext.react4xp.verbose && project.ext.react4xp.buildExternals) {
println "react4xpExternals task - args:"
println "\t${args}\n"
}*/
inputs.file(project.ext.react4xp.outputFileName)
inputs.file("package.json")
inputs.file("package-lock.json")
}
react4xpExternals.dependsOn += 'config_tasks'
if (project.ext.react4xp.buildExternals) {
jar.dependsOn += 'react4xpExternals'
}
task react4xpClient(type: NodeTask) {
group 'React4xp'
description 'Compile the react4xp runtime client'
// 1 MANDATORY STEP: react4xp/buildComponents compiles the components added in this project into runnable/renderable components. See react4xp/buildComponents docs.
script = file('node_modules/webpack/bin/webpack.js')
args = [
'--config', 'node_modules/react4xp/client/webpack.config.js',
'--color',
'--env', 'VERBOSE=' + project.ext.react4xp.verbose,
'--env', 'ENTRY_DIRS=' + project.ext.react4xp.entryDirs,
'--env', 'CHUNK_DIRS=' + project.ext.react4xp.chunkDirs,
'--env', 'ROOT="' + ROOT + '"'
]
// Pretty if chatty
if (project.ext.react4xp.verbose) {
args.addAll(['--progress'])
}
// Finally, and mandatorily: tells all of the webpack steps here where to find the react4xp master config file that was built during the config_react4xp task
args.addAll(['--env', 'REACT4XP_CONFIG_FILE=' + project.ext.react4xp.masterConfigFileName])
if (project.ext.react4xp.verbose && project.ext.react4xp.buildRuntimeClient) {
println "react4xpClient task - args:"
println "\t${args}\n"
}
inputs.file(project.ext.react4xp.outputFileName)
inputs.file("package.json")
inputs.file("package-lock.json")
}
react4xpClient.dependsOn += 'config_tasks'
if (project.ext.react4xp.buildRuntimeClient) {
jar.dependsOn += 'react4xpClient'
}
task react4xpNashornPolyfills(type: NodeTask) {
group 'React4xp'
description 'Run the imported react4xp webpack scripts that compile the components and externals (as well as client and nashorn polyfills if needed)'
script = file('node_modules/webpack/bin/webpack.js')
args = [
'--config', 'node_modules/react4xp/nashornPolyfills/webpack.config.js',
'--color',
'--env', 'VERBOSE=' + project.ext.react4xp.verbose,
'--env', 'ENTRY_DIRS=' + project.ext.react4xp.entryDirs,
'--env', 'CHUNK_DIRS=' + project.ext.react4xp.chunkDirs,
'--env', 'ROOT="' + ROOT + '"'
]
// Pretty if chatty
if (project.ext.react4xp.verbose) {
args.addAll(['--progress'])
}
// Finally, and mandatorily: tells all of the webpack steps here where to find the react4xp master config file that was built during the config_react4xp task
args.addAll(['--env', 'REACT4XP_CONFIG_FILE=' + project.ext.react4xp.masterConfigFileName])
if (project.ext.react4xp.verbose && project.ext.react4xp.nashornPolyfillsSource != null) {
println "react4xpNashornPolyfills task - args:"
println "\t${args}\n"
}
inputs.file(project.ext.react4xp.outputFileName)
inputs.file("package.json")
inputs.file("package-lock.json")
}
react4xpNashornPolyfills.dependsOn += 'config_tasks'
if (project.ext.react4xp.nashornPolyfillsSource != null) {
jar.dependsOn += 'react4xpNashornPolyfills'
}