Skip to content

Commit 7e4afa6

Browse files
committed
[skip ci] feat(build): add support for custom output dir (--dest)
1 parent c665ece commit 7e4afa6

File tree

5 files changed

+66
-50
lines changed

5 files changed

+66
-50
lines changed

__tests__/checkLogs.helper.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ module.exports = async ({ client, projectPath, projectName, mode }) => {
44
const isWin = process.platform === 'win32'
55
const isBuild = mode === 'build'
66
const outputPath = projectPath(
7-
`dist_electron/${
8-
isWin ? 'win' : 'linux'
9-
}-unpacked/resources/app.asar/dist_electron/bundled`
7+
`dist_electron/${isWin ? 'win' : 'linux'}-unpacked/resources/app.asar`
108
)
119

1210
await client.getRenderProcessLogs().then(logs => {

__tests__/commands.spec.js

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ describe('build:electron', () => {
121121
)
122122
})
123123

124-
test('custom output dir is used if provided', async () => {
124+
test('custom output dir is used if set in vue.config.js', async () => {
125125
await runCommand('build:electron', {
126126
pluginOptions: {
127127
electronBuilder: {
@@ -141,6 +141,20 @@ describe('build:electron', () => {
141141
)
142142
})
143143

144+
test('custom output dir is used if dest arg is passed', async () => {
145+
await runCommand('build:electron', {}, { dest: 'output' })
146+
147+
const mainConfig = webpack.mock.calls[0][0]
148+
// Main config output is correct
149+
expect(mainConfig.output.path).toBe('projectPath/output/bundled')
150+
// cli-service build output is correct
151+
expect(serviceRun.mock.calls[0][1].dest).toBe('output/bundled')
152+
// Electron-builder output is correct
153+
expect(builder.build.mock.calls[0][0].config.directories.output).toBe(
154+
'output'
155+
)
156+
})
157+
144158
test('Custom main process webpack config is used if provided', async () => {
145159
await runCommand('build:electron', {
146160
pluginOptions: {
@@ -181,14 +195,14 @@ describe('build:electron', () => {
181195
fs.existsSync.mockReturnValueOnce(true)
182196
await runCommand('build:electron')
183197
// css/fonts folder was created
184-
expect(fs.ensureDirSync.mock.calls[0][0]).toBe(
198+
expect(fs.ensureDirSync.mock.calls[1][0]).toBe(
185199
'projectPath/dist_electron/bundled/css/fonts'
186200
)
187201
// fonts was copied to css/fonts
188-
expect(fs.copySync.mock.calls[0][0]).toBe(
202+
expect(fs.copySync.mock.calls[1][0]).toBe(
189203
'projectPath/dist_electron/bundled/fonts'
190204
)
191-
expect(fs.copySync.mock.calls[0][1]).toBe(
205+
expect(fs.copySync.mock.calls[1][1]).toBe(
192206
'projectPath/dist_electron/bundled/css/fonts'
193207
)
194208
})
@@ -201,38 +215,36 @@ describe('build:electron', () => {
201215
expect(mainConfig.resolve.extensions).toEqual(['.js', '.ts'])
202216
})
203217

204-
test.each([['--mode', 'someMode'], ['--legacy'], ['--skipBundle']])(
205-
'%s argument is removed from electron-builder args',
206-
async (...args) => {
207-
await runCommand('build:electron', {}, {}, [
208-
'--keep1',
209-
...args,
210-
'--keep2'
211-
])
212-
// Custom args should have been removed, and other args kept
213-
expect(mockYargsParse).toBeCalledWith(['--keep1', '--keep2'])
214-
mockYargsParse.mockClear()
215-
216-
await runCommand('build:electron', {}, {}, [...args, '--keep2'])
217-
// Custom args should have been removed, and other args kept
218-
expect(mockYargsParse).toBeCalledWith(['--keep2'])
219-
mockYargsParse.mockClear()
220-
221-
await runCommand('build:electron', {}, {}, ['--keep1', ...args])
222-
// Custom args should have been removed, and other args kept
223-
expect(mockYargsParse).toBeCalledWith(['--keep1'])
224-
mockYargsParse.mockClear()
225-
226-
await runCommand('build:electron', {}, {}, args)
227-
// Custom args should have been removed
228-
expect(mockYargsParse).toBeCalledWith([])
229-
mockYargsParse.mockClear()
230-
231-
await runCommand('build:electron', {}, {}, ['--keep1', '--keep2'])
232-
// Nothing should be removed
233-
expect(mockYargsParse).toBeCalledWith(['--keep1', '--keep2'])
234-
}
235-
)
218+
test.each([
219+
['--mode', 'someMode'],
220+
['--legacy'],
221+
['--skipBundle'],
222+
['--dest', 'output']
223+
])('%s argument is removed from electron-builder args', async (...args) => {
224+
await runCommand('build:electron', {}, {}, ['--keep1', ...args, '--keep2'])
225+
// Custom args should have been removed, and other args kept
226+
expect(mockYargsParse).toBeCalledWith(['--keep1', '--keep2'])
227+
mockYargsParse.mockClear()
228+
229+
await runCommand('build:electron', {}, {}, [...args, '--keep2'])
230+
// Custom args should have been removed, and other args kept
231+
expect(mockYargsParse).toBeCalledWith(['--keep2'])
232+
mockYargsParse.mockClear()
233+
234+
await runCommand('build:electron', {}, {}, ['--keep1', ...args])
235+
// Custom args should have been removed, and other args kept
236+
expect(mockYargsParse).toBeCalledWith(['--keep1'])
237+
mockYargsParse.mockClear()
238+
239+
await runCommand('build:electron', {}, {}, args)
240+
// Custom args should have been removed
241+
expect(mockYargsParse).toBeCalledWith([])
242+
mockYargsParse.mockClear()
243+
244+
await runCommand('build:electron', {}, {}, ['--keep1', '--keep2'])
245+
// Nothing should be removed
246+
expect(mockYargsParse).toBeCalledWith(['--keep1', '--keep2'])
247+
})
236248

237249
test('Modern mode is enabled by default', async () => {
238250
await runCommand('build:electron')
@@ -298,7 +310,7 @@ describe('serve:electron', () => {
298310
)
299311
})
300312

301-
test('custom output dir is used if provided', async () => {
313+
test('custom output dir is used if set in vue.config.js', async () => {
302314
await runCommand('serve:electron', {
303315
pluginOptions: {
304316
electronBuilder: {

docs/guide/configuration.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ module.exports = {
7070

7171
## Changing the Output Directory
7272

73-
If you don't want your files outputted into dist_electron, you can choose a custom folder in vue-cli-plugin-electron-builder's plugin options.
73+
If you don't want your files outputted into dist_electron, you can choose a custom folder in vue-cli-plugin-electron-builder's plugin options. If you are using `v1.0.0-rc.4` or later, you can use the `--dest` argument to change the output dir as well.
7474

75-
**Note: after changing this, you MUST update the main field of your `package.json` to `[new dir]/bundled/background.js`. It is also recommended to add the new directory to your .gitignore file.**
75+
**Note: If you are using version 1.0.0-rc.3 or lower, after changing this, you MUST update the main field of your `package.json` to `[new dir]/bundled/background.js`. It is also recommended to add the new directory to your .gitignore file.**
7676

7777
```javascript
7878
// vue.config.js

generator/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,6 @@ if (isDevelopment) {
129129
devDependencies: {
130130
electron: '^2.0.2'
131131
},
132-
main: 'dist_electron/bundled/background.js'
132+
main: 'background.js'
133133
})
134134
}

index.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ module.exports = (api, options) => {
1515
const usesTypescript = pluginOptions.disableMainProcessTypescript
1616
? false
1717
: api.hasPlugin('typescript')
18-
const outputDir = pluginOptions.outputDir || 'dist_electron'
1918
const mainProcessFile =
2019
pluginOptions.mainProcessFile ||
2120
(usesTypescript ? 'src/background.ts' : 'src/background.js')
@@ -51,22 +50,21 @@ module.exports = (api, options) => {
5150
if (index !== -1) rawArgs.splice(index, count)
5251
}
5352
removeArg('--mode', 2)
53+
removeArg('--dest', 2)
5454
removeArg('--legacy', 1)
5555
removeArg('--skipBundle', 1)
5656
// Parse the raw arguments using electron-builder yargs config
5757
const builderArgs = yargs
5858
.command(['build', '*'], 'Build', configureBuildCommand)
5959
.parse(rawArgs)
6060
// Base config used in electron-builder build
61+
const outputDir = args.dest || pluginOptions.outputDir || 'dist_electron'
6162
const defaultBuildConfig = {
6263
directories: {
63-
output: outputDir
64+
output: outputDir,
65+
app: `${outputDir}/bundled`
6466
},
65-
files: [
66-
outputDir + '/bundled/**/*',
67-
'node_modules/**/*',
68-
'package.json'
69-
],
67+
files: ['**'],
7068
extends: null
7169
}
7270
// User-defined electron-builder config, overwrites/adds to default config
@@ -91,6 +89,13 @@ module.exports = (api, options) => {
9189
console.log('Bundling render process:')
9290
// Build the render process with the custom args
9391
await api.service.run('build', vueArgs)
92+
// Copy package.json to output dir
93+
fs.copySync(
94+
api.resolve('./package.json'),
95+
`${outputDir}/bundled/package.json`
96+
)
97+
// Prevent electron-builder from installing app deps
98+
fs.ensureDirSync(`${outputDir}/bundled/node_modules`)
9499
// Copy fonts to css/fonts. Fixes some issues with static font imports
95100
if (fs.existsSync(api.resolve(outputDir + '/bundled/fonts'))) {
96101
fs.ensureDirSync(api.resolve(outputDir + '/bundled/css/fonts'))
@@ -188,6 +193,7 @@ module.exports = (api, options) => {
188193
// Use dashboard if called from ui
189194
dashboard: args.dashboard
190195
})
196+
const outputDir = pluginOptions.outputDir || 'dist_electron'
191197

192198
// Copy package.json so electron can detect app's name
193199
fs.copySync(api.resolve('./package.json'), `${outputDir}/package.json`)

0 commit comments

Comments
 (0)