-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: rewrite most of the module into "once" module
- Loading branch information
Showing
11 changed files
with
230 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
lib/modules/example.es6 | ||
lib/modules/example_once.es6 | ||
test.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* Example "once" module | ||
*/ | ||
|
||
// It is a "once" module | ||
const once = true; | ||
|
||
/** | ||
* Modify attributes of node. Optional. | ||
* | ||
* @param {object} options - Options that were passed to htmlnano | ||
* @param moduleOptions — Module options. For most modules this is just "true" (indication that the module was enabled) | ||
*/ | ||
function onattrs(options, moduleOptions) { | ||
return (attrs, node) => { | ||
// You can modify "attrs" based on "node" | ||
|
||
return attrs; // ... then return the modified attrs | ||
}; | ||
} | ||
|
||
/** | ||
* Modify content of node. Optional. | ||
* | ||
* @param {object} options - Options that were passed to htmlnano | ||
* @param moduleOptions — Module options. For most modules this is just "true" (indication that the module was enabled) | ||
*/ | ||
function oncontent(options, moduleOptions) { | ||
return (content, node) => { | ||
// Same goes the "content" | ||
|
||
return content; // ... return modified content here | ||
}; | ||
} | ||
|
||
/** | ||
* It is possible to modify entire ndde as well. Optional. | ||
* @param {object} options - Options that were passed to htmlnano | ||
* @param moduleOptions — Module options. For most modules this is just "true" (indication that the module was enabled) | ||
*/ | ||
function onnode(options, moduleOptions) { | ||
return (node) => { | ||
return node; // ... return new node here | ||
}; | ||
} | ||
|
||
export { | ||
// This marks the module is a "once" module | ||
once, | ||
onattrs, | ||
oncontent, | ||
onnode | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,23 @@ | ||
/* Minify JSON inside <script> tags */ | ||
export default function minifyJson(tree) { | ||
// Match all <script> tags which have JSON mime type | ||
tree.match({tag: 'script', attrs: {type: /(\/|\+)json/}}, node => { | ||
let content = (node.content || []).join(''); | ||
if (! content) { | ||
return node; | ||
} | ||
const rNodeAttrsTypeJson = /(\/|\+)json/; | ||
|
||
try { | ||
content = JSON.stringify(JSON.parse(content)); | ||
} catch (error) { | ||
return node; | ||
} | ||
const once = true; | ||
|
||
node.content = [content]; | ||
return node; | ||
}); | ||
function oncontent() { | ||
return (content, node) => { | ||
if (node.attrs && node.attrs.type && rNodeAttrsTypeJson.test(node.attrs.type)) { | ||
try { | ||
content = JSON.stringify(JSON.parse((content || []).join(''))); | ||
} catch (error) { | ||
// Invalid JSON | ||
} | ||
} | ||
|
||
return tree; | ||
return content; | ||
}; | ||
} | ||
|
||
/* Minify JSON inside <script> tags */ | ||
export { | ||
once, | ||
oncontent | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.