Skip to content

Commit 3cad3a5

Browse files
authored
Use content hash for facebook-www builds (#26331)
Currently, any commit to React causes an internal sync since the Git commit hash is part of the build. This creates a lot more sync commits and noise than necessary, see: https://github.com/facebook/react/commits/builds/facebook-www This PR changes the version string to be a hash of the target build files instead. This way we get a new version with any change that actually impacts the generated files and still have a matching version across the files.
1 parent ba353a5 commit 3cad3a5

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

.github/workflows/commit_artifacts.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,9 @@ jobs:
123123
./compiled/babel-plugin-react-refresh/index.js
124124
125125
ls -R ./compiled
126-
- name: Add REVISION files
126+
- name: Add REVISION file
127127
run: |
128128
echo ${{ github.sha }} >> ./compiled/facebook-www/REVISION
129-
cp ./compiled/facebook-www/REVISION ./compiled/facebook-www/REVISION_TRANSFORMS
130129
- uses: actions/upload-artifact@v3
131130
with:
132131
name: compiled
@@ -146,7 +145,16 @@ jobs:
146145
name: compiled
147146
path: compiled/
148147
- run: git status -u
148+
- name: Check if only the REVISION file has changed
149+
id: check_should_commit
150+
run: |
151+
if git status --porcelain | grep -qv '/REVISION$'; then
152+
echo "should_commit=true" >> "$GITHUB_OUTPUT"
153+
else
154+
echo "should_commit=false" >> "$GITHUB_OUTPUT"
155+
fi
149156
- name: Commit changes to branch
157+
if: steps.check_should_commit.outputs.should_commit == 'true'
150158
uses: stefanzweifel/git-auto-commit-action@v4
151159
with:
152160
commit_message: |

scripts/rollup/build-all-release-channels.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/* eslint-disable no-for-of-loops/no-for-of-loops */
44

5+
const crypto = require('node:crypto');
56
const fs = require('fs');
67
const fse = require('fs-extra');
78
const {spawnSync} = require('child_process');
@@ -40,10 +41,7 @@ if (dateString.startsWith("'")) {
4041

4142
// Build the artifacts using a placeholder React version. We'll then do a string
4243
// 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;
44+
const PLACEHOLDER_REACT_VERSION = ReactVersion + '-PLACEHOLDER';
4745

4846
// TODO: We should inject the React version using a build-time parameter
4947
// instead of overwriting the source files.
@@ -164,19 +162,27 @@ function processStable(buildDir) {
164162
}
165163

166164
if (fs.existsSync(buildDir + '/facebook-www')) {
167-
for (const fileName of fs.readdirSync(buildDir + '/facebook-www')) {
165+
const hash = crypto.createHash('sha1');
166+
for (const fileName of fs.readdirSync(buildDir + '/facebook-www').sort()) {
168167
const filePath = buildDir + '/facebook-www/' + fileName;
169168
const stats = fs.statSync(filePath);
170169
if (!stats.isDirectory()) {
170+
hash.update(fs.readFileSync(filePath));
171171
fs.renameSync(filePath, filePath.replace('.js', '.classic.js'));
172172
}
173173
}
174174
updatePlaceholderReactVersionInCompiledArtifacts(
175175
buildDir + '/facebook-www',
176-
ReactVersion + '-www-classic-' + sha + '-' + dateString
176+
ReactVersion + '-www-classic-' + hash.digest('hex').substr(0, 8)
177177
);
178178
}
179179

180+
// Update remaining placeholders with next channel version
181+
updatePlaceholderReactVersionInCompiledArtifacts(
182+
buildDir,
183+
ReactVersion + '-' + nextChannelLabel + '-' + sha + '-' + dateString
184+
);
185+
180186
if (fs.existsSync(buildDir + '/sizes')) {
181187
fs.renameSync(buildDir + '/sizes', buildDir + '/sizes-stable');
182188
}
@@ -210,19 +216,27 @@ function processExperimental(buildDir, version) {
210216
}
211217

212218
if (fs.existsSync(buildDir + '/facebook-www')) {
213-
for (const fileName of fs.readdirSync(buildDir + '/facebook-www')) {
219+
const hash = crypto.createHash('sha1');
220+
for (const fileName of fs.readdirSync(buildDir + '/facebook-www').sort()) {
214221
const filePath = buildDir + '/facebook-www/' + fileName;
215222
const stats = fs.statSync(filePath);
216223
if (!stats.isDirectory()) {
224+
hash.update(fs.readFileSync(filePath));
217225
fs.renameSync(filePath, filePath.replace('.js', '.modern.js'));
218226
}
219227
}
220228
updatePlaceholderReactVersionInCompiledArtifacts(
221229
buildDir + '/facebook-www',
222-
ReactVersion + '-www-modern-' + sha + '-' + dateString
230+
ReactVersion + '-www-modern-' + hash.digest('hex').substr(0, 8)
223231
);
224232
}
225233

234+
// Update remaining placeholders with next channel version
235+
updatePlaceholderReactVersionInCompiledArtifacts(
236+
buildDir,
237+
ReactVersion + '-' + nextChannelLabel + '-' + sha + '-' + dateString
238+
);
239+
226240
if (fs.existsSync(buildDir + '/sizes')) {
227241
fs.renameSync(buildDir + '/sizes', buildDir + '/sizes-experimental');
228242
}

0 commit comments

Comments
 (0)