Skip to content

Commit 4bed938

Browse files
committed
feat(build): use modern mode for builds
1 parent 080dae3 commit 4bed938

File tree

5 files changed

+68
-11
lines changed

5 files changed

+68
-11
lines changed

generator/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
1+
const fs = require('fs')
2+
13
module.exports = api => {
24
api.render('./template')
5+
// Read existing index.html and .gitignore
6+
let index = fs.readFileSync(api.resolve('./public/index.html'), 'utf8')
7+
let gitignore = fs.readFileSync(api.resolve('./.gitignore'), 'utf8')
8+
// Add base element inside <head> tag
9+
index = index.replace(
10+
/^\s*?<head.*?>\s*?$/m,
11+
`<head>\n <% if (BASE_URL === './') { %><base href="app://./" /><% } %>`
12+
)
13+
// Add /dist_electron to gitignore
14+
gitignore = gitignore + '\n#Electron-builder output\n/dist_electron'
15+
api.onCreateComplete(() => {
16+
// Write updated files
17+
fs.writeFileSync(api.resolve('./public/index.html'), index)
18+
fs.writeFileSync(api.resolve('./.gitignore'), gitignore)
19+
})
320
api.extendPackage({
421
scripts: {
522
'build:electron': 'vue-cli-service build:electron',

generator/template/_gitignore

Lines changed: 0 additions & 7 deletions
This file was deleted.

generator/template/src/background.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
'use strict'
22

3-
import { app, BrowserWindow } from 'electron'
3+
import { app, protocol, BrowserWindow } from 'electron'
44
import * as path from 'path'
55
import { format as formatUrl } from 'url'
6+
import createProtocol from 'vue-cli-plugin-electron-builder/lib/createProtocol'
67

78
const isDevelopment = process.env.NODE_ENV !== 'production'
89

910
// global reference to mainWindow (necessary to prevent window from being garbage collected)
1011
let mainWindow
1112

13+
// Standard scheme must be registered before the app is ready
14+
protocol.registerStandardSchemes(['app'], { secure: true })
1215
function createMainWindow () {
1316
const window = new BrowserWindow()
1417

@@ -17,6 +20,7 @@ function createMainWindow () {
1720
// Load the url of the dev server if in development mode
1821
window.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
1922
} else {
23+
createProtocol('app')
2024
// Load the index.html when not in development
2125
window.loadURL(
2226
formatUrl({

index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ module.exports = (api, options) => {
7171
// For the cli-ui webpack dashboard
7272
dashboard: args.dashboard,
7373
// Make sure files are outputted to proper directory
74-
dest: outputDir + '/bundled'
74+
dest: outputDir + '/bundled',
75+
// Enable modern mode
76+
modern: true
7577
}
7678
const mainConfig = new Config()
7779
// Configure main process webpack config
@@ -105,7 +107,8 @@ module.exports = (api, options) => {
105107
.loader('ts-loader')
106108
.options({ transpileOnly: !mainProcessTypeChecking })
107109
}
108-
110+
// Set the base url so that the app protocol is used
111+
options.baseUrl = './'
109112
console.log('Bundling render process:')
110113
// Build the render process with the custom args and config
111114
await buildRenderer(vueArgs, api, options, rendererConfig)
@@ -253,7 +256,6 @@ module.exports = (api, options) => {
253256
colors: true
254257
})
255258
)
256-
257259
console.log('\nStarting development server:\n')
258260
// Run the serve command with custom webpack config
259261
serve(

lib/createProtocol.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { protocol } from 'electron'
2+
import * as path from 'path'
3+
import { readFile } from 'fs'
4+
import { URL } from 'url'
5+
6+
export default scheme => {
7+
protocol.registerBufferProtocol(
8+
scheme,
9+
(request, respond) => {
10+
let pathName = new URL(request.url).pathname
11+
pathName = decodeURI(pathName) // Needed in case URL contains spaces
12+
13+
readFile(path.join(__dirname, pathName), (error, data) => {
14+
if (error) {
15+
console.error(`Failed to register ${scheme} protocol`, error)
16+
}
17+
let extension = path.extname(pathName).toLowerCase()
18+
let mimeType = ''
19+
20+
if (extension === '.js') {
21+
mimeType = 'text/javascript'
22+
} else if (extension === '.html') {
23+
mimeType = 'text/html'
24+
} else if (extension === '.css') {
25+
mimeType = 'text/css'
26+
} else if (extension === '.svg' || extension === '.svgz') {
27+
mimeType = 'image/svg+xml'
28+
} else if (extension === '.json') {
29+
mimeType = 'application/json'
30+
}
31+
32+
respond({ mimeType, data })
33+
})
34+
},
35+
error => {
36+
if (error) {
37+
console.error(`Failed to register ${scheme} protocol`, error)
38+
}
39+
}
40+
)
41+
}

0 commit comments

Comments
 (0)