twemoji/scripts/create-dist

71 lines
2.1 KiB
JavaScript
Executable File

#!/usr/bin/env node
/*! Copyright Twitter Inc. and other contributors. Licensed under MIT *//*
https://github.com/jdecked/twemoji/blob/gh-pages/LICENSE
*/
const fs = require('fs-extra');
const path = require('path');
const { spawnSync } = require('child_process');
const { getIntegrityHash } = require('./utils');
function file(...which) {
return path.join(__dirname, '..', ...which);
}
function distFile(...which) {
return path.join(__dirname, '..', 'dist', ...which);
}
fs.writeFileSync(
distFile('twemoji.npm.js'),
[
'var location = global.location || {};',
fs.readFileSync(distFile('twemoji.js')),
'if (!location.protocol) {',
' twemoji.base = twemoji.base.replace(/^http:/, "");',
'}',
'module.exports = twemoji;'
].join('\n')
);
fs.writeFileSync(
distFile('twemoji.amd.js'),
'define(function () {\n' +
fs.readFileSync(distFile('twemoji.js')).toString().replace(
/^(.)/gm, ' $1'
) +
'\n return twemoji;\n});'
);
spawnSync(
'yarn',
[
'uglifyjs',
'--verbose',
distFile('twemoji.js'),
'-o',
distFile('twemoji.tmp.js')
]
);
const copyright = '/*! Copyright Twitter Inc. and other contributors. Licensed under MIT */';
const minifiedContents = fs.readFileSync(distFile('twemoji.tmp.js'));
fs.unlinkSync(distFile('twemoji.tmp.js'));
fs.writeFileSync(distFile('twemoji.min.js'), `${copyright}\n${minifiedContents}`);
fs.writeFileSync(distFile('twemoji.esm.js'), `${copyright}\n${minifiedContents}\nexport default twemoji;`);
const integrityHash = getIntegrityHash(distFile('twemoji.min.js'));
const { version } = require('../package.json');
function updateSriScriptReference(filename) {
const newScript = `<script src="https://cdn.jsdelivr.net/npm/@twemoji/api@${version}/dist/twemoji.min.js" integrity="${integrityHash}"`;
fs.writeFileSync(filename, fs.readFileSync(filename).toString('utf8').replace(/<script src="[^ ]*" integrity="[^ ]*"/, newScript));
}
updateSriScriptReference(file('README.md'));
// Copy the png assets and svgs to the dist folder
fs.copySync(file('assets/svg'), distFile('svg'));
fs.copySync(file('assets/72x72'), distFile('72x72'));