-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build emoji files and shortcodes map in a plugin (#2789)
* Build emoji files and shortcodes map in a plugin * update comment
- Loading branch information
1 parent
3ca0b73
commit 05ceb2a
Showing
8 changed files
with
106 additions
and
66 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -1,73 +1,107 @@ | ||
/** | ||
* Create emoji image files in public/static/emoji. | ||
* vite will copy these to the /static/emoji folder in the build output. | ||
* TODO this could maybe be a webpack plugin? we need to be able to generate | ||
* a file with shortcode to filename mappings that we can import in the web client code. | ||
*/ | ||
import crypto from 'crypto'; | ||
import fs from 'fs/promises'; | ||
import crypto from 'node:crypto'; | ||
import fs from 'node:fs/promises'; | ||
import { fileURLToPath } from 'node:url'; | ||
import assert from 'node:assert'; | ||
import pMap from 'p-map'; | ||
import serveStatic from 'serve-static'; | ||
|
||
const emojibaseShortcodesPath = new URL('../node_modules/emojibase-data/en/shortcodes/emojibase.json', import.meta.url); | ||
const emojibaseShortcodes = JSON.parse(await fs.readFile(emojibaseShortcodesPath, { encoding: 'utf8' })); | ||
|
||
const joypixelShortcodesPath = new URL('../node_modules/emojibase-data/en/shortcodes/joypixels.json', import.meta.url); | ||
const joypixelShortcodes = JSON.parse(await fs.readFile(joypixelShortcodesPath, { encoding: 'utf8' })); | ||
|
||
const twemojiDir = new URL('../node_modules/twemoji-emojis/vendor/svg/', import.meta.url); | ||
const twemojis = await fs.readdir(twemojiDir); | ||
const twemojiNames = twemojis.map((basename) => basename.replace(/\.\w+$/, '')); | ||
|
||
const outputDir = new URL('../public/static/emoji/', import.meta.url); | ||
/** | ||
* Collect twemoji images, determine their shortcodes, build a map of shortcode to file names. | ||
* | ||
* Images are output in the build. | ||
* Joypixels shortcodes are used if available: this is for backwards compatibility. Emojibase shortcodes | ||
* are used for emoji that do not have a Joypixel shortcode. | ||
* The mapping of shortcodes to file names can be imported by JS as `virtual:emoji-shortcodes`. | ||
*/ | ||
export default function emojiPlugin () { | ||
let shortcodeToOutName; | ||
const filesToEmit = new Map(); | ||
|
||
const shortcodes = {}; | ||
function appendShortcode(hex, shortcode) { | ||
const imageName = `${hex.toLowerCase()}.svg` | ||
if (Array.isArray(shortcode)) { | ||
for (const c of shortcode) { | ||
shortcodes[c] = imageName; | ||
} | ||
} else { | ||
shortcodes[shortcode] = imageName; | ||
} | ||
} | ||
return { | ||
name: 'emoji', | ||
async configureServer (server) { | ||
server.middlewares.use(serveStatic(fileURLToPath(twemojiDir))); | ||
}, | ||
async buildStart () { | ||
const emojibaseShortcodes = JSON.parse(await fs.readFile(emojibaseShortcodesPath, { encoding: 'utf8' })); | ||
const joypixelShortcodes = JSON.parse(await fs.readFile(joypixelShortcodesPath, { encoding: 'utf8' })); | ||
|
||
// Not all emoji have a joypixel shortcode. Track which ones are left over so | ||
// we can use a different shortcode for those. | ||
const remainingTwemoji = new Set(twemojiNames); | ||
for (const [hex, shortcode] of Object.entries(joypixelShortcodes)) { | ||
if (!twemojiNames.includes(hex.toLowerCase())) { | ||
continue; | ||
} | ||
const twemojis = await fs.readdir(twemojiDir); | ||
const twemojiNames = twemojis.map((basename) => basename.replace(/\.\w+$/, '')); | ||
|
||
appendShortcode(hex, shortcode); | ||
remainingTwemoji.delete(hex.toLowerCase()); | ||
} | ||
const shortcodes = {}; | ||
function appendShortcode(hex, shortcode) { | ||
const imageName = `${hex.toLowerCase()}.svg` | ||
if (Array.isArray(shortcode)) { | ||
for (const c of shortcode) { | ||
shortcodes[c] = imageName; | ||
} | ||
} else { | ||
shortcodes[shortcode] = imageName; | ||
} | ||
} | ||
|
||
for (const hex of remainingTwemoji) { | ||
const shortcode = emojibaseShortcodes[hex.toUpperCase()]; | ||
if (!shortcode) { | ||
continue; | ||
} | ||
// Not all emoji have a joypixel shortcode. Track which ones are left over so | ||
// we can use a different shortcode for those. | ||
const remainingTwemoji = new Set(twemojiNames); | ||
for (const [hex, shortcode] of Object.entries(joypixelShortcodes)) { | ||
if (!twemojiNames.includes(hex.toLowerCase())) { | ||
continue; | ||
} | ||
|
||
appendShortcode(hex, shortcode); | ||
} | ||
appendShortcode(hex, shortcode); | ||
remainingTwemoji.delete(hex.toLowerCase()); | ||
} | ||
|
||
console.log('generating', Object.keys(shortcodes).length, 'emoji...'); | ||
for (const hex of remainingTwemoji) { | ||
const shortcode = emojibaseShortcodes[hex.toUpperCase()]; | ||
if (!shortcode) { | ||
continue; | ||
} | ||
|
||
await fs.rm(outputDir, { force: true, recursive: true }); | ||
await fs.mkdir(outputDir, { recursive: true }); | ||
const shortcodeHashes = Object.fromEntries( | ||
await pMap(Object.entries(shortcodes), async ([shortcode, filename]) => { | ||
const bytes = await fs.readFile(new URL(filename, twemojiDir)); | ||
const hash = crypto.createHash('sha1').update(bytes).digest('hex').slice(0, 7); | ||
const outName = `${hash}.svg`; | ||
await fs.writeFile(new URL(outName, outputDir), bytes); | ||
return [shortcode, outName]; | ||
}), | ||
); | ||
await fs.writeFile(new URL('../src/utils/emojiShortcodes.js', import.meta.url), ` | ||
// GENERATED FILE: run \`npm run emoji\` | ||
/* eslint-disable */ | ||
export default JSON.parse(${JSON.stringify(JSON.stringify(shortcodeHashes))}); | ||
`.trim()); | ||
appendShortcode(hex, shortcode); | ||
} | ||
|
||
if (this.meta.watchMode) { | ||
shortcodeToOutName = Object.entries(shortcodes); | ||
} else { | ||
shortcodeToOutName = await pMap(Object.entries(shortcodes), async ([shortcode, filename]) => { | ||
const bytes = await fs.readFile(new URL(filename, twemojiDir)); | ||
const hash = crypto.createHash('sha1').update(bytes).digest('hex').slice(0, 7); | ||
const outName = `${hash}.svg`; | ||
if (filesToEmit.has(outName)) { | ||
assert(filesToEmit.get(outName).equals(bytes)); | ||
} else { | ||
filesToEmit.set(outName, bytes); | ||
} | ||
return [shortcode, outName]; | ||
}); | ||
} | ||
}, | ||
async generateBundle () { | ||
for (const [outName, bytes] of filesToEmit) { | ||
this.emitFile({ | ||
type: 'asset', | ||
fileName: `static/emoji/${outName}`, | ||
source: bytes, | ||
}); | ||
} | ||
}, | ||
async resolveId (id) { | ||
if (id === 'virtual:emoji-shortcodes') { | ||
return '\0virtual:emoji-shortcodes' | ||
} | ||
}, | ||
async load (id) { | ||
if (id === '\0virtual:emoji-shortcodes') { | ||
return `export default JSON.parse(${ | ||
JSON.stringify(JSON.stringify(Object.fromEntries(shortcodeToOutName))) | ||
})`; | ||
} | ||
}, | ||
} | ||
} |
This file contains 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