chore: Struct adjust

This commit is contained in:
草鞋没号 2021-02-18 16:11:08 +08:00
parent 48d5a92e6d
commit 7b472f36c6
9 changed files with 40 additions and 73 deletions

View File

@ -1,3 +1,4 @@
import { sep } from 'path'
import { get } from 'http' import { get } from 'http'
import { green } from 'chalk' import { green } from 'chalk'
@ -18,3 +19,23 @@ export function waitOn(arg0: { port: string | number; interval?: number; }) {
}, interval) }, interval)
}) })
} }
/** cjs2esm */
export function cjs2esm() {
return {
name: '@rollup/plugin-cjs2esm',
transform(code: string, filename: string) {
if (filename.includes(`${sep}node_modules${sep}`)) {
return code
}
const cjsRegexp = /(const|let|var)[\n\s]+(\w+)[\n\s]*=[\n\s]*require\(["|'](.+)["|']\)/g
const res = code.match(cjsRegexp)
if (res) {
// const Store = require('electron-store') -> import Store from 'electron-store'
code = code.replace(cjsRegexp, `import $2 from '$3'`)
}
return code
},
}
}

View File

@ -1,11 +1,10 @@
/** /**
* electron * electron
*/ */
require('dotenv').config({ path: join(__dirname, '../../.env') })
import { join } from 'path' import { join } from 'path'
import { app, BrowserWindow } from 'electron' import { app, BrowserWindow } from 'electron'
import dotenv from 'dotenv'
dotenv.config({ path: join(__dirname, '../../.env') })
let win: BrowserWindow let win: BrowserWindow

View File

@ -1,3 +1,4 @@
// @ts-nocheck
/** docoment 加载完成 */ /** docoment 加载完成 */
function domReady(...args) { function domReady(...args) {

View File

@ -2,12 +2,8 @@ import { createApp } from 'vue'
import App from './App.vue' import App from './App.vue'
// vite 会编译 import 的形式;所以 electron 及 node.js 内置模块用 require 形式 // vite 会编译 import 的形式;所以 electron 及 node.js 内置模块用 require 形式
const { ipcRenderer } = require('electron') const { ipcRenderer } = require('electron')
// import { store, isdev } from '/utils/index'
import './index.css' import './index.css'
console.log('ipcRenderer:', ipcRenderer) console.log('ipcRenderer:', ipcRenderer)
// console.log('Store', store)
// console.log('electron is dev', isdev)
createApp(App as any).mount('#app').$nextTick(window.ClosePreloadLoading) createApp(App).mount('#app').$nextTick(window.ClosePreloadLoading)

View File

@ -1,7 +0,0 @@
import store from './store'
const isdev = require('electron-is-dev')
export {
store,
isdev,
}

View File

@ -1,3 +0,0 @@
/*@replace = import Store from 'electron-store'*/const Store = require('electron-store')
export default new Store

View File

@ -11,16 +11,12 @@
"baseUrl": "./", "baseUrl": "./",
"strict": true, "strict": true,
"paths": { "paths": {
"/assets/*": ["src/render/assets/*"], "@/*": ["src/render/*"],
"/components/*": ["src/render/components/*"], "@root/*": ["./*"],
"/lib/*": ["src/render/lib/*"],
"/utils/*": ["src/render/utils/*"],
"/views/*": ["src/render/views/*"]
}, },
"allowSyntheticDefaultImports": true "allowSyntheticDefaultImports": true
}, },
"include": [ "include": [
"src/**/*", "src"
"typings/*.d.ts"
] ]
} }

View File

@ -1,71 +1,35 @@
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, sep } from 'path' import externals from 'rollup-plugin-node-externals'
import dotenv from 'dotenv' import { join } from 'path'
import { cjs2esm } from './script/utils'
dotenv.config({ path: join(__dirname, '.env') })
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, root: join(__dirname, 'src/render'),
// base: './',
server: { server: {
port: +process.env.PORT, port: +process.env.PORT,
}, },
resolve: { resolve: {
alias: { alias: {
'@': join(__dirname, 'src/render'),
'@root': __dirname, '@root': __dirname,
'@assets': join(__dirname, 'src/render/assets'),
'@components': join(__dirname, 'src/render/components'),
'@lib': join(__dirname, 'src/render/lib'),
'@utils': join(__dirname, 'src/render/utils'),
'@views': join(__dirname, 'src/render/views'),
}, },
}, },
optimizeDeps: { optimizeDeps: {
// 这里不加也没事,用 require 的形式就能避开 import 被编译成 /@modules/fs?import exclude: ['electron-store', 'electron']
// allowNodeBuiltins: ['electron-is-dev', 'electron-store', 'electron']
}, },
build: { build: {
outDir: join(__dirname, 'dist/render'), outDir: join(__dirname, 'dist/render'),
rollupOptions: { rollupOptions: {
output: { external: ['electron'],
// format: 'commonjs',
},
external: [
'crypto',
'assert',
'fs',
'util',
'os',
'events',
'child_process',
'http',
'https',
'path',
'electron',
],
plugins: [ plugins: [
{ externals(),
name: '@rollup/plugin-cjs2esm', // cjs2esm(),
transform(code, filename) {
if (filename.includes(`${sep}node_modules${sep}`)) {
return code
}
const cjsRegexp = /(const|let|var)[\n\s]+(\w+)[\n\s]*=[\n\s]*require\(["|'](.+)["|']\)/g
const res = code.match(cjsRegexp)
if (res) {
// const Store = require('electron-store') -> import Store from 'electron-store'
code = code.replace(cjsRegexp, `import $2 from '$3'`)
}
return code
},
}
], ],
}, },
}, },
}) })