Skip to content

Commit 4cb6886

Browse files
committed
feat(externals): add support for custom node_modules folder, closes #75
1 parent 6c76898 commit 4cb6886

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

__tests__/webpackConfig.spec.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const mockApi = {
1616
return '../__tests__/mock_package.json'
1717
} else if (path.match('./node_modules/mockExternal/package.json')) {
1818
return 'mockExternalPath'
19+
} else if (path.match('customNodeModulesPath/mockExternal/package.json')) {
20+
return 'customExternalPath'
1921
} else if (path.match('./node_modules/mockExternal/index.js')) {
2022
return 'mockExternalIndex'
2123
}
@@ -131,7 +133,7 @@ describe.each(['production', 'development'])('getExternals in %s', env => {
131133
// Mock dep's package.json
132134
const { readFileSync: realReadFileSync } = fs
133135
fs.readFileSync = jest.fn((path, ...args) => {
134-
if (path === 'mockExternalPath') {
136+
if (path === 'mockExternalPath' || path === 'customExternalPath') {
135137
return JSON.stringify(modulePkg)
136138
}
137139
// Don't effect other calls
@@ -187,4 +189,13 @@ describe.each(['production', 'development'])('getExternals in %s', env => {
187189
)
188190
expect(externals).toBeUndefined()
189191
})
192+
193+
test("Dep's package.json is read from nodeModulesPath", async () => {
194+
await mockGetExternals({}, { nodeModulesPath: 'customNodeModulesPath' })
195+
196+
// App's package.json is read from custom path
197+
expect(fs.readFileSync).toBeCalledWith(`customExternalPath`)
198+
// Not read from default path
199+
expect(fs.readFileSync).not.toBeCalledWith(`mockExternalPath`)
200+
})
190201
})

docs/guide/guide.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ module.exports = {
1818
pluginOptions: {
1919
electronBuilder: {
2020
// List native deps here if they don't work
21-
externals: ['my-native-dep']
21+
externals: ['my-native-dep'],
22+
// Set this to your project's node_modules folder if it is not ./node_modules (for yarn workspaces)
23+
nodeModulesPath: '../../node_modules'
2224
}
2325
}
2426
}

lib/webpackConfig.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ async function chainWebpack (api, pluginOptions, config) {
4040
}
4141
// Find all the dependencies without a `main` property or with a `binary` property or set by user and add them as webpack externals
4242
function getExternals (api, pluginOptions) {
43+
const nodeModulesPath = pluginOptions.nodeModulesPath || './node_modules'
4344
const userExternals = pluginOptions.externals || []
4445
const userExternalsWhitelist = []
4546
userExternals.forEach((d, i) => {
@@ -53,7 +54,7 @@ function getExternals (api, pluginOptions) {
5354
// Return true if we want to add a dependency to externals
5455
try {
5556
const pgkString = fs
56-
.readFileSync(api.resolve(`./node_modules/${dep}/package.json`))
57+
.readFileSync(api.resolve(`${nodeModulesPath}/${dep}/package.json`))
5758
.toString()
5859
const pkg = JSON.parse(pgkString)
5960
const fields = ['main', 'module', 'jsnext:main', 'browser']

0 commit comments

Comments
 (0)