From 7b472f36c6f64211a77054b33e6cd8482fc6826c 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: Thu, 18 Feb 2021 16:11:08 +0800 Subject: [PATCH] chore: Struct adjust --- script/utils.ts | 21 ++++++++++++++ src/main/index.ts | 5 ++-- src/preload/index.js | 1 + src/render/main.ts | 6 +--- src/render/typings.d.ts | 2 +- src/render/utils/index.ts | 7 ----- src/render/utils/store.ts | 3 -- tsconfig.json | 10 ++----- vite.config.ts | 58 ++++++++------------------------------- 9 files changed, 40 insertions(+), 73 deletions(-) delete mode 100644 src/render/utils/index.ts delete mode 100644 src/render/utils/store.ts diff --git a/script/utils.ts b/script/utils.ts index ecbdbea..4ac0438 100644 --- a/script/utils.ts +++ b/script/utils.ts @@ -1,3 +1,4 @@ +import { sep } from 'path' import { get } from 'http' import { green } from 'chalk' @@ -18,3 +19,23 @@ export function waitOn(arg0: { port: string | number; interval?: number; }) { }, 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 + }, + } +} diff --git a/src/main/index.ts b/src/main/index.ts index 22aaaca..9000d8c 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -1,11 +1,10 @@ /** * electron 主文件 */ +require('dotenv').config({ path: join(__dirname, '../../.env') }) + import { join } from 'path' import { app, BrowserWindow } from 'electron' -import dotenv from 'dotenv' - -dotenv.config({ path: join(__dirname, '../../.env') }) let win: BrowserWindow diff --git a/src/preload/index.js b/src/preload/index.js index 04aecee..9bcc03a 100644 --- a/src/preload/index.js +++ b/src/preload/index.js @@ -1,3 +1,4 @@ +// @ts-nocheck /** docoment 加载完成 */ function domReady(...args) { diff --git a/src/render/main.ts b/src/render/main.ts index 16d0c9d..54e49ed 100644 --- a/src/render/main.ts +++ b/src/render/main.ts @@ -2,12 +2,8 @@ import { createApp } from 'vue' import App from './App.vue' // vite 会编译 import 的形式;所以 electron 及 node.js 内置模块用 require 形式 const { ipcRenderer } = require('electron') -// import { store, isdev } from '/utils/index' - import './index.css' 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) diff --git a/src/render/typings.d.ts b/src/render/typings.d.ts index 740f412..77842c9 100644 --- a/src/render/typings.d.ts +++ b/src/render/typings.d.ts @@ -7,5 +7,5 @@ declare module '*.jpg' interface Window { /** 关闭预加载动画 */ - ClosePreloadLoading: () => void + ClosePreloadLoading: () => void } diff --git a/src/render/utils/index.ts b/src/render/utils/index.ts deleted file mode 100644 index 2b8d079..0000000 --- a/src/render/utils/index.ts +++ /dev/null @@ -1,7 +0,0 @@ -import store from './store' -const isdev = require('electron-is-dev') - -export { - store, - isdev, -} diff --git a/src/render/utils/store.ts b/src/render/utils/store.ts deleted file mode 100644 index 1aba990..0000000 --- a/src/render/utils/store.ts +++ /dev/null @@ -1,3 +0,0 @@ -/*@replace = import Store from 'electron-store'*/const Store = require('electron-store') - -export default new Store diff --git a/tsconfig.json b/tsconfig.json index 5a17f94..3a41ab3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,16 +11,12 @@ "baseUrl": "./", "strict": true, "paths": { - "/assets/*": ["src/render/assets/*"], - "/components/*": ["src/render/components/*"], - "/lib/*": ["src/render/lib/*"], - "/utils/*": ["src/render/utils/*"], - "/views/*": ["src/render/views/*"] + "@/*": ["src/render/*"], + "@root/*": ["./*"], }, "allowSyntheticDefaultImports": true }, "include": [ - "src/**/*", - "typings/*.d.ts" + "src" ] } diff --git a/vite.config.ts b/vite.config.ts index 07b8eab..836a8d8 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,71 +1,35 @@ +require('dotenv').config({ path: join(__dirname, '.env') }) + import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' -import { join, sep } from 'path' -import dotenv from 'dotenv' - -dotenv.config({ path: join(__dirname, '.env') }) -const root = join(__dirname, 'src/render') +import externals from 'rollup-plugin-node-externals' +import { join } from 'path' +import { cjs2esm } from './script/utils' // https://vitejs.dev/config/ export default defineConfig({ plugins: [vue()], - root, - // base: './', + root: join(__dirname, 'src/render'), server: { port: +process.env.PORT, }, resolve: { alias: { + '@': join(__dirname, 'src/render'), '@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: { - // 这里不加也没事,用 require 的形式就能避开 import 被编译成 /@modules/fs?import - // allowNodeBuiltins: ['electron-is-dev', 'electron-store', 'electron'] + exclude: ['electron-store', 'electron'] }, build: { outDir: join(__dirname, 'dist/render'), rollupOptions: { - output: { - // format: 'commonjs', - }, - external: [ - 'crypto', - 'assert', - 'fs', - 'util', - 'os', - 'events', - 'child_process', - 'http', - 'https', - 'path', - 'electron', - ], + external: ['electron'], plugins: [ - { - name: '@rollup/plugin-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 - }, - } + externals(), + // cjs2esm(), ], }, }, }) -