如何在Webpack 5 + Pug中链接页面?

huangapple go评论52阅读模式
英文:

How to link pages in Webpack 5 + Pug?

问题

我有一个基本的webpack模板,带有pug-loader。我有一个入口pug文件。当我在开发服务器上开发时,我需要一个可以链接到另一个页面(也是用pug编写的)的工作链接。我该怎么做?我应该将pug文件编译成HTML文件并链接HTML页面吗?我知道浏览器不读取pug,只是不知道如何正确设置它。

我刚刚写了一个链接到另一个.pug页面,但我总是收到404错误。自动创建HTML文件已经设置好了以进行构建。

英文:

I have a basic webpack template with pug-loader. I have an entry pug file. When I develop on dev server, I need working link to another page (also written in pug). How can I do that? Should I compile pug files to html in my src directory and link html pages instead? I know browser doesn't read pug, just don't know how to set up it right

I just wrote a link to another .pug page and I always get 404. Auto creating of html files is set up for building

答案1

得分: 1

Here is the translated code:

已解决需要使用以下代码和url-loader

{
  test: /\.(html)$/i,
  use: [
    {
      loader: 'url-loader',
      options: {
        // The `mimetype` and `encoding` arguments will be obtained from your options
        // The `resourcePath` argument is the path to the file.
        generator: (content, mimetype, encoding, resourcePath) => {
          if (/\.html$/i.test(resourcePath)) {
            return `data:${mimetype},${content.toString()}`;
          }
          return `data:${mimetype}${encoding ? `;${encoding}` : ''},${content.toString(encoding)}`;
        },
      },
    },
  ],
}

Please note that the code part is translated, and I have excluded the request for further translation.

英文:

Resolved myself. Needs to use url-loader with following code:

{
        test: /\.(html)$/i,
        use: [
          {
            loader: 'url-loader',
            options: {
              // The `mimetype` and `encoding` arguments will be obtained from your options
              // The `resourcePath` argument is path to file.
              generator: (content, mimetype, encoding, resourcePath) => {
                if (/\.html$/i.test(resourcePath)) {
                  return `data:${mimetype},${content.toString()}`;
                }
                return `data:${mimetype}${
                  encoding ? `;${encoding}` : ''
                },${content.toString(encoding)}`;
              },
            },
          },
        ],
      },

And write links to other pug files like they paths in dist are, with .html extension

huangapple
  • 本文由 发表于 2023年5月7日 03:18:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定