Skip to content

Commit

Permalink
Merge pull request #5 from ndaidong/dev
Browse files Browse the repository at this point in the history
v0.0.4 - Improve script tags re-generator
  • Loading branch information
ndaidong committed Oct 25, 2022
2 parents 9c3cab9 + b7d1ea8 commit 5340311
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.0.3",
"version": "0.0.4",
"name": "webasset",
"description": "To handle nunjucks template, javascript & css for web",
"repository": {
Expand All @@ -14,6 +14,7 @@
"type": "module",
"scripts": {
"lint": "standard .",
"lint:fix": "standard --fix",
"pretest": "npm run lint",
"test": "NODE_OPTIONS=--experimental-vm-modules jest --verbose --coverage=true"
},
Expand All @@ -29,7 +30,6 @@
"esbuild": "^0.15.12",
"html-minifier-terser": "7.0.0",
"linkedom": "^0.14.19",
"node-schedule": "^2.1.0",
"nonalog": "^1.0.1",
"nunjucks": "^3.2.3",
"postcss": "^8.4.18",
Expand All @@ -38,7 +38,7 @@
"strip-css-comments": "^5.0.0"
},
"devDependencies": {
"jest": "^29.2.1",
"jest": "^29.2.2",
"standard": "^17.0.0"
}
}
23 changes: 19 additions & 4 deletions utils/htmlify.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,33 @@ const normalize = (html, revision = '') => {
const jsLinks = []
document.querySelectorAll('script').forEach((elm) => {
const href = elm.getAttribute('src') || ''
const type = elm.getAttribute('type') || ''
const defer = elm.getAttribute('defer') || ''
const xasync = elm.getAttribute('async') || ''
if (href && !isAbsoluteURL(href)) {
jsLinks.push(href)
jsLinks.push({
href,
type,
defer,
xasync
})
elm.parentNode.removeChild(elm)
}
})
const body = document.querySelector('body')
jsLinks.forEach((href) => {
jsLinks.forEach(({ href, type, defer, xasync }) => {
const fpath = href + '?rev=' + revision
const scriptTag = document.createElement('script')
scriptTag.setAttribute('async')
scriptTag.setAttribute('src', fpath)
scriptTag.setAttribute('type', 'module')
if (type) {
scriptTag.setAttribute('type', type)
}
if (xasync) {
scriptTag.setAttribute('async')
}
if (defer) {
scriptTag.setAttribute('defer')
}
body.appendChild(scriptTag)
})
const output = Array.from(document.children).map(it => it.outerHTML).join('')
Expand Down

0 comments on commit 5340311

Please sign in to comment.