当前位置:网站首页 > 云计算与后端部署 > 正文

webpack5配置portfinder支持端口多开

一,概述

有时本地运行多个项目时,80端口已经被使用,这时候npm run dev,如果没有检测可用端口的功能,就会报错:”端口已被占用"。为了解决这一个问题,需要在运行项目之前,扫描端口,取得一个可用的端口,来进行项目的运行。

二,portfinder使用前

在没有引入之前,webpack的配置时大概这样的。

const path = require("path"); const { 
    merge } = require("webpack-merge"); const webpack = require("webpack"); const HtmlWebpackPlugin = require("html-webpack-plugin"); const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const BaseWebpackConfig = require("./webpack.base.conf"); module.exports = merge(BaseWebpackConfig, { 
    mode: "development", output: { 
    path: path.resolve(__dirname, "../dist"), filename: "./js/[name].[chunkhash].js", clean: true // 打包时先清除上次打包文件 }, devServer: { 
    hot: true, //模块的热替换 open: true, // 编译结束后自动打开浏览器 port: 8080, // 设置本地端口号 host: "localhost", //设置本地url // 设置代理,用来解决本地开发跨域问题 proxy: { 
    "/api": { 
    secure: false, changeOrigin: true, target: "https://www.fastmock.site/mock/88bbb3bb8d6ea3dc8f09431a61ce2e50/mymock_test", pathRewrite: { 
    "^/api": "" } } } }, plugins: [ new HtmlWebpackPlugin({ 
    template: "./public/index.html", //用来做模板的html的文件路径 filename: "index.html", //生成的html的名字 title: "webpack5的项目配置", //这个就对应上文的title inject: "body" //打包出来的那个js文件,放置在生成的body标签内 }), new MiniCssExtractPlugin(), new webpack.HotModuleReplacementPlugin() ], optimization: { 
    runtimeChunk: "single" } }); 

三,portfinder的使用

1,安装

npm i portfinder -D 

2,引入

const portfinder = require('portfinder');//自动查找可用端口 

3,修改配置

主要修改的地方是这里:

...、、其他引入 const FriendlyErrorsWebpackPlugin = require("friendly-errors-webpack-plugin"); const portfinder = require('portfinder');//自动查找可用端口 const devWebpackConfig = merge(BaseWebpackConfig, { 
    ...//旧有配置 }); //改成这里来module.exports了,可以看到最后又把整个webpack配置resolve出去了。 module.exports = new Promise((resolve, reject) => { 
    portfinder.basePort = devWebpackConfig.devServer.port; portfinder.getPort((err, port) => { 
    if (err) { 
    reject(err) } else { 
    // publish the new Port, necessary for e2e tests process.env.PORT = port // add port to devServer config,主要是这一步更新可用的端口 devWebpackConfig.devServer.port = port // Add FriendlyErrorsPlugin devWebpackConfig.plugins.push( new FriendlyErrorsWebpackPlugin({ 
    compilationSuccessInfo: { 
    messages: [ `Your application is running here: http://${devWebpackConfig.devServer.host}:${port}` ], notes: [] }, clearConsole: true }) ) resolve(devWebpackConfig) } }) }) 

修改后完整的配置:

const path = require("path"); const { 
    merge } = require("webpack-merge"); const webpack = require("webpack"); const HtmlWebpackPlugin = require("html-webpack-plugin"); const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const BaseWebpackConfig = require("./webpack.base.conf"); const FriendlyErrorsWebpackPlugin = require("friendly-errors-webpack-plugin"); const portfinder = require('portfinder');//自动查找可用端口 const devWebpackConfig = merge(BaseWebpackConfig, { 
    mode: "development", output: { 
    path: path.resolve(__dirname, "../dist"), filename: "./js/[name].[chunkhash].js", clean: true // 打包时先清除上次打包文件 }, devServer: { 
    hot: true, //模块的热替换 open: true, // 编译结束后自动打开浏览器 port: 8080, // 设置本地端口号 host: "localhost", //设置本地url // 设置代理,用来解决本地开发跨域问题 proxy: { 
    "/api": { 
    secure: false, changeOrigin: true, target: "https://www.fastmock.site/mock/88bbb3bb8d6ea3dc8f09431a61ce2e50/mymock_test", pathRewrite: { 
    "^/api": "" } } } }, plugins: [ new HtmlWebpackPlugin({ 
    template: "./public/index.html", //用来做模板的html的文件路径 filename: "index.html", //生成的html的名字 title: "webpack5的项目配置", //这个就对应上文的title inject: "body" //打包出来的那个js文件,放置在生成的body标签内 }), new MiniCssExtractPlugin(), new webpack.HotModuleReplacementPlugin() ], optimization: { 
    runtimeChunk: "single" } }); module.exports = new Promise((resolve, reject) => { 
    portfinder.basePort = devWebpackConfig.devServer.port; portfinder.getPort((err, port) => { 
    if (err) { 
    reject(err) } else { 
    // publish the new Port, necessary for e2e tests process.env.PORT = port // add port to devServer config devWebpackConfig.devServer.port = port // Add FriendlyErrorsPlugin devWebpackConfig.plugins.push( new FriendlyErrorsWebpackPlugin({ 
    compilationSuccessInfo: { 
    messages: [ `Your application is running here: http://${devWebpackConfig.devServer.host}:${port}` ], notes: [] }, clearConsole: true }) ) resolve(devWebpackConfig) } }) }) 
到此这篇webpack5配置portfinder支持端口多开的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • 调试前端代码二三事--(一)-调试基础2024-12-02 07:00:07
  • 搭建前端项目时的技术选型考虑2024-12-02 07:00:07
  • http之浏览器同源政策——端口、域名、协议,三者同及不跨域;解决跨域的方案有哪些2024-12-02 07:00:07
  • Centos 查看服务器磁盘,内存,端口等命令2024-12-02 07:00:07
  • JKS后缀结尾tomcat的证书转换成key和crt结尾的nginx证书2024-12-02 07:00:07
  • docker基础(五)-多阶段构建部署vue前端项目2024-12-02 07:00:07
  • docker基础(三)-制作镜像部署vue前端项目2024-12-02 07:00:07
  • 前端项目架构模板-(三)交互式打包及自动化部署前端项目2024-12-02 07:00:07
  • 异步操作之后让await后续的代码能够继续执行2024-12-02 07:00:07
  • VUE项目部署到tomcat服务器-前端配置2024-12-02 07:00:07
  • 全屏图片