@@ -38,6 +38,20 @@ if (dateString.startsWith("'")) {
3838 dateString = dateString . substr ( 1 , 8 ) ;
3939}
4040
41+ // Build the artifacts using a placeholder React version. We'll then do a string
42+ // replace to swap it with the correct version per release channel.
43+ //
44+ // The placeholder version is the same format that the "next" channel uses
45+ const PLACEHOLDER_REACT_VERSION =
46+ ReactVersion + '-' + nextChannelLabel + '-' + sha + '-' + dateString ;
47+
48+ // TODO: We should inject the React version using a build-time parameter
49+ // instead of overwriting the source files.
50+ fs . writeFileSync (
51+ './packages/shared/ReactVersion.js' ,
52+ `export default '${ PLACEHOLDER_REACT_VERSION } ';\n`
53+ ) ;
54+
4155if ( process . env . CIRCLE_NODE_TOTAL ) {
4256 // In CI, we use multiple concurrent processes. Allocate half the processes to
4357 // build the stable channel, and the other half for experimental. Override
@@ -48,33 +62,21 @@ if (process.env.CIRCLE_NODE_TOTAL) {
4862 if ( index < halfTotal ) {
4963 const nodeTotal = halfTotal ;
5064 const nodeIndex = index ;
51- updateTheReactVersionThatDevToolsReads (
52- ReactVersion + '-' + sha + '-' + dateString
53- ) ;
5465 buildForChannel ( 'stable' , nodeTotal , nodeIndex ) ;
5566 processStable ( './build' ) ;
5667 } else {
5768 const nodeTotal = total - halfTotal ;
5869 const nodeIndex = index - halfTotal ;
59- updateTheReactVersionThatDevToolsReads (
60- ReactVersion + '-experimental-' + sha + '-' + dateString
61- ) ;
6270 buildForChannel ( 'experimental' , nodeTotal , nodeIndex ) ;
6371 processExperimental ( './build' ) ;
6472 }
6573} else {
6674 // Running locally, no concurrency. Move each channel's build artifacts into
6775 // a temporary directory so that they don't conflict.
68- updateTheReactVersionThatDevToolsReads (
69- ReactVersion + '-' + sha + '-' + dateString
70- ) ;
7176 buildForChannel ( 'stable' , '' , '' ) ;
7277 const stableDir = tmp . dirSync ( ) . name ;
7378 crossDeviceRenameSync ( './build' , stableDir ) ;
7479 processStable ( stableDir ) ;
75- updateTheReactVersionThatDevToolsReads (
76- ReactVersion + '-experimental-' + sha + '-' + dateString
77- ) ;
7880 buildForChannel ( 'experimental' , '' , '' ) ;
7981 const experimentalDir = tmp . dirSync ( ) . name ;
8082 crossDeviceRenameSync ( './build' , experimentalDir ) ;
@@ -129,6 +131,10 @@ function processStable(buildDir) {
129131 true
130132 ) ;
131133 fs . renameSync ( buildDir + '/node_modules' , buildDir + '/oss-stable' ) ;
134+ updatePlaceholderReactVersionInCompiledArtifacts (
135+ buildDir + '/oss-stable' ,
136+ ReactVersion + '-' + nextChannelLabel + '-' + sha + '-' + dateString
137+ ) ;
132138
133139 // Now do the semver ones
134140 const semverVersionsMap = new Map ( ) ;
@@ -142,6 +148,10 @@ function processStable(buildDir) {
142148 defaultVersionIfNotFound ,
143149 false
144150 ) ;
151+ updatePlaceholderReactVersionInCompiledArtifacts (
152+ buildDir + '/oss-stable-semver' ,
153+ ReactVersion
154+ ) ;
145155 }
146156
147157 if ( fs . existsSync ( buildDir + '/facebook-www' ) ) {
@@ -152,6 +162,10 @@ function processStable(buildDir) {
152162 fs . renameSync ( filePath , filePath . replace ( '.js' , '.classic.js' ) ) ;
153163 }
154164 }
165+ updatePlaceholderReactVersionInCompiledArtifacts (
166+ buildDir + '/facebook-www' ,
167+ ReactVersion + '-www-classic-' + sha + '-' + dateString
168+ ) ;
155169 }
156170
157171 if ( fs . existsSync ( buildDir + '/sizes' ) ) {
@@ -162,7 +176,7 @@ function processStable(buildDir) {
162176function processExperimental ( buildDir , version ) {
163177 if ( fs . existsSync ( buildDir + '/node_modules' ) ) {
164178 const defaultVersionIfNotFound =
165- '0.0.0' + '-' + ' experimental' + ' -' + sha + '-' + dateString ;
179+ '0.0.0' + '-experimental-' + sha + '-' + dateString ;
166180 const versionsMap = new Map ( ) ;
167181 for ( const moduleName in stablePackages ) {
168182 versionsMap . set ( moduleName , defaultVersionIfNotFound ) ;
@@ -177,6 +191,13 @@ function processExperimental(buildDir, version) {
177191 true
178192 ) ;
179193 fs . renameSync ( buildDir + '/node_modules' , buildDir + '/oss-experimental' ) ;
194+ updatePlaceholderReactVersionInCompiledArtifacts (
195+ buildDir + '/oss-experimental' ,
196+ // TODO: The npm version for experimental releases does not include the
197+ // React version, but the runtime version does so that DevTools can do
198+ // feature detection. Decide what to do about this later.
199+ ReactVersion + '-experimental-' + sha + '-' + dateString
200+ ) ;
180201 }
181202
182203 if ( fs . existsSync ( buildDir + '/facebook-www' ) ) {
@@ -187,6 +208,10 @@ function processExperimental(buildDir, version) {
187208 fs . renameSync ( filePath , filePath . replace ( '.js' , '.modern.js' ) ) ;
188209 }
189210 }
211+ updatePlaceholderReactVersionInCompiledArtifacts (
212+ buildDir + '/facebook-www' ,
213+ ReactVersion + '-www-modern-' + sha + '-' + dateString
214+ ) ;
190215 }
191216
192217 if ( fs . existsSync ( buildDir + '/sizes' ) ) {
@@ -278,14 +303,32 @@ function updatePackageVersions(
278303 }
279304}
280305
281- function updateTheReactVersionThatDevToolsReads ( version ) {
282- // Overwrite the ReactVersion module before the build script runs so that it
283- // is included in the final bundles. This only runs in CI, so it's fine to
284- // edit the source file.
285- fs . writeFileSync (
286- './packages/shared/ReactVersion.js' ,
287- `export default '${ version } ';\n`
288- ) ;
306+ function updatePlaceholderReactVersionInCompiledArtifacts (
307+ artifactsDirectory ,
308+ newVersion
309+ ) {
310+ // Update the version of React in the compiled artifacts by searching for
311+ // the placeholder string and replacing it with a new one.
312+ const artifactFilenames = String (
313+ spawnSync ( 'grep' , [
314+ '-lr' ,
315+ PLACEHOLDER_REACT_VERSION ,
316+ '--' ,
317+ artifactsDirectory ,
318+ ] ) . stdout
319+ )
320+ . trim ( )
321+ . split ( '\n' )
322+ . filter ( filename => filename . endsWith ( '.js' ) ) ;
323+
324+ for ( const artifactFilename of artifactFilenames ) {
325+ const originalText = fs . readFileSync ( artifactFilename , 'utf8' ) ;
326+ const replacedText = originalText . replace (
327+ PLACEHOLDER_REACT_VERSION ,
328+ newVersion
329+ ) ;
330+ fs . writeFileSync ( artifactFilename , replacedText ) ;
331+ }
289332}
290333
291334/**
0 commit comments