1
0
mirror of https://github.com/twitter/twemoji.git synced 2024-12-28 15:19:10 +00:00
twemoji/2/scripts/create-dist

73 lines
2.1 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env node
/*! Copyright Twitter Inc. and other contributors. Licensed under MIT *//*
https://github.com/twitter/twemoji/blob/gh-pages/LICENSE
*/
const fs = require('fs');
const path = require('path');
const { spawnSync } = require('child_process');
function file(...which) {
return path.join(__dirname, '..', ...which);
}
fs.writeFileSync(
file('twemoji.npm.js'),
[
'var location = global.location || {};',
fs.readFileSync(file('twemoji.js')),
'if (!location.protocol) {',
' twemoji.base = twemoji.base.replace(/^http:/, "");',
'}',
'module.exports = twemoji;'
].join('\n')
);
fs.writeFileSync(
file('twemoji.amd.js'),
'define(function () {\n' +
fs.readFileSync(file('twemoji.js')).toString().replace(
/^(.)/gm, ' $1'
) +
'\n return twemoji;\n});'
);
spawnSync(
'yarnpkg',
[
'uglifyjs',
'--verbose',
file('twemoji.js'),
'-o',
file('twemoji.tmp.js')
]
);
2019-05-29 20:43:48 +00:00
const copyright = '/*! Copyright Twitter Inc. and other contributors. Licensed under MIT */';
const minifiedContents = fs.readFileSync(file('twemoji.tmp.js'));
fs.unlinkSync(file('twemoji.tmp.js'));
2019-05-29 20:43:48 +00:00
fs.writeFileSync(file('twemoji.min.js'), `${copyright}\n${minifiedContents}`);
fs.writeFileSync(file('twemoji.esm.js'), `${copyright}\n${minifiedContents}\nexport default twemoji;`);
const algorithm = 'sha384';
const digest = spawnSync('openssl', ['dgst', `-${algorithm}`, '-binary', file('twemoji.min.js')]);
if (digest.status || digest.signal){
throw new Error(digest.stderr.toString('utf8'));
}
const integrityHash = digest.stdout.toString('base64');
const integrity = ``;
const { version } = require('../../package.json');
const src = ``
function updateScript(filename) {
const newScript = `<script src="https://twemoji.maxcdn.com/2/twemoji.min.js?${version}" integrity="${algorithm}-${integrityHash}"`
fs.writeFileSync(
filename,
fs.readFileSync(filename).toString('utf8').replace(/<script src="[^ ]*" integrity="[^ ]*"/, newScript));
}
updateScript(file('..', 'README.md'));
updateScript(file('templates', 'preview.html'));