Skip to content

Commit cd3d9ab

Browse files
committed
feat(build): only include external deps, fixes #223
1 parent f61d2b7 commit cd3d9ab

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

Diff for: __tests__/commands.spec.js

+39-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const path = require('path')
88
const execa = require('execa')
99
const portfinder = require('portfinder')
1010
const Application = require('spectron').Application
11-
const { chainWebpack } = require('../lib/webpackConfig')
11+
const { chainWebpack, getExternals } = require('../lib/webpackConfig')
1212
// #endregion
1313

1414
// #region Mocks
@@ -47,6 +47,12 @@ jest.mock('@vue/cli-service/lib/commands/serve', () => ({
4747
serve: jest.fn().mockResolvedValue({ url: 'serveUrl' })
4848
}))
4949
jest.mock('electron-builder', () => ({ build: jest.fn().mockResolvedValue() }))
50+
// Mock package.json
51+
fs.readFileSync.mockReturnValue(
52+
JSON.stringify({
53+
dependencies: {}
54+
})
55+
)
5056
const mockWait = jest.fn().mockResolvedValue()
5157
const mockStart = jest.fn()
5258
jest.mock('spectron', () => ({
@@ -204,14 +210,12 @@ describe('electron:build', () => {
204210
fs.existsSync.mockReturnValueOnce(true)
205211
await runCommand('electron:build')
206212
// css/fonts folder was created
207-
expect(fs.ensureDirSync.mock.calls[2][0]).toBe(
213+
expect(fs.ensureDirSync).toBeCalledWith(
208214
'projectPath/dist_electron/bundled/css/fonts'
209215
)
210216
// fonts was copied to css/fonts
211-
expect(fs.copySync.mock.calls[1][0]).toBe(
212-
'projectPath/dist_electron/bundled/fonts'
213-
)
214-
expect(fs.copySync.mock.calls[1][1]).toBe(
217+
expect(fs.copySync).toBeCalledWith(
218+
'projectPath/dist_electron/bundled/fonts',
215219
'projectPath/dist_electron/bundled/css/fonts'
216220
)
217221
})
@@ -337,15 +341,42 @@ describe('electron:build', () => {
337341
}
338342
})
339343
expect(fs.writeFileSync).toBeCalledWith(
340-
`dist_electron${path.sep}bundled${path.sep}legacy-assets-index.html.json`,
344+
`dist_electron${path.sep}bundled${
345+
path.sep
346+
}legacy-assets-index.html.json`,
341347
'[]'
342348
)
343349
expect(fs.writeFileSync).toBeCalledWith(
344-
`dist_electron${path.sep}bundled${path.sep}legacy-assets-subpage.html.json`,
350+
`dist_electron${path.sep}bundled${
351+
path.sep
352+
}legacy-assets-subpage.html.json`,
345353
'[]'
346354
)
347355
}
348356
)
357+
358+
test('Only external deps are included in the package.json', async () => {
359+
fs.readFileSync.mockReturnValueOnce(
360+
JSON.stringify({
361+
dependencies: {
362+
nonExternal: '^0.0.1',
363+
external: '^0.0.1'
364+
}
365+
})
366+
)
367+
getExternals.mockReturnValueOnce({ external: 'require("external")' })
368+
369+
await runCommand('electron:build')
370+
371+
expect(fs.writeFileSync).toBeCalledWith(
372+
`dist_electron${path.sep}bundled${path.sep}package.json`,
373+
JSON.stringify({
374+
dependencies: {
375+
external: '^0.0.1'
376+
}
377+
})
378+
)
379+
})
349380
})
350381

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

Diff for: index.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,20 @@ module.exports = (api, options) => {
130130
// Build the render process with the custom args
131131
await api.service.run('build', vueArgs)
132132
// Copy package.json to output dir
133-
fs.copySync(
134-
api.resolve('./package.json'),
135-
`${outputDir}/bundled/package.json`
133+
const pkg = JSON.parse(
134+
fs.readFileSync(api.resolve('./package.json'), 'utf8')
135+
)
136+
const externals = getExternals(api, pluginOptions)
137+
// https://github.com/nklayman/vue-cli-plugin-electron-builder/issues/223
138+
// Strip non-externals from dependencies so they won't be copied into app.asar
139+
Object.keys(pkg.dependencies).forEach(dependency => {
140+
if (!Object.keys(externals).includes(dependency)) {
141+
delete pkg.dependencies[dependency]
142+
}
143+
})
144+
fs.writeFileSync(
145+
`${outputDir}/bundled/package.json`,
146+
JSON.stringify(pkg, 2)
136147
)
137148
// Prevent electron-builder from installing app deps
138149
fs.ensureDirSync(`${outputDir}/bundled/node_modules`)

0 commit comments

Comments
 (0)