!!! ensure cwd crrect

This commit is contained in:
草鞋没号 2021-05-15 16:30:59 +08:00
parent 030b7a5f3f
commit f6f363364d
3 changed files with 37 additions and 15 deletions

View File

@ -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))

View File

@ -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')

View File

@ -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(),