Skip to content

🎥 Make videos programmatically with React

Docs: Prevent content hash bundle corruption

Igor Samokhovets4effd7834113
diff --git a/packages/docs/build-docs.ts b/packages/docs/build-docs.ts
index 4f20a7805c..0b70bce0d0 100644
--- a/packages/docs/build-docs.ts
+++ b/packages/docs/build-docs.ts
@@ -143,6 +143,9 @@ await run(
docusaurusBuild.args,
docusaurusEnv,
);
+await run('validate generated JavaScript', 'bun', [
+ 'validate-built-javascript.ts',
+]);
await run('build standalone Browser Studio', 'bun', [
'build-browser-studio-standalone.ts',
]);
diff --git a/packages/docs/docusaurus.config.ts b/packages/docs/docusaurus.config.ts
index ed7cad66d1..2ad87f1c7c 100644
--- a/packages/docs/docusaurus.config.ts
+++ b/packages/docs/docusaurus.config.ts
@@ -1,5 +1,6 @@
import type {Config} from '@docusaurus/types';
import elementSourceDependencies from './plugins/element-source-dependencies.js';
+import longContentHashes from './plugins/long-content-hashes.js';
import remarkElementSource from './plugins/remark-element-source.js';
import remarkExportRaw from './plugins/remark-export-raw.js';
import {elementRegistry} from './src/components/Elements/element-registry';
@@ -348,6 +349,7 @@ const config: Config = {
],
plugins: [
elementSourceDependencies,
+ longContentHashes,
[
'@docusaurus/plugin-content-docs',
{
diff --git a/packages/docs/plugins/long-content-hashes.js b/packages/docs/plugins/long-content-hashes.js
new file mode 100644
index 0000000000..1a1b89fb2a
--- /dev/null
+++ b/packages/docs/plugins/long-content-hashes.js
@@ -0,0 +1,39 @@
+// Rspack's RealContentHashPlugin globally replaces preliminary hashes in all
+// assets. An eight-character hash can occur in source data and corrupt a
+// bundle: https://github.com/web-infra-dev/rspack/issues/8474
+const shortContentHash = '[contenthash:8]';
+const longContentHash = '[contenthash:12]';
+
+export default function longContentHashes() {
+ return {
+ name: 'long-content-hashes',
+ configureWebpack(config, isServer) {
+ if (isServer || config.mode !== 'production') {
+ return {};
+ }
+
+ for (const key of ['filename', 'chunkFilename']) {
+ const value = config.output?.[key];
+ if (typeof value === 'string') {
+ config.output[key] = value.replace(shortContentHash, longContentHash);
+ }
+ }
+
+ for (const plugin of config.plugins ?? []) {
+ const options = plugin?.options;
+ if (!options) {
+ continue;
+ }
+
+ for (const key of ['filename', 'chunkFilename']) {
+ const value = options[key];
+ if (typeof value === 'string') {
+ options[key] = value.replace(shortContentHash, longContentHash);
+ }
+ }
+ }
+
+ return {};
+ },
+ };
+}
diff --git a/packages/docs/validate-built-javascript.ts b/packages/docs/validate-built-javascript.ts
new file mode 100644
index 0000000000..9896cba2f3
--- /dev/null
+++ b/packages/docs/validate-built-javascript.ts
@@ -0,0 +1,23 @@
+import path from 'path';
+
+const buildDirectory = path.join(import.meta.dir, 'build');
+const transpiler = new Bun.Transpiler({loader: 'js'});
+let filesChecked = 0;
+
+for await (const relativePath of new Bun.Glob('**/*.js').scan({
+ cwd: buildDirectory,
+ onlyFiles: true,
+})) {
+ const file = path.join(buildDirectory, relativePath);
+ try {
+ transpiler.scan(await Bun.file(file).text());
+ } catch (error) {
+ throw new Error(`Generated invalid JavaScript in ${relativePath}`, {
+ cause: error,
+ });
+ }
+
+ filesChecked++;
+}
+
+console.log(`Validated ${filesChecked} generated JavaScript files.`);