我读过很多关于webpack的教程,但似乎更多的是为了创建网络应用程序,然后是为了我想要做的事情,所以我不想浪费更多的时间,如果以下不可能的话。
我正在创建一个第三方eCommerce系统上的网站,他们有一个编码模板的系统,可以用来改变他们网站的结构。下面是一个我将要创建的模板的例子(尽管有很多类型&我需要做的变化不只是几个)。
我简化这些模板创建的想法是创建一组pug组件,并将它们放在组件/目录中。在components目录之外,我想创建利用组件的更高级别的pug模板。创建这些文件后,我将使用NPM构建它,模板文件需要转换为/dist文件并放置在文件夹中。
这跟webpack有什么关系吗?
项目结构:
src/
..components/
....header/
......header1.pug
......header1.scss
..navcustom-template.pug
..customfooter-template.pug
..non-template-specific.scss一旦建成:
dist/
..navcustom-template.html
..customfooter-template.html
..non-template-specific.css
src/
..components/
....header/
......header1.pug
......header1.scss
..navcustom-template.pug
..customfooter-template.pug
..non-template-specific.scss模板样本:
<!--
Section: NavCustom
-->
<style>
//Template Speific CSS Imports Here
</style>
<script type="text/javascript">
//Template Speific JS Imports Here
</script>
<header>
<div class="col-md-4">
// Social Media Code
</div>
<div class="col-md-4">
// Logo Code
</div>
<div class="col-md-4">
// Call to Action Code
</div>
</header>
<nav>
</nav>发布于 2018-09-05 05:02:50
您可以使用这些包(--save-dev适用于所有人):
raw-loaderpug-html-loader读取Pug文件html-webpack-pug-plugin从Pug生成HTMLhtml-webpack-plugin (标准的HTML )然后配置Webpack,如下所示,其中index.js是您应该创建的虚拟文件,如果您还没有条目的话。您需要处理的每个Pug模板都在底部的一个单独的HtmlWebpackPlugin对象中编写。
var HtmlWebpackPlugin = require('html-webpack-plugin');
var HtmlWebpackPugPlugin = require('html-webpack-pug-plugin');
module.exports = {
entry: ['./src/index.js'],
output: {
path: __dirname + '/dist',
publicPath: '/'
},
module: {
rules: [
{
test: /\.pug$/,
use: [
"raw-loader",
"pug-html-loader"
]
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: 'src/navcustom-template.pug',
filename: 'navcustom-template.html'
}),
new HtmlWebpackPlugin({
template: 'src/customfooter-template.pug',
filename: 'customfooter-template.html'
}),
new HtmlWebpackPugPlugin()
]
}所有的Pug混频器(您的src/components文件)都将包含在输出中。这个例子是经过测试和工作的。
编辑:要动态处理src目录中的所有Pug文件,请使用以下配置:
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackPugPlugin = require('html-webpack-pug-plugin');
const fs = require('fs');
let templates = [];
let dir = 'src';
let files = fs.readdirSync(dir);
files.forEach(file => {
if (file.match(/\.pug$/)) {
let filename = file.substring(0, file.length - 4);
templates.push(
new HtmlWebpackPlugin({
template: dir + '/' + filename + '.pug',
filename: filename + '.html'
})
);
}
});
module.exports = {
entry: ['./src/index.js'],
output: {
path: __dirname + '/dist',
publicPath: '/'
},
module: {
rules: [
{
test: /\.pug$/,
use: [
"raw-loader",
"pug-html-loader"
]
}
]
},
plugins: [
...templates,
new HtmlWebpackPugPlugin()
]
}这使用fs.readdirSync来获取目录中的所有Pug文件。使用同步版本(与fs.readdir不同),因为module.exports函数将在文件处理之前返回。
发布于 2022-11-23 21:24:59
在2022年出现了Webpack的Pug插件,它将Pug编译成静态的HTML,从在Pug中直接定义的源文件中提取CSS和JS。
仅安装pug-plugin就足够了:
npm install pug-plugin --save-devwebpack.config.js
const path = require('path');
const PugPlugin = require('pug-plugin');
module.exports = {
output: {
path: path.join(__dirname, 'dist/'),
filename: 'assets/js/[name].[contenthash:8].js'
},
entry: {
// Note: a Pug file is the entry-point for all scripts and styles.
// All scripts and styles must be specified in Pug.
// Define Pug files in entry:
index: './src/views/index.pug', // => dist/index.html
'navcustom-template': './src/navcustom-template.pug', // => dist/navcustom-template.html
'customfooter-template': './src/customfooter-template.pug', // => dist/customfooter-template
// etc ...
},
plugins: [
// enable using Pug files as entry-point
new PugPlugin({
extractCss: {
filename: 'assets/css/[name].[contenthash:8].css' // output filename of CSS files
},
})
],
module: {
rules: [
{
test: /\.pug$/,
loader: PugPlugin.loader, // the Pug loader
},
{
test: /\.(css|sass|scss)$/,
use: ['css-loader', 'sass-loader']
},
],
},
};当然,可以像上面的答案那样动态地生成条目。
在Pug文件中,通过require()使用样式和脚本的源文件
html
head
//- add source SCSS styles using a path relative to Pug file
link(href=require('./styles.scss') rel='stylesheet')
body
h1 Hello Pug!
//- add source JS/TS scripts
script(src=require('./main.js'))生成的HTML:
<html>
<head>
<link href="/assets/css/styles.f57966f4.css" rel="stylesheet">
</head>
<body>
<h1>Hello Pug!</h1>
<script src="/assets/js/main.b855d8f4.js"></script>
</body>
</html>只有一个Pug插件取代了与Pug一起使用的许多插件和加载器的功能:
pug-html-loaderhtml-webpack-pug-pluginhtml-webpack-pluginmini-css-extract-pluginresolve-url-loadersvg-url-loaderposthtml-inline-svghttps://stackoverflow.com/questions/52176542
复制相似问题