Skip to content

Commit

Permalink
Create JS resource with unique ID (#62)
Browse files Browse the repository at this point in the history
* Do not emit a source map

* Create JS resource with unique ID

* Update automatically the page template

* Review template generation

* Clean up and refactor

- move static directory inside browser module
- update tests
  • Loading branch information
hvelarde authored Sep 6, 2018
1 parent 00fb475 commit 43c36fc
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 21 deletions.
4 changes: 0 additions & 4 deletions src/collective/lazysizes/browser/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# -*- coding: utf-8 -*-
from collective.lazysizes.config import JS
from plone.app.layout.viewlets import ViewletBase


class ResourcesViewlet(ViewletBase):
"""This viewlet inserts static resources on page header."""

def js(self):
return self.site_url + '/' + JS
8 changes: 7 additions & 1 deletion src/collective/lazysizes/browser/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
xmlns:browser="http://namespaces.zope.org/browser"
i18n_domain="collective.lazysizes">

<browser:resourceDirectory
name="collective.lazysizes"
directory="static"
layer="collective.lazysizes.interfaces.ILazySizesLayer"
/>

<browser:viewlet
name="collective.lazysizes.resources"
manager="plone.app.layout.viewlets.interfaces.IHtmlHead"
class=".ResourcesViewlet"
template="resources.pt"
template="static/resources.pt"
permission="zope2.Public"
/>

Expand Down
1 change: 0 additions & 1 deletion src/collective/lazysizes/browser/resources.pt

This file was deleted.

File renamed without changes.
2 changes: 0 additions & 2 deletions src/collective/lazysizes/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

PROJECTNAME = 'collective.lazysizes'

JS = '++resource++collective.lazysizes/lazysizes.js'

IS_PLONE_5 = api.env.plone_version().startswith('5')
6 changes: 0 additions & 6 deletions src/collective/lazysizes/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@
permission="collective.lazysizes.Setup"
/>

<browser:resourceDirectory
name="collective.lazysizes"
directory="static"
layer="collective.lazysizes.interfaces.ILazySizesLayer"
/>

<adapter
for="* .interfaces.ILazySizesLayer"
name="collective.lazysizes"
Expand Down
8 changes: 4 additions & 4 deletions src/collective/lazysizes/tests/test_resources_viewlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ def get_viewlet(self, context, name='collective.lazysizes.resources'):
return viewlet[0]

def test_viewlet(self):
from collective.lazysizes.config import JS
html = etree.HTML(self.viewlet())
self.assertIn('async', html.xpath('//script')[0].attrib)
self.assertTrue(
html.xpath('//script')[0].attrib['src'],
'http://nohost/plone/' + JS)
# script name must include the hash of latest git commit
regexp = r'lazysizes-[\da-f]{7}\.js$'
self.assertRegexpMatches(
html.xpath('//script')[0].attrib['src'], regexp)
3 changes: 3 additions & 0 deletions webpack/app/resources.pt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<% for (let js in htmlWebpackPlugin.files.js) { %>
<script async tal:attributes="src <%= 'string:${view/site_url}/' + htmlWebpackPlugin.files.js[js] %>" />
<% } %>
23 changes: 20 additions & 3 deletions webpack/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
const HtmlWebpackPlugin = require('html-webpack-plugin')

// get partial prefix of latest commit in webpack directory
const childProcess = require('child_process');
const gitCmd = 'git rev-list -1 HEAD --abbrev-commit $(pwd)'
const gitHash = childProcess.execSync(gitCmd).toString().substring(0, 7);

// clean up old script
const path = `${__dirname}/../src/collective/lazysizes/browser/static`
childProcess.execSync(`rm -f ${path}/lazysizes-*`);

module.exports = {
entry: [
'./app/lazysizes-icon.png',
'./app/lazysizes.js',
],
output: {
filename: 'lazysizes.js',
filename: `lazysizes-${gitHash}.js`,
library: 'lazysizes',
libraryTarget: 'umd',
path: `${__dirname}/../src/collective/lazysizes/static`,
path: path,
publicPath: '++resource++collective.lazysizes/',
},
module: {
Expand Down Expand Up @@ -46,5 +57,11 @@ module.exports = {
]
}]
},
devtool: 'source-map',
plugins: [
new HtmlWebpackPlugin({
inject: false,
filename: 'resources.pt',
template: 'app/resources.pt',
})
]
}

0 comments on commit 43c36fc

Please sign in to comment.