同一个目录下html页面跳转,webpack打包多页应用,如何处理不同html页面(通过a标签)之间的跳转?...

本文介绍了在使用webpack打包多页应用时遇到的页面跳转404问题。作者提供了项目的源码链接,并详细描述了问题的重现步骤。问题关键在于打包后路径与源码路径不一致导致的跳转失败。作者期望找到一种方法,使打包后的文件路径更灵活,并确保页面间能正常跳转。webpack配置中已包含了HtmlWebpackPlugin用于生成多个HTML页面,但仍然存在路径匹配的问题。
摘要由CSDN通过智能技术生成

想试着用webpack打包多页(多个html文件)应用,不同html之间(利用a标签)跳转只和项目文件结构路径相关,但是打包后发现页面之间跳转都是404了!

源码地址:https://gitee.com/qingyun1029/webpack-for-multiple-page-demo

使用方式:

克隆项目到本地

git clone git@gitee.com:qingyun1029/webpack-for-multiple-page-demo.git

安装依赖模块

npm install

打包代码

npm run build

问题重现:打包后,打开dist/index.html,点击页面上的链接,无法跳转到about页面;反之亦然!

分析:

页面之间的跳转路径唯一相关的是项目文件路径结构,但是通过webpack打包后,输出的路径肯定和源码中写的路径不一样的(通常源码页面放在src文件夹下面,但是打包后肯定不希望有这层路径吧!),所以我该怎么处理这一层关系呢?

期望:

通过webpack打包后的文件路径能够比较灵活的自定义;

页面之间能够正常跳转;

webpack配置如下:

'use strict'

const webpack = require('webpack')

const path = require('path')

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

module.exports = {

mode: 'production',

entry: {

home: './src/pages/home/index.js',

about: './src/pages/about/index.js',

},

output: {

path: path.resolve(__dirname, './dist'),

filename: '[name].[chunkhash].js',

},

resolve: {

extensions: ['.js', '.json'],

},

module: {

},

plugins: [

new HtmlWebpackPlugin({

chunks: ['home'],

filename: 'home.html',

template: 'src/pages/home/html/index.html',

inject: true,

minify: {

removeComments: true,

collapseWhitespace: true,

},

}),

new HtmlWebpackPlugin({

chunks: ['about'],

filename: 'about.html',

template: 'src/pages/about/html/index.html',

inject: true,

minify: {

removeComments: true,

collapseWhitespace: true,

},

}),

],

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值