Skip to content

Commit 9624584

Browse files
committed
Initial revision
0 parents  commit 9624584

30 files changed

+1741
-0
lines changed

.babelrc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"comments": false,
3+
"env": {
4+
"main": {
5+
"presets": [
6+
["env", {
7+
"targets": { "node": 7 }
8+
}],
9+
"stage-0"
10+
]
11+
},
12+
"renderer": {
13+
"presets": [
14+
["env", {
15+
"modules": false
16+
}],
17+
"stage-0"
18+
]
19+
},
20+
"web": {
21+
"presets": [
22+
["env", {
23+
"modules": false
24+
}],
25+
"stage-0"
26+
]
27+
}
28+
},
29+
"plugins": ["transform-runtime"]
30+
}

.electron-vue/build.js

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
'use strict'
2+
3+
process.env.NODE_ENV = 'production'
4+
5+
const { say } = require('cfonts')
6+
const chalk = require('chalk')
7+
const del = require('del')
8+
const { spawn } = require('child_process')
9+
const webpack = require('webpack')
10+
const Multispinner = require('multispinner')
11+
12+
13+
const mainConfig = require('./webpack.main.config')
14+
const rendererConfig = require('./webpack.renderer.config')
15+
const webConfig = require('./webpack.web.config')
16+
17+
const doneLog = chalk.bgGreen.white(' DONE ') + ' '
18+
const errorLog = chalk.bgRed.white(' ERROR ') + ' '
19+
const okayLog = chalk.bgBlue.white(' OKAY ') + ' '
20+
const isCI = process.env.CI || false
21+
22+
if (process.env.BUILD_TARGET === 'clean') clean()
23+
else if (process.env.BUILD_TARGET === 'web') web()
24+
else build()
25+
26+
function clean () {
27+
del.sync(['build/*', '!build/icons', '!build/icons/icon.*'])
28+
console.log(`\n${doneLog}\n`)
29+
process.exit()
30+
}
31+
32+
function build () {
33+
greeting()
34+
35+
del.sync(['dist/electron/*', '!.gitkeep'])
36+
37+
const tasks = ['main', 'renderer']
38+
const m = new Multispinner(tasks, {
39+
preText: 'building',
40+
postText: 'process'
41+
})
42+
43+
let results = ''
44+
45+
m.on('success', () => {
46+
process.stdout.write('\x1B[2J\x1B[0f')
47+
console.log(`\n\n${results}`)
48+
console.log(`${okayLog}take it away ${chalk.yellow('`electron-builder`')}\n`)
49+
process.exit()
50+
})
51+
52+
pack(mainConfig).then(result => {
53+
results += result + '\n\n'
54+
m.success('main')
55+
}).catch(err => {
56+
m.error('main')
57+
console.log(`\n ${errorLog}failed to build main process`)
58+
console.error(`\n${err}\n`)
59+
process.exit(1)
60+
})
61+
62+
pack(rendererConfig).then(result => {
63+
results += result + '\n\n'
64+
m.success('renderer')
65+
}).catch(err => {
66+
m.error('renderer')
67+
console.log(`\n ${errorLog}failed to build renderer process`)
68+
console.error(`\n${err}\n`)
69+
process.exit(1)
70+
})
71+
}
72+
73+
function pack (config) {
74+
return new Promise((resolve, reject) => {
75+
webpack(config, (err, stats) => {
76+
if (err) reject(err.stack || err)
77+
else if (stats.hasErrors()) {
78+
let err = ''
79+
80+
stats.toString({
81+
chunks: false,
82+
colors: true
83+
})
84+
.split(/\r?\n/)
85+
.forEach(line => {
86+
err += ` ${line}\n`
87+
})
88+
89+
reject(err)
90+
} else {
91+
resolve(stats.toString({
92+
chunks: false,
93+
colors: true
94+
}))
95+
}
96+
})
97+
})
98+
}
99+
100+
function web () {
101+
del.sync(['dist/web/*', '!.gitkeep'])
102+
webpack(webConfig, (err, stats) => {
103+
if (err || stats.hasErrors()) console.log(err)
104+
105+
console.log(stats.toString({
106+
chunks: false,
107+
colors: true
108+
}))
109+
110+
process.exit()
111+
})
112+
}
113+
114+
function greeting () {
115+
const cols = process.stdout.columns
116+
let text = ''
117+
118+
if (cols > 85) text = 'lets-build'
119+
else if (cols > 60) text = 'lets-|build'
120+
else text = false
121+
122+
if (text && !isCI) {
123+
say(text, {
124+
colors: ['yellow'],
125+
font: 'simple3d',
126+
space: false
127+
})
128+
} else console.log(chalk.yellow.bold('\n lets-build'))
129+
console.log()
130+
}

.electron-vue/dev-client.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const hotClient = require('webpack-hot-middleware/client?noInfo=true&reload=true')
2+
3+
hotClient.subscribe(event => {
4+
/**
5+
* Reload browser when HTMLWebpackPlugin emits a new index.html
6+
*
7+
* Currently disabled until jantimon/html-webpack-plugin#680 is resolved.
8+
* https://github.com/SimulatedGREG/electron-vue/issues/437
9+
* https://github.com/jantimon/html-webpack-plugin/issues/680
10+
*/
11+
// if (event.action === 'reload') {
12+
// window.location.reload()
13+
// }
14+
15+
/**
16+
* Notify `mainWindow` when `main` process is compiling,
17+
* giving notice for an expected reload of the `electron` process
18+
*/
19+
if (event.action === 'compiling') {
20+
document.body.innerHTML += `
21+
<style>
22+
#dev-client {
23+
background: #4fc08d;
24+
border-radius: 4px;
25+
bottom: 20px;
26+
box-shadow: 0 4px 5px 0 rgba(0, 0, 0, 0.14), 0 1px 10px 0 rgba(0, 0, 0, 0.12), 0 2px 4px -1px rgba(0, 0, 0, 0.3);
27+
color: #fff;
28+
font-family: 'Source Sans Pro', sans-serif;
29+
left: 20px;
30+
padding: 8px 12px;
31+
position: absolute;
32+
}
33+
</style>
34+
35+
<div id="dev-client">
36+
Compiling Main Process...
37+
</div>
38+
`
39+
}
40+
})

.electron-vue/dev-runner.js

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
'use strict'
2+
3+
const chalk = require('chalk')
4+
const electron = require('electron')
5+
const path = require('path')
6+
const { say } = require('cfonts')
7+
const { spawn } = require('child_process')
8+
const webpack = require('webpack')
9+
const WebpackDevServer = require('webpack-dev-server')
10+
const webpackHotMiddleware = require('webpack-hot-middleware')
11+
12+
const mainConfig = require('./webpack.main.config')
13+
const rendererConfig = require('./webpack.renderer.config')
14+
15+
let electronProcess = null
16+
let manualRestart = false
17+
let hotMiddleware
18+
19+
function logStats (proc, data) {
20+
let log = ''
21+
22+
log += chalk.yellow.bold(`┏ ${proc} Process ${new Array((19 - proc.length) + 1).join('-')}`)
23+
log += '\n\n'
24+
25+
if (typeof data === 'object') {
26+
data.toString({
27+
colors: true,
28+
chunks: false
29+
}).split(/\r?\n/).forEach(line => {
30+
log += ' ' + line + '\n'
31+
})
32+
} else {
33+
log += ` ${data}\n`
34+
}
35+
36+
log += '\n' + chalk.yellow.bold(`┗ ${new Array(28 + 1).join('-')}`) + '\n'
37+
38+
console.log(log)
39+
}
40+
41+
function startRenderer () {
42+
return new Promise((resolve, reject) => {
43+
rendererConfig.entry.renderer = [path.join(__dirname, 'dev-client')].concat(rendererConfig.entry.renderer)
44+
45+
const compiler = webpack(rendererConfig)
46+
hotMiddleware = webpackHotMiddleware(compiler, {
47+
log: false,
48+
heartbeat: 2500
49+
})
50+
51+
compiler.plugin('compilation', compilation => {
52+
compilation.plugin('html-webpack-plugin-after-emit', (data, cb) => {
53+
hotMiddleware.publish({ action: 'reload' })
54+
cb()
55+
})
56+
})
57+
58+
compiler.plugin('done', stats => {
59+
logStats('Renderer', stats)
60+
})
61+
62+
const server = new WebpackDevServer(
63+
compiler,
64+
{
65+
contentBase: path.join(__dirname, '../'),
66+
quiet: true,
67+
before (app, ctx) {
68+
app.use(hotMiddleware)
69+
ctx.middleware.waitUntilValid(() => {
70+
resolve()
71+
})
72+
}
73+
}
74+
)
75+
76+
server.listen(9080)
77+
})
78+
}
79+
80+
function startMain () {
81+
return new Promise((resolve, reject) => {
82+
mainConfig.entry.main = [path.join(__dirname, '../src/main/index.dev.js')].concat(mainConfig.entry.main)
83+
84+
const compiler = webpack(mainConfig)
85+
86+
compiler.plugin('watch-run', (compilation, done) => {
87+
logStats('Main', chalk.white.bold('compiling...'))
88+
hotMiddleware.publish({ action: 'compiling' })
89+
done()
90+
})
91+
92+
compiler.watch({}, (err, stats) => {
93+
if (err) {
94+
console.log(err)
95+
return
96+
}
97+
98+
logStats('Main', stats)
99+
100+
if (electronProcess && electronProcess.kill) {
101+
manualRestart = true
102+
process.kill(electronProcess.pid)
103+
electronProcess = null
104+
startElectron()
105+
106+
setTimeout(() => {
107+
manualRestart = false
108+
}, 5000)
109+
}
110+
111+
resolve()
112+
})
113+
})
114+
}
115+
116+
function startElectron () {
117+
electronProcess = spawn(electron, ['--inspect=5858', '.'])
118+
119+
electronProcess.stdout.on('data', data => {
120+
electronLog(data, 'blue')
121+
})
122+
electronProcess.stderr.on('data', data => {
123+
electronLog(data, 'red')
124+
})
125+
126+
electronProcess.on('close', () => {
127+
if (!manualRestart) process.exit()
128+
})
129+
}
130+
131+
function electronLog (data, color) {
132+
let log = ''
133+
data = data.toString().split(/\r?\n/)
134+
data.forEach(line => {
135+
log += ` ${line}\n`
136+
})
137+
if (/[0-9A-z]+/.test(log)) {
138+
console.log(
139+
chalk[color].bold('┏ Electron -------------------') +
140+
'\n\n' +
141+
log +
142+
chalk[color].bold('┗ ----------------------------') +
143+
'\n'
144+
)
145+
}
146+
}
147+
148+
function greeting () {
149+
const cols = process.stdout.columns
150+
let text = ''
151+
152+
if (cols > 104) text = 'electron-vue'
153+
else if (cols > 76) text = 'electron-|vue'
154+
else text = false
155+
156+
if (text) {
157+
say(text, {
158+
colors: ['yellow'],
159+
font: 'simple3d',
160+
space: false
161+
})
162+
} else console.log(chalk.yellow.bold('\n electron-vue'))
163+
console.log(chalk.blue(' getting ready...') + '\n')
164+
}
165+
166+
function init () {
167+
greeting()
168+
169+
Promise.all([startRenderer(), startMain()])
170+
.then(() => {
171+
startElectron()
172+
})
173+
.catch(err => {
174+
console.error(err)
175+
})
176+
}
177+
178+
init()

0 commit comments

Comments
 (0)