Skip to content

Commit 87ec44c

Browse files
committed
feat(webpackConfig): experimental native dep check
1 parent db7eeba commit 87ec44c

File tree

2 files changed

+65
-33
lines changed

2 files changed

+65
-33
lines changed

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ module.exports = (api, options) => {
6161
async (args, rawArgs) => {
6262
// Use custom config for webpack
6363
process.env.IS_ELECTRON = true
64+
if (args.experimentalNativeDepCheck) {
65+
process.env.VCPEB_EXPERIMENTAL_NATIVE_DEP_CHECK = true
66+
}
6467
const builder = require('electron-builder')
6568
const yargs = require('yargs')
6669
// Import the yargs options from electron-builder
@@ -74,6 +77,7 @@ module.exports = (api, options) => {
7477
removeArg('--skipBundle', 1, rawArgs)
7578
removeArg('--report', 1, rawArgs)
7679
removeArg('--report-json', 1, rawArgs)
80+
removeArg('--experimentalNativeDepCheck', 1, rawArgs)
7781
// Parse the raw arguments using electron-builder yargs config
7882
const builderArgs = yargs
7983
.command(['build', '*'], 'Build', configureBuildCommand)
@@ -261,6 +265,9 @@ module.exports = (api, options) => {
261265
async (args, rawArgs) => {
262266
// Use custom config for webpack
263267
process.env.IS_ELECTRON = true
268+
if (args.experimentalNativeDepCheck) {
269+
process.env.VCPEB_EXPERIMENTAL_NATIVE_DEP_CHECK = true
270+
}
264271
const execa = require('execa')
265272
const preload = pluginOptions.preload || {}
266273
const mainProcessWatch = [
@@ -275,6 +282,7 @@ module.exports = (api, options) => {
275282
removeArg('--debug', 1, rawArgs)
276283
removeArg('--headless', 1, rawArgs)
277284
removeArg('--https', 1, rawArgs)
285+
removeArg('--experimentalNativeDepCheck', 1, rawArgs)
278286

279287
// Run the serve command
280288
const server = await api.service.run('serve', {

lib/webpackConfig.js

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -97,41 +97,65 @@ function getExternals (api, pluginOptions) {
9797
const { dependencies } = require(api.resolve('./package.json'))
9898
const externalsList = Object.keys(dependencies || {}).reduce(
9999
(depList, dep) => {
100-
// Return true if we want to add a dependency to externals
101100
try {
102-
let pgkString
103-
for (const path of nodeModulesPaths) {
104-
// Check if package.json exists
105-
if (fs.existsSync(api.resolve(`${path}/${dep}/package.json`))) {
106-
// If it does, read it and break
107-
pgkString = fs
108-
.readFileSync(api.resolve(`${path}/${dep}/package.json`))
109-
.toString()
110-
break
101+
if (process.env.VCPEB_EXPERIMENTAL_NATIVE_DEP_CHECK) {
102+
// If dep is in whitelist, don't add it no matter what
103+
if (userExternalsWhitelist.includes(dep)) {
104+
return depList
105+
}
106+
const name = userExternals.find((name) =>
107+
new RegExp(`^${dep}(/|$)`).test(name)
108+
)
109+
// If dep is listed in user external array, it is an external
110+
if (name) {
111+
// Use user-provided name if it exists to support subpaths
112+
depList.push(name || dep)
113+
return depList
114+
}
115+
for (const path of nodeModulesPaths) {
116+
// Check if binding.gyp exists
117+
if (fs.existsSync(api.resolve(`${path}/${dep}/binding.gyp`))) {
118+
// If it does, dep is native
119+
// Use user-provided name if it exists to support subpaths
120+
depList.push(name || dep)
121+
return depList
122+
}
123+
}
124+
} else {
125+
let pgkString
126+
for (const path of nodeModulesPaths) {
127+
// Check if package.json exists
128+
if (fs.existsSync(api.resolve(`${path}/${dep}/package.json`))) {
129+
// If it does, read it and break
130+
pgkString = fs
131+
.readFileSync(api.resolve(`${path}/${dep}/package.json`))
132+
.toString()
133+
break
134+
}
135+
}
136+
if (!pgkString) {
137+
throw new Error(`Could not find a package.json for module ${dep}`)
138+
}
139+
const pkg = JSON.parse(pgkString)
140+
const name = userExternals.find((name) =>
141+
new RegExp(`^${pkg.name}(/|$)`).test(name)
142+
)
143+
const fields = ['main', 'module', 'jsnext:main', 'browser']
144+
if (
145+
// Not whitelisted
146+
userExternalsWhitelist.indexOf(dep) === -1 &&
147+
// Doesn't have main property
148+
(!fields.some((field) => field in pkg) ||
149+
// Has binary property
150+
!!pkg.binary ||
151+
// Has gypfile property
152+
!!pkg.gypfile ||
153+
// Listed in user-defined externals list
154+
!!name)
155+
) {
156+
// Use user-provided name if it exists, for subpaths
157+
depList.push(name || dep)
111158
}
112-
}
113-
if (!pgkString) {
114-
throw new Error(`Could not find a package.json for module ${dep}`)
115-
}
116-
const pkg = JSON.parse(pgkString)
117-
const name = userExternals.find((name) =>
118-
new RegExp(`^${pkg.name}(/|$)`).test(name)
119-
)
120-
const fields = ['main', 'module', 'jsnext:main', 'browser']
121-
if (
122-
// Not whitelisted
123-
userExternalsWhitelist.indexOf(dep) === -1 &&
124-
// Doesn't have main property
125-
(!fields.some((field) => field in pkg) ||
126-
// Has binary property
127-
!!pkg.binary ||
128-
// Has gypfile property
129-
!!pkg.gypfile ||
130-
// Listed in user-defined externals list
131-
!!name)
132-
) {
133-
// Use user-provided name if it exists, for subpaths
134-
depList.push(name || dep)
135159
}
136160
} catch (e) {
137161
console.log(e)

0 commit comments

Comments
 (0)