无法直接打开dist下index.html运行,求Webpack配置解决方案
问题描述
尝试直接打开dist目录下的index.html文件(如同打开普通HTML文件一样),但出现错误:
不确定Webpack是否打包正确,希望实现直接打开HTML文件就能运行应用。
webpack.common.js
const HtmlWebpackPlugin = require('html-webpack-plugin'); const TerserPlugin = require('terser-webpack-plugin'); const path = require('path'); const webpack = require('webpack'); require('../server/boot'); const paths = require('./paths'); module.exports = { //entry file entry: paths.src + '/index.jsx', target: 'web', // output: { // publicPath: '/', // chunkFilename: '[chunkhash].js', // hotUpdateChunkFilename: '[id].[fullhash].hot-update.js', // filename: '[name].bundle.js', // }, optimization: { minimizer: [ new TerserPlugin({ terserOptions: { format: { comments: false, }, }, parallel: true, extractComments: false, }), ], }, // Customize the webpack build process plugins: [ // Removes/cleans build folders and unused assets when rebuilding // new CleanWebpackPlugin(), new webpack.HotModuleReplacementPlugin(), // Generates an HTML file from a template // Generates deprecation warning: https://github.com/jantimon/html-webpack-plugin/issues/1501 new HtmlWebpackPlugin({ template: paths.public + '/index.html', // template file filename: 'index.html', // output file minify: { removeComments: true, collapseWhitespace: true, }, inlineSource: '.(js|jsx|css|scss)$', }), new webpack.DefinePlugin({ 'process.env': JSON.stringify(process.env), }), ], // Determine how modules within the project are treated module: { rules: [ // JavaScript: Use Babel to transpile JavaScript files { test: /\.(js|jsx)$/, exclude: /node_modules/, use: ['babel-loader'] }, // Images: Copy image files to build folder { test: /\.(png|svg|jpg|jpeg|gif)$/i, type: 'asset/inline', }, // Fonts and SVGs: Inline files { test: /\.(woff|woff2|eot|ttf|otf)$/i, type: 'asset/inline' }, ], }, };
webpack.prod.js
const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const { merge } = require('webpack-merge'); const paths = require('./paths'); const common = require('./webpack.common'); const path = require('path'); module.exports = merge(common, { mode: 'production', output: { path: paths.build, // publicPath: '/', // filename: 'js/[name].[contenthash].bundle.js', // chunkFilename: '[chunkhash].js', // hotUpdateChunkFilename: '[id].[fullhash].hot-update.js', }, devServer: { contentBase: '/dist', historyApiFallback: true, // no publicPath }, module: { rules: [ { test: /\.(sass|scss|css)$/, use: ['style-loader', 'css-loader', 'postcss-loader', 'sass-loader'], }, ], }, plugins: [ // Extracts CSS into separate files new MiniCssExtractPlugin({ filename: '[name].[hash].css', chunkFilename: '[id].css', }), ], optimization: { minimize: true, minimizer: [new CssMinimizerPlugin(), '...'], runtimeChunk: { name: 'runtime', }, }, performance: { hints: false, maxEntrypointSize: 512000, maxAssetSize: 512000, }, });
解决方案
直接打开本地HTML用的是file://协议,和HTTP服务的路径规则不同,改以下几个配置就能解决问题:
- 设置相对路径的
publicPath
在webpack.prod.js的output里把publicPath设为./,所有资源都会基于当前HTML所在目录加载,不会出现路径找不到的情况:
output: { path: paths.build, publicPath: './', // 核心修改:改为相对路径 filename: 'js/[name].[contenthash].bundle.js', chunkFilename: 'js/[chunkhash].js', }
同时把webpack.common.js里注释的output配置取消注释,开发环境保持publicPath: '/'即可,不影响devServer运行。
- 修复CSS插件冲突
生产环境中同时使用MiniCssExtractPlugin和style-loader会导致冲突,把style-loader替换成MiniCssExtractPlugin.loader,让CSS被提取为单独文件,确保路径能正确解析:
module: { rules: [ { test: /\.(sass|scss|css)$/, use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader'], }, ], },
删除多余的devServer配置
webpack.prod.js里的devServer是开发环境专用配置,生产打包不需要,直接删除即可。确认路径配置正确性
检查paths.js里的build是否指向项目根目录下的dist文件夹,示例配置如下:
module.exports = { src: path.resolve(__dirname, '../src'), public: path.resolve(__dirname, '../public'), build: path.resolve(__dirname, '../dist'), };
修改完成后重新执行打包命令,生成dist目录后直接打开index.html就能正常运行应用了。
内容的提问来源于stack exchange,提问作者Samben Jerald
相关产品推荐
相关产品推荐

