How to set up a web package for Pug, React and ES6

I am trying to create a site using React and ES6. I use Webpack to translate my JS using Babel, and it works great. Now I need to know how to write my template in Pug (or HTML, for that matter) and add it to the Webpack workflow. I want two files in my build folder:

  • My bundle.js
  • My index.htmlfile compiled from my index.pugfile

An example file webpack.config.jswould be helpful, but what I really need is just a general idea on how to do this.

Thank!

+4
source share
1 answer

webpack, -.

htmlwebpack

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

.

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

- -

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"]
        }
    ]
  }
}
+5

Source: https://habr.com/ru/post/1669105/


All Articles