electron-vite / vite-plugin-electron

:electron: Electron⚡️Vite core repo
https://github.com/electron-vite
MIT License
689 stars 56 forks source link

How do I import other ts files in version 0.28 /electron/main/index.ts | 如何在0.28版本 /electron/main/index.ts 中引入其他ts文件 #240

Closed SanGongHappy closed 5 hours ago

SanGongHappy commented 6 months ago

I use the following code in vite.config.ts, which causes my ts file to generate mjs files, but my other ts files in the folder do not generate mjs files | 我在vite.config.ts 中使用如下代码,会使我的ts文件会生成mjs文件,但是我在文件夹中的其他ts文件不生成mjs文件


import fs from "node:fs";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
import pkg from "./package.json";
import electron from "vite-plugin-electron";
import renderer from "vite-plugin-electron-renderer";

import { loadEnv } from "vite";
import { fileURLToPath } from "node:url";

// 按需导入
// import AutoImport from 'unplugin-auto-import/vite'

// https://vitejs.dev/config/
export default defineConfig(({ command }) => {
  fs.rmSync("dist-electron", { recursive: true, force: true });

  const isServe = command === "serve";
  const isBuild = command === "build";
  const sourcemap = isServe || !!process.env.VSCODE_DEBUG;

  return {
    base: "./",
    plugins: [
      vue(),
      electron([
        {
          entry: "electron/main/index.ts",
          onstart(options) {
            if (process.env.VSCODE_DEBUG) {
              /* eslint-disable no-console */
              console.log("[startup] Electron App");
            } else {
              options.startup();
            }
          },

          vite: {
            build: {
              outDir: "dist-electron/main",
              rollupOptions: {
                external: Object.keys(
                  "dependencies" in pkg ? pkg.dependencies : {},
                ),
                output: {
                  format: "esm",
                  entryFileNames: `[name].mjs`,
                },
              },
            },
          },
        },

        {
          entry: "electron/preload/index.ts",
          onstart(options) {
            options.reload();
          },
          vite: {
            build: {
              outDir: "dist-electron/preload",
              rollupOptions: {
                external: Object.keys(
                  "dependencies" in pkg ? pkg.dependencies : {},
                ),
                output: {
                  format: "esm",
                  entryFileNames: `[name].mjs`,
                },
              },
            },
          },
        },
      ]),
      renderer({
        resolve: {
          serialport: { type: "cjs" },
          got: { type: "esm" },
        },
      }),
    ],
  };
});
yejimeiming commented 1 month ago

entry 支持多文件入口,参考 Vite 文档 👉🏻 https://vitejs.dev/guide/build.html#library-mode

SanGongHappy commented 1 month ago

有没有方式,批量转换 ts 文件到 mjs,因为开发者对mjs实际上是不需要的,应该专注于ts文件的逻辑,且一但文件多了以后,这种方式会很麻烦

yejimeiming commented 5 hours ago

你可能直接使用 esbuild 或者 tsc 更合适,Vite 是个 bundler 不是 transformer