!!! 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 { builtinModules } from 'module'
import { sep } from 'path' import { sep, join } from 'path'
import { get } from 'http' import { get } from 'http'
import { green } from 'chalk' import { green } from 'chalk'
import { Plugin } from 'rollup'
/** 轮询监听 vite 启动 */ /** 轮询监听 vite 启动 */
export function waitOn(arg0: { port: string | number; interval?: number; }) { 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)) 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 { createApp } from 'vue'
import App from './App.vue' import App from './App.vue'
import fs = require('fs')
import { ipcRenderer } from 'electron' import { ipcRenderer } from 'electron'
// import Store from 'electron-store' import Store from 'electron-store'
import './index.css' import './index.css'
console.log('fs', fs)
console.log('ipcRenderer:', ipcRenderer) console.log('ipcRenderer:', ipcRenderer)
// console.log('electron-store', new Store({ name: 'electron-vue' })) console.log('electron-store', new Store({ name: 'electron-vue' }))
createApp(App) createApp(App)
.mount('#app') .mount('#app')

View File

@ -3,13 +3,15 @@ require('dotenv').config({ path: join(__dirname, '.env') })
import { defineConfig } from 'vite' import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue' import vue from '@vitejs/plugin-vue'
import { join } from 'path' import { join } from 'path'
import typescript from '@rollup/plugin-typescript' // import typescript from '@rollup/plugin-typescript'
import { builtins } from './script/utils' import { builtins, ensureCwdCrrect } from './script/utils'
const root = join(__dirname, 'src/render')
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default defineConfig({ export default defineConfig({
plugins: [vue()], plugins: [vue()],
root: join(__dirname, 'src/render'), root,
base: './', // index.html 中静态资源加载位置 base: './', // index.html 中静态资源加载位置
server: { server: {
port: +process.env.PORT, port: +process.env.PORT,
@ -24,18 +26,13 @@ export default defineConfig({
outDir: join(__dirname, 'dist/render'), outDir: join(__dirname, 'dist/render'),
emptyOutDir: true, emptyOutDir: true,
minify: false, minify: false,
commonjsOptions: { commonjsOptions: {},
dynamicRequireTargets: [
join(__dirname, 'package.json'),
],
},
assetsDir: '', // 相对路径 加载问题 assetsDir: '', // 相对路径 加载问题
sourcemap: true, sourcemap: true,
rollupOptions: { rollupOptions: {
plugins: [ plugins: [
// typescript({ // typescript({ module: 'ESNext' }),
// module: 'ESNext', // ensureCwdCrrect(join(root, 'main.ts')),
// }),
], ],
external: [ external: [
...builtins(), ...builtins(),