diff --git a/script/plugins/index.ts b/script/plugins/index.ts index 801dae0..de84c27 100644 --- a/script/plugins/index.ts +++ b/script/plugins/index.ts @@ -1,4 +1,3 @@ -import fs from 'fs' import path from 'path' import acorn from 'acorn' import { Plugin as RollupPlugin } from 'rollup' @@ -29,7 +28,7 @@ export function cjs2esm() { } /** esm2cjs */ -export function esm2cjs(needConvertModules: string[]): VitePlugin { +export function esm2cjs(moduleList: string[]): VitePlugin { const filter = { include: (id: string) => vue_js_ts_extensions.includes(path.parse(id).ext) } @@ -47,9 +46,8 @@ export function esm2cjs(needConvertModules: string[]): VitePlugin { let codeRet = code node.body.reverse().forEach((item) => { - console.log(item) if (item.type !== 'ImportDeclaration') return - if (!needConvertModules.includes(item.source.value)) return // 跳过不要转换的模块 + if (!moduleList.includes(item.source.value)) return // 跳过不要转换的模块 const statr = codeRet.substring(0, item.start) const end = codeRet.substring(item.end) @@ -80,8 +78,6 @@ const { ${modules.join(', ')} } = ${deftModule}${end}` } }) - fs.writeFileSync(path.join(__dirname, '.tmp/') + `${parsed.name + parsed.ext}.js`, codeRet) - return codeRet } }, diff --git a/src/render/main.ts b/src/render/main.ts index 1051be6..f1eb985 100644 --- a/src/render/main.ts +++ b/src/render/main.ts @@ -6,7 +6,6 @@ import { ipcRenderer } from 'electron' 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' })) diff --git a/vite.config.ts b/vite.config.ts index f3548b9..37fa13a 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -13,49 +13,61 @@ import { const root = join(__dirname, 'src/render') // https://vitejs.dev/config/ -export default defineConfig({ - plugins: [ - vue(), - esm2cjs([ - 'electron', - 'electron-store', - ]), - ], - root, - base: './', // index.html 中静态资源加载位置 - server: { - port: +process.env.PORT, - }, - resolve: { - alias: { - '@render': join(__dirname, 'src/render'), - '@main': join(__dirname, 'src/main'), - '@src': join(__dirname, 'src'), - '@root': __dirname, +export default defineConfig(env => { + const isserve = env.command === 'serve' + + return { + plugins: [ + vue(), + ...(isserve ? [ + /** + * !!! 开发期将 electron 及相关模块转换成 commonjs 避开 vite 处理 + */ + esm2cjs([ + 'electron', + 'electron-store', + ]), + ] : []), + ], + root, + base: './', // index.html 中静态资源加载位置 + server: { + port: +process.env.PORT, }, - }, - optimizeDeps: { - exclude: ['electron'], - }, - build: { - outDir: join(__dirname, 'dist/render'), - emptyOutDir: true, - minify: false, - commonjsOptions: {}, - assetsDir: '', // 相对路径 加载问题 - sourcemap: true, - rollupOptions: { - plugins: [ - // typescript({ module: 'ESNext' }), - // ensureCwdCrrect(join(root, 'main.ts')), - ], - external: [ - ...builtins(), - 'electron', - ], - output: { - format: 'cjs', + resolve: { + alias: { + '@render': join(__dirname, 'src/render'), + '@main': join(__dirname, 'src/main'), + '@src': join(__dirname, 'src'), + '@root': __dirname, }, }, - }, + optimizeDeps: { + exclude: [ + 'electron', + 'electron-store', // optional + ], + }, + build: { + outDir: join(__dirname, 'dist/render'), + emptyOutDir: true, + minify: false, + commonjsOptions: {}, + assetsDir: '', // 相对路径 加载问题 + sourcemap: true, + rollupOptions: { + plugins: [ + // typescript({ module: 'ESNext' }), + // ensureCwdCrrect(join(root, 'main.ts')), + ], + external: [ + ...builtins(), + 'electron', + ], + output: { + format: 'cjs', + }, + }, + }, + } })