【问题标题】:How to create multiple pages in Webpack?如何在 Webpack 中创建多个页面?
【发布时间】:2017-11-17 08:59:58
【问题描述】:

这似乎是一个业余问题,但我无法使用 Webpack 创建其他页面

这是我的 webpack.config.js 文件

var HtmlWebpackPlugin = require('html-webpack-plugin')
var HtmlWebpackHarddiskPlugin = require('html-webpack-harddisk-plugin')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var webpack = require('webpack')
var path = require('path')

var isProd = process.env.NODE_ENV === 'production' // true or false
var cssDev = ['style-loader', 'css-loader', 'sass-loader']
var cssProd = ExtractTextPlugin.extract({
  fallback: 'style-loader',
  loader: ['css-loader', 'sass-loader'],
  publicPath: '/dist'
})
var cssConfig = isProd ? cssProd : cssDev

module.exports = {
  entry: {
    app: './src/app.js'
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].bundle.js'
  },
  module: {
    rules: [
      {
        enforce: 'pre',
        test: /\.js$/,
        use: 'eslint-loader',
        exclude: /node_modules/
      },
      {
        test: /\.scss$/,
        use: cssConfig
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: 'babel-loader'
      },
      {
        test: /\.pug$/,
        use: 'pug-loader'
      }
    ]
  },
  devServer: {
    contentBase: path.join(__dirname, 'dist'),
    compress: true,
    hot: true,
    open: true
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'Project Demo',
      hash: true,
      alwaysWriteToDisk: true,
      template: './src/index.pug'
    }),
    new HtmlWebpackHarddiskPlugin(),
    new ExtractTextPlugin({
      filename: 'app.css',
      disable: !isProd,
      allChunks: true
    }),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NamedModulesPlugin()
  ]
}

基本上,我的配置文件当前使用HtmlWebpackPlugin 输入 我的index.pug 从src 文件夹然后输出 index.html 到dist文件夹。

但是,如果我想创建不同的页面怎么办?例如,关于页面。

我知道如果我在我的src 文件夹中创建一个about.pug,它不会被Webpack 处理,因为我已经将它包含在我的配置文件中。但是,我不确定如何做到这一点。

我如何在我的src 文件夹中输入不同的哈巴狗文件,例如about.pug gallery.pug contact.pug 然后在我的@987654333 中拥有所有这些输出 @文件夹为about.htmlgallery.htmlcontact.html

【问题讨论】:

    标签: javascript node.js webpack pug webpack-dev-server


    【解决方案1】:

    正如@Derek 所述,您可以通过编程方式生成HtmlWebpackPlugin 实例列表。

    const path = require('path')
    const fs = require('fs')
    
    // Store .html file names in src/ in an array
    const pages =
      fs
        .readdirSync(path.resolve(__dirname, 'src'))
        .filter(fileName => fileName.endsWith('.html'))
    
    module.exports = {
      // Use .map() on the array of file names to map each file name
      // to the plugin instance, then spread the mapped array into
      // the plugins array.
      plugins: [
        ...pages.map(page => new HtmlWebpackPlugin({
          template: page,
          filename: page
        }))
      ]
     }
    

    【讨论】:

      【解决方案2】:

      我相信你可以创建HtmlWebpackPlugin的多个实例:

      plugins: [
          new HtmlWebpackPlugin({
            title: 'Project Demo',
            hash: true,
            alwaysWriteToDisk: true,
            template: './src/index.pug'
          }),
          new HtmlWebpackPlugin({
            title: 'Page2',
            hash: true,
            alwaysWriteToDisk: true,
            template: './src/page2.pug'
          }),
      ...
      

      【讨论】:

      • 没有办法包含整个目录吗?
      • 不要这么认为。我无法通过我的公司网络访问 GitHub,但通过谷歌搜索找到了这个 github.com/jantimon/…
      • 或者你可以写一个循环来生成数组。
      • webpack 配置只是一个 JavaScript 文件,因此您可以读取一个目录并根据目录的内容以编程方式生成 HtmlWebpackPlugin 条目列表。
      猜你喜欢
      • 2017-05-19
      • 1970-01-01
      • 2014-07-20
      • 1970-01-01
      • 2021-02-25
      • 2014-07-02
      • 2012-12-14
      • 2021-01-06
      • 1970-01-01
      相关资源
      最近更新 更多