在webpack的项目中,我决定从ejs转向pug作为模板引擎。我需要的是预编译的静态输出(无论是在dev还是prod模式下)。此外,我需要在编译过程中将数据传递给模板,并为此使用htmlWebpackPlugin的自定义选项。项目结构如下:
root
|--package.json
|--webpack.configs // .dev.js, build.js
|--data.json
|--layout.pug
|--app
|--|--index.js
|--|--views
|--|--|--index.pug
|--|--|--includes
|--|--|--|--nav.pug
|--|--|--|--footer.pug
|--assets
|--|--(styles, images...)我找到并尝试的第一种方法是将数据传递到入口点(在index.js编译局部变量并通过innerHTML =将它们插入页面)。这种方式绝对不适合我,因为我需要100%的预编译静态输出,没有运行时HTML插入。然后乐趣开始了..。
例如,我需要创建导航。我有如下导航模式:
module.exports.nav = [
{
text: 'Home',
href: '/'
},
{
text: 'Offers',
href: '/offers.html'
},
{
text: 'Subnav Parent Test',
isSubnavTrigger: true
},
{
text: 'Subnav Item',
href: '/test.html',
isSubnavItem: true,
subnavParent: 'Subnav Parent Test'
}
]和这个app/views/includes/nav.pug的pug部分
nav.navbar(role="navigation", ariaLabel="navigation")
.container
.navbar-brand
a.navbar-item(href="/")
img(src="/assets/images/logo.png", alt=title)
.navbar-menu(id="navbar-menu")
.navbar-start
.navbar-end
for link, index in nav
//- TODO: isActive
if !link.isSubnavTrigger && !link.isSubnavItem
a(href=link.href, class="navbar-item")= link.text
else if link.isSubnavTrigger
.navbar-item.has-dropdown.is-hoverable
a.navbar-link= link.text
.navbar-dropdown
- var parent = link.text
- var dd = nav.filter(_item => _item.subnavParent ===
parent)
each dd_link in dd
a.navbar-item(href=dd_link.href)= dd_link.text该部分包含在公共布局layout.pug中。
- var { title, nav } = htmlWebpackPlugin.options;
<!DOCTYPE html>
html(lang="en")
head
title= title + 'Pug Webpack Test'
block metas
body
header
include ./app/views/includes/nav.pug
main
block content
footer
include ./app/views/includes/footer.pug希望app/views/includes/nav.pug能插值nav变量。
因此,我发现了两种情况:使用module -> rules -> loader和内联加载程序进行HTMLWebpackPlugin。
第一次。webpack.config.js设置:
module.exports = {
module: {
rules: [
//...
{
test: /\.pug$/,
use: ['file-loader?name=[path][name].html', 'pug-html-loader?pretty&exports=false']
}
//...
],
},
plugins: [
//...
new HtmlWebpackPlugin({
template: './layout.pug',
filename: 'index.html',
title: 'siteTitle',
nav: nav // required somewhere at the top of the file
})
]
}在这种情况下,我无法将选项从htmlWebpackPlugin传递到模板中。正如在layout.pug中提到的,我尝试从选项中解构title和nav字段,并获得编译错误:Cannot read property 'options' of undefined。
第二个案子。webpack.config.js
module.exports = {
module: {
rules: [
//...
// Removed PUG loaders
//...
],
},
plugins: [
//...
new HtmlWebpackPlugin({
template: '!!pug-loader!./layout.pug', // <- inlined loader
filename: 'index.html',
title: 'siteTitle',
nav: nav // required somewhere at the top of the file
})
]
}
// by the way, in case of EJS I've been using exactly this approach
// and it worked out pretty well with partials这种方法完美地编译了layout.pug,并使用了从htmlWebpackPlugin、获取的选项,直到它不包含包含的。我需要把我的nav传递给相应的部分,然后从那里渲染导航。并且部分开始将错误抛到编译中,就好像它不理解pug并需要加载程序一样:
ERROR in ./app/views/includes/nav.pug 13:12
Module parse failed: Unexpected token (13:12)
You may need an appropriate loader to handle this file type.
| .navbar-start
| .navbar-end
> for link, index in nav
| //- TODO: isActive
| if !link.isSubnavTrigger && !link.isSubnavItem
@ ./app/views/index.pug (c:/_npmg/node_modules/pug-loader!./app/views/index.pug) 4:514-543我现在一团糟。我不想退回到ejs,但如果我找不到答案,似乎我将不得不这样做。
谢谢向前。
发布于 2019-06-12 12:19:15
解决了,
答案包括两个结论:
pug-loader (不是pug-html-loader,也不是多个加载器链)。引用是这里,但用两个词表示:
pug-loader:返回一个函数
pug-html-loader:返回一个字符串,因此它是静态的,不能传递任何选项因此,与此相关的最后webpack.config.js设置是:
module.exports = {
module: {
rules: [
//...
test: /\.pug$/,
use: [
{loader: 'pug-loader'}
]
//...
],
},
plugins: [
//...
new HtmlWebpackPlugin({
template: './layout.pug', // <- NOT inlined loader
filename: 'index.html',
title: 'siteTitle',
nav: nav // required somewhere at the top of the file
})
]
}htmlWebpackPlugin.options的数据,我必须为该数据声明变量,而不是在公共布局文件中,而是在部分数据本身中:// app/views/partials/nav.pug
- var { nav } = htmlWebpackPlugin.options
nav.navbar(... and the other code you've seen above)如果有人面临这个问题,这是百分之百的工作方法。
发布于 2019-07-09 21:20:56
我想通知OP他的解决方案,但我不能评论。
HtmlWebpackPlugin和pug-loader可以很好地合作。而不是使用:
// webpack.config file
new HtmlWebpackPlugin({
template: './layout.pug',
filename: 'index.html',
myContent: 'loremp ipsum'
})
// app/views/partials/nav.pug
- var { myContent } = htmlWebpackPlugin.options
p= myContent您可以使用以下语法:
// webpack.config file
new HtmlWebpackPlugin({
template: './layout.pug',
filename: 'index.html',
templateParameters: {
myContent: 'loremp ipsum'
}
})
// app/views/partials/nav.pug
p= myContent它与所有帕格引擎功能一起工作,我们可以获得更多可读的模板。
https://stackoverflow.com/questions/56558741
复制相似问题