Skip to content

Commit 5bf4771

Browse files
committed
fix(generator): remove need for <base> tag, set baseUrl instead
1 parent b2c7202 commit 5bf4771

File tree

5 files changed

+22
-53
lines changed

5 files changed

+22
-53
lines changed

__tests__/build.helper.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const create = require('./createProject.helper.js')
22
const path = require('path')
3-
const fs = require('fs-extra')
43
const Application = require('spectron').Application
54
const portfinder = require('portfinder')
65
const checkLogs = require('./checkLogs.helper.js')
@@ -43,12 +42,6 @@ const runTests = useTS =>
4342
project.has(`dist_electron/${projectName}-0.1.0-x86_64.AppImage`)
4443
).toBe(false)
4544
expect(project.has(`dist_electron/${projectName}_0.1.0_amd64`)).toBe(false)
46-
// Ensure base is set properly (for app protocol)
47-
const index = fs.readFileSync(
48-
projectPath('dist_electron/bundled/index.html'),
49-
'utf8'
50-
)
51-
expect(index.indexOf('<base href=app://./ >')).not.toBe(-1)
5245
// Launch app with spectron
5346
const app = new Application({
5447
path: `./__tests__/projects/${projectName}/dist_electron/${

__tests__/commands.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ const runCommand = async (command, options = {}, args = {}, rawArgs = []) => {
8686
pluginIndex(api, options)
8787
// Run the provided command
8888
await commands[command](args, rawArgs)
89+
return { api, options }
8990
}
9091
// #endregion
9192

@@ -296,6 +297,22 @@ describe('electron:build', () => {
296297
'projectPath/outputDir/bundled/background.js'
297298
)
298299
})
300+
301+
test('Base url is set to "app://./"', async () => {
302+
const { options } = await runCommand('electron:build')
303+
expect(options.baseUrl).toBe('app://./')
304+
})
305+
306+
test('Base url is set to "./" if pluginOptions.noAppProtocol is set', async () => {
307+
const { options } = await runCommand('electron:build', {
308+
pluginOptions: {
309+
electronBuilder: {
310+
noAppProtocol: true
311+
}
312+
}
313+
})
314+
expect(options.baseUrl).toBe('./')
315+
})
299316
})
300317

301318
describe('electron:serve', () => {

__tests__/generator.spec.js

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ describe('.gitignore', () => {
6262
'apiResolve_./.gitignore',
6363
expect.any(String)
6464
)
65-
// Only background.js should have been written
66-
expect(fs.writeFileSync).toHaveBeenCalledTimes(1)
65+
// Nothing should have been written
66+
expect(fs.writeFileSync).toHaveBeenCalledTimes(0)
6767
})
6868

6969
test.each(['#Electron-builder output', '/dist_electron'])(
@@ -90,43 +90,12 @@ describe('.gitignore', () => {
9090
'apiResolve_./.gitignore',
9191
expect.any(String)
9292
)
93-
// Only index should have been written
94-
expect(fs.writeFileSync).toHaveBeenCalledTimes(1)
93+
// Nothing should have been written
94+
expect(fs.writeFileSync).toHaveBeenCalledTimes(0)
9595
}
9696
)
9797
})
9898

99-
describe('index.html', () => {
100-
test.each([
101-
// None
102-
'',
103-
// Base URL
104-
` <% if (BASE_URL === './') { %><base href="app://./" /><% } %>\n`
105-
])('Only add missing tags to index.html', existing => {
106-
// Disable .gitignore modification
107-
fs.existsSync.mockReturnValueOnce(false)
108-
fs.readFileSync.mockImplementation((path, encoding) => {
109-
// Check that utf8 encoding is set
110-
expect(encoding).toBe('utf8')
111-
if (path === 'apiResolve_./package.json') {
112-
return JSON.stringify({ scripts: {} })
113-
}
114-
// return mock content
115-
return ` <head>\n${existing} </head>`
116-
})
117-
118-
// Run the generator with mock api
119-
generator(mockApi)
120-
// Run the onCreateComplete callback
121-
completionCb()
122-
123-
const index = fs.writeFileSync.mock.calls[0][1]
124-
expect(index).toBe(` <head>
125-
<% if (BASE_URL === './') { %><base href="app://./" /><% } %>
126-
</head>`)
127-
})
128-
})
129-
13099
describe('background.js', () => {
131100
test.each([false, true])(
132101
'Background file is not added if it exists',

generator/index.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@ module.exports = (api, options = {}) => {
1010
api.render('./template')
1111
}
1212
api.onCreateComplete(() => {
13-
// Read existing index.html
14-
let index = fs.readFileSync(api.resolve('./public/index.html'), 'utf8')
15-
const tag = `<% if (BASE_URL === './') { %><base href="app://./" /><% } %>`
16-
if (index.indexOf(tag) === -1) {
17-
// Inject base tag if it doesn't exist
18-
index = index.replace(/^\s*?<head.*?>\s*?$/m, ` <head>\n ${tag}`)
19-
}
20-
// Write updated index.html
21-
fs.writeFileSync(api.resolve('./public/index.html'), index)
22-
2313
// Update .gitignore if it exists
2414
if (fs.existsSync(api.resolve('./.gitignore'))) {
2515
let gitignore = fs.readFileSync(api.resolve('./.gitignore'), 'utf8')

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ module.exports = (api, options) => {
103103
modern: !args.legacy
104104
}
105105
// Set the base url so that the app protocol is used
106-
options.baseUrl = './'
106+
options.baseUrl = pluginOptions.noAppProtocol ? './' : 'app://./'
107107
info('Bundling render process:')
108108
// Build the render process with the custom args
109109
await api.service.run('build', vueArgs)

0 commit comments

Comments
 (0)