From 7151015a49063a694df6563a7bce4ac3c10b45d2 Mon Sep 17 00:00:00 2001 From: Clemens Wolff Date: Fri, 24 May 2019 10:07:37 -0400 Subject: [PATCH] Exclude unused hljs languages from bundle --- app/server/static/components/hljsLanguages.js | 1 + app/server/static/components/uploadMixin.js | 10 +++++++++- app/server/static/webpack.config.js | 6 ++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 app/server/static/components/hljsLanguages.js diff --git a/app/server/static/components/hljsLanguages.js b/app/server/static/components/hljsLanguages.js new file mode 100644 index 00000000..dd98ba87 --- /dev/null +++ b/app/server/static/components/hljsLanguages.js @@ -0,0 +1 @@ +module.exports = ['json']; diff --git a/app/server/static/components/uploadMixin.js b/app/server/static/components/uploadMixin.js index 1ccb6156..d0af074e 100644 --- a/app/server/static/components/uploadMixin.js +++ b/app/server/static/components/uploadMixin.js @@ -1,7 +1,15 @@ -import hljs from 'highlight.js'; +import hljs from 'highlight.js/lib/highlight'; +import hljsLanguages from './hljsLanguages'; import HTTP, { newHttpClient } from './http'; import Messages from './messages.vue'; +hljsLanguages.forEach((languageName) => { + /* eslint-disable import/no-dynamic-require, global-require */ + const languageModule = require(`highlight.js/lib/languages/${languageName}`); + /* eslint-enable import/no-dynamic-require, global-require */ + hljs.registerLanguage(languageName, languageModule); +}); + export default { components: { Messages }, diff --git a/app/server/static/webpack.config.js b/app/server/static/webpack.config.js index fbf636bd..d6d41bad 100644 --- a/app/server/static/webpack.config.js +++ b/app/server/static/webpack.config.js @@ -3,6 +3,8 @@ const path = require('path'); const process = require('process'); const BundleTracker = require('webpack-bundle-tracker'); const VueLoaderPlugin = require('vue-loader/lib/plugin') +const { ContextReplacementPlugin } = require('webpack'); +const hljsLanguages = require('./components/hljsLanguages'); const devMode = process.env.DEBUG !== 'False'; const hotReload = process.env.HOT_RELOAD === '1'; @@ -58,6 +60,10 @@ module.exports = { ] }, plugins: [ + new ContextReplacementPlugin( + /highlight\.js\/lib\/languages$/, + new RegExp(`^./(${hljsLanguages.join('|')})$`) + ), new BundleTracker({ filename: './webpack-stats.json' }), new VueLoaderPlugin() ],