From ea9bc832b304567c6b935936d694da97b876bf9c Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Thu, 15 Mar 2018 22:24:30 -0700 Subject: [PATCH] Lazy load jsmediatags --- config/webpack.common.js | 1 + js/fileUtils.js | 27 ++++++++++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/config/webpack.common.js b/config/webpack.common.js index feaddb10..ac7932a7 100644 --- a/config/webpack.common.js +++ b/config/webpack.common.js @@ -74,6 +74,7 @@ module.exports = { }, output: { filename: "[name]-[hash].js", + chunkFilename: "[name]-[hash].js", publicPath: "/", path: path.resolve(__dirname, "../built") } diff --git a/js/fileUtils.js b/js/fileUtils.js index f54ea1ac..899cc2f7 100644 --- a/js/fileUtils.js +++ b/js/fileUtils.js @@ -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" + ); }); }