local icons

Summary:
Currently icons were always fetched remotely. We used a service worker to prefetch and cache some icons, that were critical to the UI.

In this diff, we are bundling icons at build time, with the app. In utils/icons.js we still specfify the list of icons which should be bundled. These are downloaded as part of the build step and bundled with the app. We are downloading the icons in 1x and 2x (the two most common pixel densities).

Reviewed By: jknoxville

Differential Revision: D16620764

fbshipit-source-id: 965a7793ad1f08aebb292606add00218429cdaf4
This commit is contained in:
Daniel Büchele
2019-08-02 08:56:23 -07:00
committed by Facebook Github Bot
parent 1717fba410
commit 16e913a819
6 changed files with 127 additions and 178 deletions

View File

@@ -17,6 +17,8 @@ const {
getVersionNumber,
genMercurialRevision,
} = require('./build-utils.js');
const fetch = require('node-fetch');
const {ICONS, getIconURL} = require('../src/utils/icons.js');
function generateManifest(versionNumber) {
const filePath = path.join(__dirname, '..', 'dist');
@@ -112,11 +114,51 @@ function copyStaticFolder(buildFolder) {
});
}
function downloadIcons(buildFolder) {
const iconURLs = Object.entries(ICONS).reduce((acc, [name, sizes]) => {
acc.push(
// get icons in @1x and @2x
...sizes.map(size => ({name, size, density: 1})),
...sizes.map(size => ({name, size, density: 2})),
);
return acc;
}, []);
return Promise.all(
iconURLs.map(({name, size, density}) =>
fetch(getIconURL(name, size, density))
.then(res => {
if (res.status !== 200) {
throw new Error(`Could not download the icon: ${name}`);
}
return res;
})
.then(
res =>
new Promise((resolve, reject) => {
const fileStream = fs.createWriteStream(
path.join(
buildFolder,
'icons',
`${name}-${size}@${density}x.png`,
),
);
res.body.pipe(fileStream);
res.body.on('error', reject);
fileStream.on('finish', resolve);
}),
)
.catch(console.error),
),
);
}
(async () => {
const dir = await buildFolder();
// eslint-disable-next-line no-console
console.log('Created build directory', dir);
copyStaticFolder(dir);
await downloadIcons(dir);
await compileDefaultPlugins(path.join(dir, 'defaultPlugins'));
await compile(dir, path.join(__dirname, '..', 'src', 'init.js'));
const versionNumber = getVersionNumber();