Skip to content

Commit d751a85

Browse files
feat(rebuilder): only rebuild prod and optional deps (ignore dev deps)
1 parent bb2c6cf commit d751a85

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

src/electron-forge-start.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import rebuild from './util/rebuild';
1010
import resolveDir from './util/resolve-dir';
1111

1212
const main = async () => {
13-
const startSpinner = ora.ora('Launching Application').start();
13+
const locateSpinner = ora.ora('Locating Application').start();
1414
let dir = process.cwd();
1515
program
1616
.version(require('../package.json').version)
@@ -28,15 +28,19 @@ const main = async () => {
2828

2929
dir = await resolveDir(dir);
3030
if (!dir) {
31-
startSpinner.fail();
31+
locateSpinner.fail();
3232
console.error('Failed to locate startable Electron application'.red);
3333
if (global._resolveError) global._resolveError();
3434
process.exit(1);
3535
}
36+
locateSpinner.succeed();
3637

3738
const packageJSON = JSON.parse(await fs.readFile(path.resolve(dir, 'package.json'), 'utf8'));
3839

3940
await rebuild(dir, packageJSON.devDependencies['electron-prebuilt-compile'], process.platform, process.arch);
41+
42+
const startSpinner = ora.ora('Launching Application').start();
43+
4044
const spawnOpts = {
4145
cwd: dir,
4246
stdio: 'inherit',

src/util/rebuild.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,26 @@ const d = debug('electron-forge:rebuild');
99

1010
export default async (buildPath, electronVersion, pPlatform, pArch) => {
1111
const rebuilds = [];
12+
let rebuildCount = 0;
13+
let rebuildCompleteCount = 0;
14+
const prodDeps = {};
15+
16+
const nativeSpinner = ora.ora(`Preparing native dependencies ${rebuildCompleteCount}/${rebuildCount}`).start();
17+
const updateNativeSpinner = () => {
18+
nativeSpinner.text = `Preparing native dependencies ${rebuildCompleteCount}/${rebuildCount}`;
19+
};
1220

1321
const rebuildModuleAt = async (modulePath) => {
1422
if (await fs.exists(path.resolve(modulePath, 'binding.gyp'))) {
1523
const metaPath = path.resolve(modulePath, 'build', 'Release', '.forge-meta');
24+
rebuildCount += 1;
25+
updateNativeSpinner();
1626
if (await fs.exists(metaPath)) {
1727
const meta = await fs.readFile(metaPath, 'utf8');
1828
if (meta === pArch) {
1929
d(`skipping: ${path.basename(modulePath)} as it is already built`);
30+
rebuildCompleteCount += 1;
31+
updateNativeSpinner();
2032
return;
2133
}
2234
}
@@ -59,11 +71,14 @@ export default async (buildPath, electronVersion, pPlatform, pArch) => {
5971
if (code !== 0) return reject(new Error(`Failed to rebuild: ${modulePath}\n\n${output}`));
6072
await fs.mkdirs(path.dirname(metaPath));
6173
await fs.writeFile(metaPath, pArch);
74+
rebuildCompleteCount += 1;
75+
updateNativeSpinner();
6276
resolve();
6377
});
6478
});
6579
}
6680
};
81+
6782
const rebuildAllModulesIn = (nodeModulesPath) => {
6883
for (const modulePath of fs.readdirSync(nodeModulesPath)) {
6984
rebuilds.push(rebuildModuleAt(path.resolve(nodeModulesPath, modulePath)));
@@ -75,7 +90,40 @@ export default async (buildPath, electronVersion, pPlatform, pArch) => {
7590
}
7691
}
7792
};
78-
const nativeSpinner = ora.ora('Preparing native dependencies').start();
93+
94+
const findModule = async (moduleName, fromDir, foundFn) => {
95+
let targetDir = fromDir;
96+
const foundFns = [];
97+
while (targetDir !== buildPath) {
98+
const testPath = path.resolve(targetDir, 'node_modules', moduleName);
99+
if (await fs.exists(testPath)) {
100+
foundFns.push(foundFn(testPath));
101+
}
102+
targetDir = path.dirname(targetDir);
103+
}
104+
await Promise.all(foundFns);
105+
};
106+
107+
const markChildrenAsProdDeps = async (modulePath) => {
108+
const childPackageJSON = JSON.parse(await fs.readFile(path.resolve(modulePath, 'package.json'), 'utf8'));
109+
const moduleWait = [];
110+
Object.keys(childPackageJSON.dependencies || {}).forEach((key) => {
111+
if (prodDeps[key]) return;
112+
prodDeps[key] = true;
113+
moduleWait.push(findModule(key, modulePath, markChildrenAsProdDeps));
114+
});
115+
await Promise.all(moduleWait);
116+
};
117+
118+
const rootPackageJSON = JSON.parse(await fs.readFile(path.resolve(buildPath, 'package.json'), 'utf8'));
119+
const markWaiters = [];
120+
Object.keys(rootPackageJSON.dependencies || {}).concat(Object.keys(rootPackageJSON.optionalDependencies || {})).forEach((key) => {
121+
prodDeps[key] = true;
122+
markWaiters.push(markChildrenAsProdDeps(path.resolve(buildPath, 'node_modules', key)));
123+
});
124+
125+
await Promise.all(markWaiters);
126+
79127
rebuildAllModulesIn(path.resolve(buildPath, 'node_modules'));
80128
try {
81129
await Promise.all(rebuilds);

0 commit comments

Comments
 (0)