Skip to content

Commit

Permalink
webpack: disable vendor chunk
Browse files Browse the repository at this point in the history
* Removes the vendor chunk group.
* Adds the "UniqueJinjaManifestLoader" to avoid rendering dubplicate
  chunks tags.
  • Loading branch information
slint committed May 11, 2020
1 parent cbc0849 commit 8bdecd0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
7 changes: 0 additions & 7 deletions invenio_assets/assets/build/webpack.config.js
Expand Up @@ -83,13 +83,6 @@ var webpackConfig = {
})
],
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: "vendor",
chunks: "initial"
}
},
chunks: "all"
},
// Extract webpack runtime and module manifest to its own file in order to
Expand Down
5 changes: 5 additions & 0 deletions invenio_assets/ext.py
Expand Up @@ -18,6 +18,7 @@
from flask_webpackext import FlaskWebpackExt

from .collect import collect_staticroot_removal
from .webpack import UniqueJinjaManifestLoader

__all__ = ('InvenioAssets', )

Expand Down Expand Up @@ -69,8 +70,12 @@ def init_config(self, app):
app.config.setdefault('COLLECT_STORAGE', 'flask_collect.storage.link')
app.config.setdefault(
'COLLECT_FILTER', partial(collect_staticroot_removal, app))

# Flask-WebpackExt config
app.config.setdefault(
'WEBPACKEXT_PROJECT', 'invenio_assets.webpack:project')
app.config.setdefault(
'WEBPACKEXT_MANIFEST_LOADER', UniqueJinjaManifestLoader)
if app.debug: # for development use 2-level deep symlinking
from pywebpack.storage import LinkStorage
app.config.setdefault(
Expand Down
49 changes: 49 additions & 0 deletions invenio_assets/webpack.py
Expand Up @@ -11,6 +11,14 @@
from __future__ import absolute_import, print_function

import os
from collections import OrderedDict

from flask import current_app, request
from flask_webpackext import WebpackBundle, WebpackBundleProject
from flask_webpackext.manifest import JinjaManifest, JinjaManifestLoader
from markupsafe import Markup
from pywebpack import ManifestEntry, UnsupportedExtensionError, \
bundles_from_entry_point

project = WebpackBundleProject(
__name__,
Expand Down Expand Up @@ -62,3 +70,44 @@ def dependencies(self):
"""Proxy to the active theme's bundle dependencies."""
return self._active_theme_bundle.dependencies


class UniqueJinjaManifestEntry(ManifestEntry):
"""Manifest entry which avoids double output of chunks."""

def __html__(self):
"""Output chunk HTML tags that haven't been yet output."""
if not hasattr(request, '_jinja_webpack_entries'):
setattr(request, '_jinja_webpack_entries', {
'.js': OrderedDict(),
'.css': OrderedDict(),
})

output = []

# For debugging add from which entry the chunk came
if current_app.debug:
output.append('<!-- {} -->'.format(self.name))
for p in self._paths:
_, ext = os.path.splitext(p.lower())
# If we haven't come across the chunk yet, we add it to the output
if p not in request._jinja_webpack_entries[ext]:
tpl = self.templates.get(ext)
if tpl is None:
raise UnsupportedExtensionError(p)
output.append(tpl.format(p))

# Mark the we have already output the chunk
request._jinja_webpack_entries[ext][p] = None
return Markup('\n'.join(output))


class UniqueJinjaManifestLoader(JinjaManifestLoader):
"""Factory which uses the Jinja manifest entry."""

def __init__(self, manifest_cls=JinjaManifest,
entry_cls=UniqueJinjaManifestEntry):
"""Initialize manifest loader."""
super(UniqueJinjaManifestLoader, self).__init__(
manifest_cls=manifest_cls,
entry_cls=entry_cls
)

0 comments on commit 8bdecd0

Please sign in to comment.