首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Webpack 5将` `html/index.pug`构建为‘`dist/index.html’

使用Webpack 5将` `html/index.pug`构建为‘`dist/index.html’
EN

Stack Overflow用户
提问于 2021-12-08 21:46:37
回答 1查看 351关注 0票数 1

我想用Webpack 5把html/index.pug构建成dist/index.html

对于Webpack 4,我曾经使用过file-loader,但在Webpack 5:在装载机页面中没有提到它,这似乎是不可取的。Webpack 5的解决方案似乎是在使用资产模块:这一页清楚地表明,对于Webpack 4来说,file-loader是旧的解决方案。

不过,到目前为止,我还是没能让它发挥作用。这是我在webpack.config.jsmodule.rules中尝试过的几种配置。

1.仅使用pug-loader

代码语言:javascript
复制
{
    test:path.resolve('./html/index.pug'),
    type:'asset/resource',
    loader:'pug-loader',
    generator:{filename:'index.html'}}
}

这将创建一个包含以下内容的dist/index.html文件:

代码语言:javascript
复制
var pug = require("!../node_modules/pug-runtime/index.js");

function template(locals) {var pug_html = "", pug_mixins = {}, pug_interp;pug_html = pug_html + "\u003C!DOCTYPE html\u003E\u003Chtml lang=\"en\"\u003E\u003Chead\u003E\u003Ctitle\u003E" + (pug.escape(null == (pug_interp = "Hello World") ? "" : pug_interp)) + "\u003C\u002Ftitle\u003E\u003C\u002Fhead\u003E\u003Cbody\u003E\u003Cp\u003EHello World\u003C\u002Fp\u003E\u003C\u002Fbody\u003E\u003C\u002Fhtml\u003E";;return pug_html;};
module.exports = template;

看起来,pug-loader将pug文件转换为一个JavaScript模块,在调用它时生成html代码。我想要的是得到的HTML代码,而不是生成它的JS函数。

2.使用val-loader执行上面生成的JavaScript模块

代码语言:javascript
复制
{
    test:path.resolve('./html/index.pug'),
    type:'asset/resource',
    use:['val-loader','pug-loader'],
    generator:{filename:'index.html'}}
}

这也不起作用: Webpack试图构建dist/index.pug时抛出了一个错误

代码语言:javascript
复制
ERROR in ./html/index.pug
Module build failed (from ./node_modules/val-loader/dist/cjs.js):
Error: Unable to execute "/home/bluenebula/work/webpack5-test/html/index.pug": Error: Cannot find module '!../node_modules/pug-runtime/index.js'

注意,/home/bluenebula/work/webpack5-test/node_modules/pug-runtime/index.js确实存在。

问题

  1. Asset是使用Webpack 5从Pug文件生成HTML文件的正确工具吗?
  2. 我还能做什么?
  3. 我怎么才能让它起作用?
EN

回答 1

Stack Overflow用户

发布于 2022-03-21 15:46:39

(Pug 3.0.2)

我用“帕格装载机”替换为“@webdiscus/pug-载入器”,

而且起作用了。

代码语言:javascript
复制
plugins: [
    new HtmlWebpackPlugin({
      template: path.join(__dirname, 'src/index.pug'),
      filename: 'index.html',
    }),
  ],
  module: {
    rules: [
      {
        test: /\.pug$/,
        loader: '@webdiscus/pug-loader',
      },
    ],
  },

我所遵循的参考:https://github.com/webdiscus/pug-loader#usage-in-javascript

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70282111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档