From f6f363364dd9200e27e95d915d14f2bcc00608a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=89=E9=9E=8B=E6=B2=A1=E5=8F=B7?= <308487730@qq.com> Date: Sat, 15 May 2021 16:30:59 +0800 Subject: [PATCH] !!! ensure cwd crrect --- script/utils.ts | 22 ++++++++++++++++++++-- src/render/main.ts | 11 +++++++++-- vite.config.ts | 19 ++++++++----------- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/script/utils.ts b/script/utils.ts index 67b4f86..666f1ed 100644 --- a/script/utils.ts +++ b/script/utils.ts @@ -1,7 +1,8 @@ import { builtinModules } from 'module' -import { sep } from 'path' +import { sep, join } from 'path' import { get } from 'http' import { green } from 'chalk' +import { Plugin } from 'rollup' /** 轮询监听 vite 启动 */ export function waitOn(arg0: { port: string | number; interval?: number; }) { @@ -41,5 +42,22 @@ export function cjs2esm() { } } -/** node.js 内置模块 */ +/** ensure cwd crrect */ +export function ensureCwdCrrect(filename: string): Plugin { + return { + name: 'cxmh:ensure-cwd-crrect', + transform(code, id) { + if (id === filename) { + return ` +// !!! ensure cwd crrect +process.chdir(__dirname) + +${code} +` + } + }, + } +} + +/** node.js builtins module */ export const builtins = () => builtinModules.filter(x => !/^_|^(internal|v8|node-inspect)\/|\//.test(x)) diff --git a/src/render/main.ts b/src/render/main.ts index 68b874b..5d4e47b 100644 --- a/src/render/main.ts +++ b/src/render/main.ts @@ -1,11 +1,18 @@ +/** + * !!! ensure cwd crrect + */ +process.chdir(__dirname) + import { createApp } from 'vue' import App from './App.vue' +import fs = require('fs') import { ipcRenderer } from 'electron' -// import Store from 'electron-store' +import Store from 'electron-store' import './index.css' +console.log('fs', fs) console.log('ipcRenderer:', ipcRenderer) -// console.log('electron-store', new Store({ name: 'electron-vue' })) +console.log('electron-store', new Store({ name: 'electron-vue' })) createApp(App) .mount('#app') diff --git a/vite.config.ts b/vite.config.ts index e9835cd..86081b9 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -3,13 +3,15 @@ require('dotenv').config({ path: join(__dirname, '.env') }) import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import { join } from 'path' -import typescript from '@rollup/plugin-typescript' -import { builtins } from './script/utils' +// import typescript from '@rollup/plugin-typescript' +import { builtins, ensureCwdCrrect } from './script/utils' + +const root = join(__dirname, 'src/render') // https://vitejs.dev/config/ export default defineConfig({ plugins: [vue()], - root: join(__dirname, 'src/render'), + root, base: './', // index.html 中静态资源加载位置 server: { port: +process.env.PORT, @@ -24,18 +26,13 @@ export default defineConfig({ outDir: join(__dirname, 'dist/render'), emptyOutDir: true, minify: false, - commonjsOptions: { - dynamicRequireTargets: [ - join(__dirname, 'package.json'), - ], - }, + commonjsOptions: {}, assetsDir: '', // 相对路径 加载问题 sourcemap: true, rollupOptions: { plugins: [ - // typescript({ - // module: 'ESNext', - // }), + // typescript({ module: 'ESNext' }), + // ensureCwdCrrect(join(root, 'main.ts')), ], external: [ ...builtins(),