K
K
kirillleogky2019-12-25 12:02:38
JavaScript
kirillleogky, 2019-12-25 12:02:38

How to include two html pages in webpack?

There is this structure:
5e0325a5670ff550266532.png
Tell me how can I connect two html files via webpack?
And how to specify the correct path for this in the webpack config
. The first one should be the landing page - src/screens/landing/index.html
The second one opens when the button on the landing page is clicked - src/screens/app/index.html
My webpack config:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: './src/index.js',
  mode: 'development',
  output: {
    filename: 'main.js',
    path: path.resolve(__dirname, 'dist')
  },
  module: {
  rules: [
    {
      test: /\.js$/,
      enforce: 'pre',
      exclude: /node_modules/,
      loader: 'eslint-loader',
    },
    {
      test: /\.scss$/,
      use: [{
          loader: "style-loader"
      }, {
          loader: "css-loader"
      }, {
          loader: "sass-loader",
      }]
    },
    {
      test: /\.(jpg|png|svg|ttf|woff|eot)$/,
      loader: 'url-loader',
      options: {
       name: 'img/[name].[ext]',
      },
    }
  ],
 },
  plugins: [
    new HtmlWebpackPlugin({ template: 'src/screens/app/index.html'})
  ]
};

I probably need to somehow change this in the config, but I don’t know how:
plugins: [
    new HtmlWebpackPlugin({ template: 'src/screens/app/index.html'})
  ]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Levchenko, 2020-01-06
@nuykon

plugins: [
    new HtmlWebpackPlugin({ template: 'src/screens/app/index.html'}),
    new HtmlWebpackPlugin({ template: 'src/screens/landing/index.html'})
]

but in my opinion it's better to go through the folder and get a list of pages (an array)
and then something like this:
plugins: [
    ...PAGES.map((page) => new HtmlWebpackPlugin({
      template: `${PAGES_DIR}/${page}`,
      filename: `./${page}`,
      inject: true,
    })),
  ],

A
Anatoly, 2020-07-15
@Beefeater

From myself I will add that the second file is not created. Now it is worth prescribing filename to it

new HTMLWebpackPlugin({
      filename: 'index.html',
      template: './index.html'
    }),
new HTMLWebpackPlugin({
      filename: 'second.html',
      template: './second.html'
    })

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question