@webdiscus/pug-loader
Advanced tools
Comparing version 2.8.2 to 2.9.0
# Change log | ||
## 2.9.0 (2022-09-27) | ||
- feat: add resolving for require in conditional, e.g.: | ||
```pug | ||
if condition | ||
img(src=require('./image1.png')) | ||
else | ||
img(src=require('./image2.png')) | ||
``` | ||
- feat: add resolving for require in mixin argument, e.g. `+image(require('./logo.png'), 'logo')` | ||
- feat: add resolving for require in `each in` and in `each of` iteration object, e.g. `each [key, img] of new Map([['apple', require('./apple.png')], ['sony', require('./sony.png')]])` | ||
- refactor: replace pug-walk lib with optimized up to x2.5 faster implementation without recursion | ||
- test(BREAKING): drop support for Node v12, because lastest `jest` v29 supports >= Node v14 | ||
## 2.8.2 (2022-09-21) | ||
@@ -4,0 +17,0 @@ - chore: optimize script store for pug-plugin |
{ | ||
"name": "@webdiscus/pug-loader", | ||
"version": "2.8.2", | ||
"version": "2.9.0", | ||
"description": "Pug loader renders Pug files into HTML or compiles them into a template function.", | ||
@@ -9,3 +9,2 @@ "keywords": [ | ||
"webpack", | ||
"entry", | ||
"compile", | ||
@@ -15,3 +14,4 @@ "render", | ||
"plain", | ||
"template", | ||
"filter", | ||
"markdown", | ||
"pug-loader", | ||
@@ -71,12 +71,12 @@ "pug-plugin" | ||
"devDependencies": { | ||
"@babel/core": "^7.18.10", | ||
"@babel/core": "^7.18.13", | ||
"@babel/preset-env": "^7.18.10", | ||
"@types/jest": "^28.1.7", | ||
"@types/jest": "^28.1.8", | ||
"css-loader": "^6.7.1", | ||
"html-loader": "^4.1.0", | ||
"html-webpack-plugin": "^5.5.0", | ||
"jest": "^28.1.3", | ||
"jest": "^29.0.1", | ||
"prettier": "^2.7.1", | ||
"prismjs": "^1.28.0", | ||
"pug-plugin": "~4.1.1", | ||
"prismjs": "^1.29.0", | ||
"pug-plugin": "~4.2.0", | ||
"responsive-loader": "^3.1.0", | ||
@@ -83,0 +83,0 @@ "sharp": "^0.30.7", |
131
src/index.js
const fs = require('fs'); | ||
const path = require('path'); | ||
const pug = require('pug'); | ||
const walk = require('pug-walk'); | ||
const { plugin, scriptStore } = require('./Modules'); | ||
@@ -79,2 +78,93 @@ const dependency = require('./Dependency'); | ||
/** | ||
* Resolve filenames in Pug node. | ||
* | ||
* @param {Object} node The Pug AST Node. | ||
*/ | ||
const resolveNode = (node) => { | ||
switch (node.type) { | ||
case 'Code': | ||
if (isRequired(node.val)) { | ||
node.val = loader.resolveResource(node.val, node.filename); | ||
} | ||
break; | ||
case 'Mixin': | ||
if (isRequired(node.args)) { | ||
node.args = loader.resolveResource(node.args, node.filename); | ||
} | ||
break; | ||
case 'Each': | ||
case 'EachOf': | ||
if (isRequired(node.obj)) { | ||
node.obj = loader.resolveResource(node.obj, node.filename); | ||
} | ||
break; | ||
default: | ||
if (node.attrs && node.attrs.length > 0) { | ||
// resolving of required files in tag attributes | ||
for (let attr of node.attrs) { | ||
const value = attr.val; | ||
if (isRequired(value)) { | ||
if (node.name === 'script') { | ||
attr.val = loader.resolveScript(value, attr.filename); | ||
} else if (node.name === 'link' && node.attrs.find(isStyle)) { | ||
attr.val = loader.resolveStyle(value, attr.filename); | ||
} else { | ||
attr.val = loader.resolveResource(value, attr.filename); | ||
} | ||
} | ||
} | ||
} | ||
break; | ||
} | ||
}; | ||
/** | ||
* Traverse all Pug nodes and resolve filename in each node. | ||
* | ||
* @note: This is implementation of the 'pug-walk' logic without recursion and up to x2.5 faster. | ||
* | ||
* @param {Object} tree The tree of Pug nodes. | ||
*/ | ||
const walkTree = (tree) => { | ||
let stack = [tree]; | ||
let ast, i; | ||
while ((ast = stack.pop())) { | ||
while (true) { | ||
resolveNode(ast); | ||
switch (ast.type) { | ||
case 'Tag': | ||
case 'Code': | ||
case 'Case': | ||
case 'Mixin': | ||
case 'When': | ||
case 'While': | ||
case 'EachOf': | ||
if (ast.block) stack.push(ast.block); | ||
break; | ||
case 'Each': | ||
if (ast.block) stack.push(ast.block); | ||
if (ast.alternate) stack.push(ast.alternate); | ||
break; | ||
case 'Conditional': | ||
if (ast.consequent) stack.push(ast.consequent); | ||
if (ast.alternate) stack.push(ast.alternate); | ||
break; | ||
default: | ||
break; | ||
} | ||
if (!ast.nodes || ast.nodes.length === 0) break; | ||
const lastIndex = ast.nodes.length - 1; | ||
for (i = 0; i < lastIndex; i++) { | ||
stack.push(ast.nodes[i]); | ||
} | ||
ast = ast.nodes[lastIndex]; | ||
} | ||
} | ||
}; | ||
/** | ||
* The pug plugin to resolve path for include, extends, require. | ||
@@ -98,31 +188,10 @@ * | ||
/** | ||
* Resolve the filename for require(). | ||
* Resolve the filename in require(). | ||
* | ||
* @param {{}} ast The parsed tree of pug template. | ||
* @param {{}} tree The parsed tree of pug template. | ||
* @return {{}} | ||
*/ | ||
preCodeGen(ast) { | ||
return walk(ast, (node) => { | ||
if (node.type === 'Code') { | ||
// resolving for require in code, like `- var data = require('./data.js')` | ||
const value = node.val; | ||
if (isRequired(value)) { | ||
node.val = loader.resolveResource(value, node.filename); | ||
} | ||
} else if (node.attrs) { | ||
// resolving of required files in tag attributes | ||
for (let attr of node.attrs) { | ||
const value = attr.val; | ||
if (isRequired(value)) { | ||
if (node.name === 'script') { | ||
attr.val = loader.resolveScript(value, attr.filename); | ||
} else if (node.name === 'link' && node.attrs.find(isStyle)) { | ||
attr.val = loader.resolveStyle(value, attr.filename); | ||
} else { | ||
attr.val = loader.resolveResource(value, attr.filename); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
preCodeGen(tree) { | ||
walkTree(tree); | ||
return tree; | ||
}, | ||
@@ -260,3 +329,6 @@ }; | ||
//let profilerTime = 0; | ||
module.exports = function (content, map, meta) { | ||
//const startTime = performance.now(); | ||
const loaderContext = this; | ||
@@ -274,4 +346,9 @@ const callback = loaderContext.async(); | ||
} | ||
// Profiling of common runtime. | ||
// profilerTime += performance.now() - startTime; | ||
// console.log('>>> pug-loader time :', profilerTime); | ||
callback(null, result, map, meta); | ||
}); | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
129735
2301