Lazy load jsmediatags

This commit is contained in:
Jordan Eldredge 2018-03-15 22:24:30 -07:00
parent 5ce33768ec
commit ea9bc832b3
2 changed files with 19 additions and 9 deletions

View file

@ -74,6 +74,7 @@ module.exports = {
},
output: {
filename: "[name]-[hash].js",
chunkFilename: "[name]-[hash].js",
publicPath: "/",
path: path.resolve(__dirname, "../built")
}

View file

@ -1,5 +1,4 @@
import invariant from "invariant";
import jsmediatags from "jsmediatags/dist/jsmediatags";
export function genMediaTags(file) {
invariant(
@ -11,14 +10,24 @@ export function genMediaTags(file) {
file = `${location.protocol}//${location.host}${location.pathname}${file}`;
}
return new Promise((resolve, reject) => {
try {
jsmediatags.read(file, { onSuccess: resolve, onError: reject });
} catch (e) {
// Possibly jsmediatags could not find a parser for this file?
// Nothing to do.
// Consider removing this after https://github.com/aadsm/jsmediatags/issues/83 is resolved.
reject(e);
}
require.ensure(
["jsmediatags/dist/jsmediatags"],
require => {
const jsmediatags = require("jsmediatags/dist/jsmediatags");
try {
jsmediatags.read(file, { onSuccess: resolve, onError: reject });
} catch (e) {
// Possibly jsmediatags could not find a parser for this file?
// Nothing to do.
// Consider removing this after https://github.com/aadsm/jsmediatags/issues/83 is resolved.
reject(e);
}
},
() => {
// The dependency failed to load
},
"jsmediatags"
);
});
}