当前位置:  首页>> 技术小册>> Webpack零基础入门

首先肯定会问什么是 pug,如果是 nodejs 程序员的话,肯定听过 jade 吧,pug 就是 从 jade 改名过来的。

说白了,它就是可以让你以更好的语法来写 html 。

下面看看例子就会清楚的。

现在我们就要代替之前的 src/index.html 改用 pug 语法来写。

首先把 src/index.html 改名叫 src/index.pug

  1. $ rename src/index.html src/index.pug

src/index.pug

  1. doctype html
  2. html(lang="en")
  3. head
  4. title= pageTitle
  5. script(type='text/javascript').
  6. if (foo) bar(1 + 5)
  7. body
  8. h1 Pug - node template engine
  9. #root
  10. #container.col
  11. if youAreUsingPug
  12. p You are amazing
  13. else
  14. p Get on it!
  15. p.
  16. Pug is a terse and simple templating language with a
  17. strong focus on performance and powerful features.

上面的内容是从 pug 官方的示例中抄的,然后稍微改了一下。

webpack.config.js

  1. ...
  2. module.exports = {
  3. ...
  4. plugins: [
  5. ...
  6. new HtmlWebpackPlugin({
  7. template: './src/index.pug',
  8. ...
  9. }),
  10. ...
  11. ],
  12. module: {
  13. rules: [
  14. ...
  15. { test: /\.pug$/, loader: ['raw-loader', 'pug-html-loader'] }
  16. ]
  17. }
  18. };
  1. $ npm install --save-dev pug pug-html-loader raw-loader

这样基本没啥问题,来看下结果:

我们来试试 pug 的 include 功能,就是可以包含子模板。

src/index.pug

  1. ...
  2. body
  3. include includes/header.pug
  4. ...

src/includes/header.png

  1. h1 from header pug file

目录结构是这样的:

  1. src
  2. ├── Root.js
  3. ├── app.js
  4. ├── app.scss
  5. ├── contact.html
  6. ├── contact.js
  7. ├── includes
  8. └── header.pug
  9. └── index.pug

结果:


该分类下的相关小册推荐: