vue3+vite+nodejs,通过接口的形式请求后端打包(可打包全部或指定打包组件)

news/2024/7/7 20:39:52 标签: 前端

项目地址https://gitee.com/sybb011016/test_build

打包通过按钮的形式请求接口,让后端进行打包,后端使用express-generator搭建模版。前端项目就在npm init vue@latest基础上添加了路由
在这里插入图片描述
如果只想打包AboutView组件,首先修改后端接口。

//打包的测试接口
router.get("/build", function (req, res) {
  //打包的文件路径
  const frontendProjectPath = "E:\\BaiduNetdiskDownload\\Vue.js\\vue_build";
  const component = req.query.component || ""; //传入的参数
  //根据参数决定打包指定目录还是全部打包 cross-env需要前端下载,运行的时候动态设置环境变量
  const buildCommand = component
    ? `cross-env BUILD_COMPONENT=${component} npm run build`
    : `npm run build`;

  exec(buildCommand, { cwd: frontendProjectPath }, (err, stdout, stderr) => {
    if (err) {
      console.error(`exec error: ${err}`);
      return res.status(500).send(`Error during build: ${err.message}`);
    }
    console.log(`stdout: ${stdout}`);
    console.error(`stderr: ${stderr}`);
    res.send("Build completed successfully");
  });
});

前端部分接口如下

export const buildAPI = (comp) => {
  return request({
    url: "/build",
    method: "GET",
    params: {
      component: comp,
    },
  });
};
let res = await buildAPI("About");

修改vite.config.ts配置

export default defineConfig(({ command, mode }) => {
  //后端接口执行的时候会设置环境变量
  const input = process.env.BUILD_COMPONENT
    ? path.resolve(
        __dirname,
        `src/views/${process.env.BUILD_COMPONENT}View.vue`
      )
    : path.resolve(__dirname, "index.html");

  return {
    ....
    build: {
      rollupOptions: {
        input,
      },
    },
  };
});

在这里插入图片描述
在这里插入图片描述
上面只是打包aboutview相关的js和css,但是并没有html查看,现在为aboutview添加一个html和js作为入口文件用于汇众所有的相关的数据。
在这里插入图片描述
在这里插入图片描述
修改vite.config.ts文件内容,下面插件的功能主要是为了修改打包后的入口文件名为index。base: "./"该属性必须添加,否则打包后的目录进行本地预览会显示文件的情况,建议也把重命名的方法添加上,否则刷新可能还是会出现本地文件夹的情况

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "node:path";
import { fileURLToPath, URL } from "node:url";
import fs from "fs/promises";

//重命名打包后的about-view.html文件为index.html
const renamePlugin = (originalName, newName) => {
  return {
    name: "rename-output-html",
    closeBundle: async () => {
      const distPath = path.resolve(__dirname, "dist");
      const originalPath = path.join(distPath, originalName);
      const newPath = path.join(distPath, newName);
      try {
        await fs.rename(originalPath, newPath);
        console.log(`Renamed ${originalName} to ${newName}`);
      } catch (err) {
        console.error(`Failed to rename ${originalName} to ${newName}:`, err);
      }
    },
  };
};

export default defineConfig(() => {
  // 检测是否设置了环境变量BUILD_COMPONENT
  const isComponentBuild = process.env.BUILD_COMPONENT;

  return {
    .....
    base: "./",
    plugins: [vue(), renamePlugin("about-view.html", "index.html")],
    build: {
      rollupOptions: {
        input:
          isComponentBuild == "About"
            ? path.resolve(__dirname, "about-view.html")
            : path.resolve(__dirname, "index.html"),
        output: {
          entryFileNames: "assets/[name]-[hash].js", //打包后的index [name]代表使用组件原有名字 [hash]代表hash随机值
          chunkFileNames: "assets/[name]-[hash].js", //打包后各个组件名的js
          assetFileNames: "assets/[name]-[hash].[ext]", //打包后各个组件的css
        },
      },
      outDir: "dist",
    },
  };
});

不使用插件
在这里插入图片描述
使用插件
在这里插入图片描述

现在我们添加单独的路由模块(这里假设我们的AboutView中需要读取router.params中的某属性),因此在单独打包之前需要在AboutView.js中创建并引入路由模块,注意这里需要配置单独的路由地址。

import { createApp } from "vue";
import AboutView from "./AboutView.vue";

import { createRouter, createWebHistory } from "vue-router";

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: "/about",
      name: "about",
      component: AboutView,
    },
  ],
});

createApp(AboutView).use(router).mount("#app");

模拟请求传参返回随机数更新标题。这里route.params.id只是随便模拟的东西,无值,主要是验证route非空,未单独引入use(router)的情况下读取未undefined。

const route = useRoute();
const title = ref("This is an about page");
onMounted(() => {
  console.log(route);
  getDataAPI(route.params.id).then((res) => {
    title.value = res.data.title;
  });
});

然后将AboutView的文件单独打包,这里直接部署到宝塔的本地环境中。需要修改一些nginx的参数。文件上传到如下结构。
在这里插入图片描述
在这里插入图片描述
配置nginx

location / { 
		try_files $uri $uri/ /index.html last;
 		index index.html; 
} 
location /api {
        proxy_pass http://127.0.0.1:3001;
        rewrite ^/api/(.*)$ /$1 break;
}
    //下面选择性添加
 add_header 'Access-Control-Allow-Origin' '*' always; #允许来自所有的访问地址 add_header 'Access-Control-Allow-Credentials' 'true' always; #允许来自所有的访问地址 add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, token, platform' always; add_header 'Access-Control-Allow-Methods' 'GET,POST,PUT,DELETE,HEAD,OPTIONS' always; #允许来自所有的访问地址

运行单独打包的AboutView
在这里插入图片描述
可以正确访问接受数据更新
在这里插入图片描述


http://www.niftyadmin.cn/n/5535225.html

相关文章

d88888888

分析:v9999999999 vn输出n个n 先算出n的位数p 所以答案是nn*10的p次方n*10的2p次方.....n*10的(n-1)p次方 化简n*(10的0次方10的p次方10的2p次方.....10的(n-1)p次方) 后面为等比数列求和 …

前端三件套开发模版——产品介绍页面

今天有空,使用前端三件套html、css、js制作了一个非常简单的产品制作页面,与大家分享,希望可以满足大家应急的需求。本页面可以对产品进行“抢购”、对产品进行介绍,同时可以安排一张产品的高清大图,我也加入了页面的背…

数据结构底层之HashMap(面经篇1)

1 . 讲一下hashmap的数据结构 HashMap是一种基于哈希表实现的数据结构,通常用于关联键值对,其中键是唯一的,而值可以重复。在Java中,HashMap是java.util.Map接口的一个实现,它提供了快速的查找、插入和删除操作。 数据…

Ueditor中集成135编辑器

一、背景 在资讯项目平台运营过程中,资讯需要排版,一般都是在135编辑器排好以后,复制到平台中UEditor编辑器中,所以,他们建议集成一下135哈 二、了解135编辑器 开始调研了解135编辑器,发现人家就支持集成…

HarmonyOS ArkUi Tabs+TabContent+List实现tab吸顶功能

Demo效果 Entry Component struct StickyNestedScroll {State message: string Hello WorldState arr: number[] []scroller new Scroller()StyleslistCard() {.backgroundColor(Color.White).height(72).width("100%").borderRadius(12)}build() {Scroll(this.sc…

记录通过Cloudflare部署属于自己的docker镜像源

引言 由于最近国内无法正常拉取docker镜像,然而找了几个能用的docker镜像源发现拉取回来的docker镜像不是最新的版本,部署到Cloudflare里Workers 和 Pages,拉取docker 镜像成功,故记录部署过程。 部署服务 登录Cloudflare后&…

深入学习索引

1.索引的底层数据结构是什么?索引是如何实现的? 索引的底层数据结构是Btree(多路树) 当设置了字段为索引以后,底层会将字段上的数据使用BTree的数据结构存储在索引文件里 2.索引的优缺点 优点:索引字段作为条件查询更快 缺点&…

【字符串处理】【双指针】个人练习-Leetcode-777. Swap Adjacent in LR String

题目链接:https://leetcode.cn/problems/swap-adjacent-in-lr-string/description/ 题目大意:给两个字符串start, end,只包含XLR三种字符。可以进行一次操作将XL转换成LX或者将RX转换为XR,返回是否存在方法使得start能转换成end …