Skip to content

Commit 7909c43

Browse files
committed
Initial commit
0 parents  commit 7909c43

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+18187
-0
lines changed

.babelrc

+30
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

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
config.mode = 'production'
76+
webpack(config, (err, stats) => {
77+
if (err) reject(err.stack || err)
78+
else if (stats.hasErrors()) {
79+
let err = ''
80+
81+
stats.toString({
82+
chunks: false,
83+
colors: true
84+
})
85+
.split(/\r?\n/)
86+
.forEach(line => {
87+
err += ` ${line}\n`
88+
})
89+
90+
reject(err)
91+
} else {
92+
resolve(stats.toString({
93+
chunks: false,
94+
colors: true
95+
}))
96+
}
97+
})
98+
})
99+
}
100+
101+
function web () {
102+
del.sync(['dist/web/*', '!.gitkeep'])
103+
webConfig.mode = 'production'
104+
webpack(webConfig, (err, stats) => {
105+
if (err || stats.hasErrors()) console.log(err)
106+
107+
console.log(stats.toString({
108+
chunks: false,
109+
colors: true
110+
}))
111+
112+
process.exit()
113+
})
114+
}
115+
116+
function greeting () {
117+
const cols = process.stdout.columns
118+
let text = ''
119+
120+
if (cols > 85) text = 'lets-build'
121+
else if (cols > 60) text = 'lets-|build'
122+
else text = false
123+
124+
if (text && !isCI) {
125+
say(text, {
126+
colors: ['yellow'],
127+
font: 'simple3d',
128+
space: false
129+
})
130+
} else console.log(chalk.yellow.bold('\n lets-build'))
131+
console.log()
132+
}

.electron-vue/dev-client.js

+40
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

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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+
rendererConfig.mode = 'development'
45+
const compiler = webpack(rendererConfig)
46+
hotMiddleware = webpackHotMiddleware(compiler, {
47+
log: false,
48+
heartbeat: 2500
49+
})
50+
51+
compiler.hooks.compilation.tap('compilation', compilation => {
52+
compilation.hooks.htmlWebpackPluginAfterEmit.tapAsync('html-webpack-plugin-after-emit', (data, cb) => {
53+
hotMiddleware.publish({ action: 'reload' })
54+
cb()
55+
})
56+
})
57+
58+
compiler.hooks.done.tap('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+
mainConfig.mode = 'development'
84+
const compiler = webpack(mainConfig)
85+
86+
compiler.hooks.watchRun.tapAsync('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+
var args = [
118+
'--inspect=5858',
119+
path.join(__dirname, '../dist/electron/main.js')
120+
]
121+
122+
// detect yarn or npm and process commandline args accordingly
123+
if (process.env.npm_execpath.endsWith('yarn.js')) {
124+
args = args.concat(process.argv.slice(3))
125+
} else if (process.env.npm_execpath.endsWith('npm-cli.js')) {
126+
args = args.concat(process.argv.slice(2))
127+
}
128+
129+
electronProcess = spawn(electron, args)
130+
131+
electronProcess.stdout.on('data', data => {
132+
electronLog(data, 'blue')
133+
})
134+
electronProcess.stderr.on('data', data => {
135+
electronLog(data, 'red')
136+
})
137+
138+
electronProcess.on('close', () => {
139+
if (!manualRestart) process.exit()
140+
})
141+
}
142+
143+
function electronLog (data, color) {
144+
let log = ''
145+
data = data.toString().split(/\r?\n/)
146+
data.forEach(line => {
147+
log += ` ${line}\n`
148+
})
149+
if (/[0-9A-z]+/.test(log)) {
150+
console.log(
151+
chalk[color].bold('┏ Electron -------------------') +
152+
'\n\n' +
153+
log +
154+
chalk[color].bold('┗ ----------------------------') +
155+
'\n'
156+
)
157+
}
158+
}
159+
160+
function greeting () {
161+
const cols = process.stdout.columns
162+
let text = ''
163+
164+
if (cols > 104) text = 'electron-vue'
165+
else if (cols > 76) text = 'electron-|vue'
166+
else text = false
167+
168+
if (text) {
169+
say(text, {
170+
colors: ['yellow'],
171+
font: 'simple3d',
172+
space: false
173+
})
174+
} else console.log(chalk.yellow.bold('\n electron-vue'))
175+
console.log(chalk.blue(' getting ready...') + '\n')
176+
}
177+
178+
function init () {
179+
greeting()
180+
181+
Promise.all([startRenderer(), startMain()])
182+
.then(() => {
183+
startElectron()
184+
})
185+
.catch(err => {
186+
console.error(err)
187+
})
188+
}
189+
190+
init()

0 commit comments

Comments
 (0)