1549469775 4 years ago
parent
commit
66032f3310
  1. 14
      .babelrc
  2. 14669
      package-lock.json
  3. 16
      package.json
  4. 56
      script/dev-runner.ts
  5. 115
      script/webpack/runMain.js
  6. 66
      script/webpack/webpack.main.config.js
  7. 8
      src/main/index.ts

14
.babelrc

@ -0,0 +1,14 @@
{
"comments": false,
"env": {
"main": {
"presets": [
["env", {
"targets": { "node": 7 }
}],
"stage-0"
]
}
},
"plugins": ["@babel/plugin-transform-runtime"]
}

14669
package-lock.json

File diff suppressed because it is too large

16
package.json

@ -4,10 +4,9 @@
"description": "description",
"main": "dist/electron/entry.js",
"scripts": {
"webpack": "node script/webpack/runMain.js",
"dev": "ts-node -r tsconfig-paths/register script/dev-runner --env=development --watch",
"build": "ts-node -r tsconfig-paths/register script/build --env=production && npm run package",
"devaa": "npm run dev:all",
"dev:all": "concurrently -n=vue,ele -c=green,blue \"npm run dev:vue\" \"npm run dev:ele\"",
"dev:vue": "vite",
@ -22,6 +21,10 @@
"author": "TopOne",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.14.8",
"@babel/plugin-transform-runtime": "^7.14.5",
"@babel/preset-env": "^7.14.8",
"@babel/preset-stage-0": "^7.8.3",
"@electron-forge/cli": "^6.0.0-beta.58",
"@electron-forge/maker-deb": "^6.0.0-beta.58",
"@electron-forge/maker-rpm": "^6.0.0-beta.58",
@ -34,15 +37,22 @@
"@rollup/plugin-typescript": "^8.2.3",
"@types/minimist": "^1.2.2",
"@types/node": "^15.14.3",
"babel-loader": "^8.2.2",
"babel-minify-webpack-plugin": "^0.3.1",
"cfonts": "^2.9.3",
"chalk": "^4.1.1",
"concurrently": "^6.2.0",
"dotenv": "^10.0.0",
"electron": "^13.1.7",
"execa": "^5.1.1",
"node-loader": "^2.0.0",
"ts-loader": "^9.2.4",
"ts-node": "^10.1.0",
"tsconfig-paths": "^3.10.1",
"vitejs-plugin-electron": "^0.1.3"
"vitejs-plugin-electron": "^0.1.3",
"webpack": "^5.46.0",
"webpack-cli": "^4.7.2",
"webpack-dev-server": "^3.11.2"
},
"dependencies": {
"@types/react": "^17.0.0",

56
script/dev-runner.ts

@ -12,6 +12,10 @@ import { exec, spawn, ChildProcess } from "child_process"
import { watch, rollup, OutputOptions } from "rollup"
import { waitOn } from "./utils"
import options from "./rollup.config"
const startMain = require("./webpack/runMain")
console.log(startMain);
// 注入环境变量
dotenv.config({ path: join(__dirname, "../.env") })
// 解析命令行参数
@ -55,7 +59,7 @@ function runVite() {
function runElectron() {
return new Promise((resolve, reject) => {
let electronProcess = spawn(electron as unknown as string, [join(__dirname, `../${main}`)], {
let electronProcess = spawn(electron as unknown as string, [join(__dirname, `../${main}`), '--inspect=5858'], {
stdio: "pipe",
env: Object.assign(process.env, { NODE_ENV: argv.env }),
})
@ -70,16 +74,9 @@ function runElectron() {
;(async () => {
let vitePorcess = await runVite()
console.log('[vite]', chalk.green(`vite ready.`))
const watcher = watch(opts)
let child: ChildProcess | null
let manualRestart = false
watcher.on("change", filename => {
const log = chalk.green(`change -- ${filename}`)
console.log(TAG, log)
})
watcher.on("event",async ev => {
if (ev.code === "END") {
startMain(async ()=>{
if (child && child.kill) {
manualRestart = true
child.kill()
@ -104,8 +101,43 @@ function runElectron() {
child.kill()
process.exit()
}
} else if (ev.code === "ERROR") {
console.log(ev.error)
}
})
// const watcher = watch(opts)
// let child: ChildProcess | null
// let manualRestart = false
// watcher.on("change", filename => {
// const log = chalk.green(`change -- ${filename}`)
// console.log(TAG, log)
// })
// watcher.on("event",async ev => {
// if (ev.code === "END") {
// if (child && child.kill) {
// manualRestart = true
// child.kill()
// child = null
// setTimeout(() => {
// manualRestart = false
// }, 5000)
// }
// try {
// child = await runElectron()
// console.log('[electron]', chalk.green(`electron ready.`))
// child.on("close", () => {
// if (!manualRestart) {
// vitePorcess.kill()
// child.kill()
// process.exit()
// }
// })
// } catch (ev) {
// console.log(ev.error)
// vitePorcess.kill()
// child.kill()
// process.exit()
// }
// } else if (ev.code === "ERROR") {
// console.log(ev.error)
// }
// })
})()

115
script/webpack/runMain.js

@ -0,0 +1,115 @@
const chalk = require('chalk')
const electron = require('electron')
const path = require('path')
const { say } = require('cfonts')
const { spawn } = require('child_process')
const webpack = require('webpack')
// const WebpackDevServer = require('webpack-dev-server')
// const webpackHotMiddleware = require('webpack-hot-middleware')
const mainConfig = require('./webpack.main.config')
// const rendererConfig = require('./webpack.renderer.config')
let electronProcess = null
let manualRestart = false
// let hotMiddleware
function logStats (proc, data) {
let log = ''
log += chalk.yellow.bold(`${proc} Process ${new Array((19 - proc.length) + 1).join('-')}`)
log += '\n\n'
if (typeof data === 'object') {
data.toString({
colors: true,
chunks: false
}).split(/\r?\n/).forEach(line => {
log += ' ' + line + '\n'
})
} else {
log += ` ${data}\n`
}
log += '\n' + chalk.yellow.bold(`${new Array(28 + 1).join('-')}`) + '\n'
console.log(log)
}
module.exports = function startMain (callback) {
return new Promise((resolve, reject) => {
mainConfig.entry.main = [path.join(__dirname, '../../src/main/index.ts')]//.concat(mainConfig.entry.main)
mainConfig.mode = 'development'
const compiler = webpack(mainConfig)
compiler.hooks.watchRun.tapAsync('watch-run', (compilation, done) => {
logStats('Main', chalk.white.bold('compiling...'))
// hotMiddleware.publish({ action: 'compiling' })
done()
})
compiler.watch({}, (err, stats) => {
if (err) {
console.log(err)
return
}
logStats('Main', stats)
callback&&callback()
resolve()
})
})
}
function electronLog (data, color) {
let log = ''
data = data.toString().split(/\r?\n/)
data.forEach(line => {
log += ` ${line}\n`
})
if (/[0-9A-z]+/.test(log)) {
console.log(
chalk[color].bold('┏ Electron -------------------') +
'\n\n' +
log +
chalk[color].bold('┗ ----------------------------') +
'\n'
)
}
}
function greeting () {
const cols = process.stdout.columns
let text = ''
if (cols > 104) text = 'electron-vue'
else if (cols > 76) text = 'electron-|vue'
else text = false
if (text) {
say(text, {
colors: ['yellow'],
font: 'simple3d',
space: false
})
} else console.log(chalk.yellow.bold('\n electron-vue'))
console.log(chalk.blue(' getting ready...') + '\n')
}
// function init () {
// greeting()
// startMain()
// Promise.all([startRenderer(), startMain()])
// .then(() => {
// startElectron()
// })
// .catch(err => {
// console.error(err)
// })
// }
// init()

66
script/webpack/webpack.main.config.js

@ -0,0 +1,66 @@
'use strict'
process.env.BABEL_ENV = 'main'
const path = require('path')
const { dependencies } = require('../../package.json')
const webpack = require('webpack')
const MinifyPlugin = require("babel-minify-webpack-plugin")
let mainConfig = {
entry: {
main: path.join(__dirname, '../../src/main/index.ts')
},
externals: [
...Object.keys(dependencies || {})
],
module: {
rules: [
// { test: /\.ts?$/, loader: "ts-loader", exclude: /node_modules/ },
{
test: /\.js$/,
use: ['babel-loader'],
exclude: /node_modules/
},
{
test: /\.node$/,
use: 'node-loader'
}
]
},
node: {
__dirname: process.env.NODE_ENV !== 'production',
__filename: process.env.NODE_ENV !== 'production'
},
output: {
filename: 'entry.js', // [name]
libraryTarget: 'commonjs2',
path: path.join(__dirname, '../../dist/electron')
},
plugins: [
new webpack.NoEmitOnErrorsPlugin()
],
resolve: {
alias: {
"@": path.join(__dirname, "../../src/render"),
"@render": path.join(__dirname, "../../src/render"),
"@main": path.join(__dirname, "../../src/main"),
"@src": path.join(__dirname, "../../src"),
"@root": path.join(__dirname, "../../"),
},
extensions: ['.js', '.json', '.node', '.ts']
},
target: 'electron-main'
}
/**
* Adjust mainConfig for production settings
*/
if (process.env.NODE_ENV === 'production') {
mainConfig.plugins.push(
new MinifyPlugin()
)
}
module.exports = mainConfig

8
src/main/index.ts

@ -1,7 +1,7 @@
/**
* electron
*/
import "@src/common/patch"
// import "@src/common/patch"
import * as dotenv from "dotenv"
const { join } = require("path")
dotenv.config({ path: join(__dirname, "../../.env") })
@ -10,14 +10,14 @@ const { app, BrowserWindow } = require("electron")
let win
function createWin() {
// 创建浏览器窗口dsa
// 创建浏览器窗口dsaasdad
win = new BrowserWindow({
width: 1024,
height: 768,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
preload: join(__dirname, "../../src/preload/index.js"),
// contextIsolation: false,
// preload: join(__dirname, "../../src/preload/index.js"),
},
})

Loading…
Cancel
Save