【问题标题】:How to set up webpack for Pug, React, and ES6如何为 Pug、React 和 ES6 设置 webpack
【发布时间】:2017-02-08 04:24:54
【问题描述】:

我正在尝试使用 React 和 ES6 创建一个网站。我正在使用 Webpack 使用 Babel 转译我的 JS,它运行良好。现在我需要知道如何用 Pug(或 HTML)编写我的模板并将其添加到 Webpack 工作流程中。我希望我的构建文件夹有两个文件:

  1. 我的bundle.js
  2. 我的index.html 文件是从我的index.pug 文件编译而来的

一个示例 webpack.config.js 文件会很有帮助,但我真正想要的只是有关如何执行此操作的一般想法。

谢谢!

【问题讨论】:

    标签: javascript reactjs webpack pug-loader


    【解决方案1】:

    你需要先安装几个 webpack 插件才能在 webpack 中使用 pug 模板。

    1. htmlwebpack plugin
    2. pug-loader

    使用 htmlwebpack 插件你可以指定你的 pug 模板文件

    new HtmlWebpackPlugin({
          template : './index.pug',
          inject   : true
    })
    

    pug 模板文件将被 pug-loader 加载。

        {
            test: /\.pug$/,
            include: path.join(__dirname, 'src'),
            loaders: [ 'pug-loader' ]
        },
    

    示例 webpack 配置文件如下 -

    const path              = require('path');
    const webpack           = require('webpack');
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    const ExtractTextPlugin = require('extract-text-webpack-plugin');
    
    const isTest = process.env.NODE_ENV === 'test'
    
    module.exports = {
    
      devtool: 'eval-source-map',
    
      entry: {
          app: [
              'webpack-hot-middleware/client',
              './src/app.jsx'
          ]
      },
      output: {
        path       : path.join(__dirname, 'public'),
        pathinfo   : true,
        filename   : 'bundle.js',
        publicPath : '/'
      },
    
      plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin(),
        new ExtractTextPlugin("style.css", { allChunks:false }),
        isTest ? undefined : new webpack.optimize.CommonsChunkPlugin({
          name: 'vendor',
        }),
        new HtmlWebpackPlugin({
              template : './index.pug',
              inject   : true
        })
      ].filter(p => !!p),
    
      resolve: {
        extensions: ['', '.json', '.js', '.jsx']
      },
    
      module: {
        loaders: [
            {
                test    : /\.jsx?$/,
                loader  : 'babel',
                exclude : /node_modules/,
                include : path.join(__dirname, 'src')
            },
            {
                test    : /\.scss?$/,
                loader  : ExtractTextPlugin.extract("style-loader", "css-loader!autoprefixer-loader!sass-loader"),
                include : path.join(__dirname, 'sass')
            },
            {
                test    : /\.png$/,
                loader  : 'file'
            },
            {
                test    : /\.(ttf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
                loader  : 'file'
            },
            {
                test: /\.pug$/,
                include: path.join(__dirname, 'src'),
                loaders: [ 'pug-loader' ]
            },
            {
                include : /\.json$/,
                loaders : ["json-loader"]
            }
        ]
      }
    }
    

    【讨论】:

    • 我会试试这个。谢谢!
    • 太棒了!有用! HtmlWebpackPlugin 是缺失的部分 :)
    • 有没有办法生成 pug 文件而不是 html 输出
    • 点赞!不是表示已经自己渲染了 pug 文件
    • 还需要安装pug,否则pug-loader 似乎丢失了
    猜你喜欢
    • 2021-12-30
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 2017-12-02
    • 2017-06-24
    • 2020-08-03
    相关资源
    最近更新 更多