-
Notifications
You must be signed in to change notification settings - Fork 1
Stop shipping six copies of static/ in every deployment #180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // Docusaurus copies the whole static/ folder into every locale build | ||
| // (build/ru/, build/ua/, ...), so each deployment carried six copies of the | ||
| // ~340 MB utility apps. This removes the locale copies; vercel.json rewrites | ||
| // /<locale>/<dir>/... back to the root copy. Rewrites only apply when no file | ||
| // matches, so generated locale pages under the same prefixes still win. | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| const ROOT = path.join(__dirname, '..'); | ||
| const STATIC_DIR = path.join(ROOT, 'static'); | ||
| const BUILD_DIR = path.join(ROOT, 'build'); | ||
| const LOCALES = ['ru', 'ua', 'de', 'es', 'et']; | ||
| // Tiny, and utilities/ shares its prefix with generated /utilities/ pages; | ||
| // not worth a rewrite. | ||
| const KEEP = new Set(['utilities', '.nojekyll']); | ||
|
|
||
| function rewrittenEntries() { | ||
| const config = JSON.parse(fs.readFileSync(path.join(ROOT, 'vercel.json'), 'utf8')); | ||
| const rule = (config.rewrites || []).find((r) => r.source.includes(':asset((?:')); | ||
| if (!rule) throw new Error('vercel.json has no locale static rewrite with an :asset((?:...)) group'); | ||
| return new Set(rule.source.match(/:asset\(\(\?:([^)]+)\)/)[1].replace(/\\\./g, '.').split('|')); | ||
| } | ||
|
|
||
| function walk(dir, rel = '') { | ||
| const out = []; | ||
| for (const entry of fs.readdirSync(path.join(dir, rel), {withFileTypes: true})) { | ||
| const child = path.join(rel, entry.name); | ||
| if (entry.isDirectory()) out.push(...walk(dir, child)); | ||
| else out.push(child); | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| function removeEmptyDirs(dir) { | ||
| if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) return; | ||
| for (const entry of fs.readdirSync(dir)) removeEmptyDirs(path.join(dir, entry)); | ||
| if (fs.readdirSync(dir).length === 0) fs.rmdirSync(dir); | ||
| } | ||
|
|
||
| const rewritten = rewrittenEntries(); | ||
| const topLevel = fs.readdirSync(STATIC_DIR).filter((name) => !KEEP.has(name)); | ||
| const uncovered = topLevel.filter((name) => !rewritten.has(name)); | ||
| if (uncovered.length) { | ||
| throw new Error( | ||
| `static/ entries not covered by the locale rewrite in vercel.json: ${uncovered.join(', ')}. ` + | ||
| 'Add them to the :asset((?:...)) group or to KEEP in scripts/dedupe-locale-static.js.', | ||
| ); | ||
| } | ||
|
|
||
| let removed = 0; | ||
| let bytes = 0; | ||
| for (const name of topLevel) { | ||
| const src = path.join(STATIC_DIR, name); | ||
| const files = fs.statSync(src).isDirectory() ? walk(src).map((f) => path.join(name, f)) : [name]; | ||
| for (const locale of LOCALES) { | ||
| for (const rel of files) { | ||
| const copy = path.join(BUILD_DIR, locale, rel); | ||
| if (!fs.existsSync(copy)) continue; | ||
| const {size} = fs.statSync(copy); | ||
| if (size !== fs.statSync(path.join(STATIC_DIR, rel)).size) continue; | ||
| fs.unlinkSync(copy); | ||
| removed += 1; | ||
| bytes += size; | ||
| } | ||
| removeEmptyDirs(path.join(BUILD_DIR, locale, name)); | ||
| } | ||
| } | ||
|
|
||
| console.log(`Removed ${removed} duplicated static files from locale builds (${(bytes / 1024 / 1024).toFixed(1)} MB).`); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.