Skip to content

Commit 0a56148

Browse files
committed
prepack: use dynamic imports instead of child process
explain ignoring of package specific prepack in `.eslintrc.js`
1 parent 25ff8a6 commit 0a56148

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

packages/gatsby/.eslintrc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ module.exports = {
66
parserOptions: {
77
jsx: true,
88
},
9+
// ignoring the package-specific prepack script here b/c it is not
10+
// covered by a `tsconfig` which makes eslint throw an error
911
ignorePatterns: ['scripts/prepack.ts'],
1012
extends: ['../../.eslintrc.js'],
1113
};

packages/gatsby/scripts/prepack.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
/* eslint-disable no-console */
2+
23
// DO NOT RUN this script yourself!
34
// This is invoked from the main `prepack.ts` script in `sentry-javascript/scripts/prepack.ts`.
5+
46
import * as fs from 'fs';
57
import * as path from 'path';
68

7-
const BUILD_DIR = 'build';
89
const PACKAGE_ASSETS = ['gatsby-browser.js', 'gatsby-node.js'];
910

10-
// copy package-specific assets to build dir
11-
PACKAGE_ASSETS.forEach(asset => {
12-
const assetPath = path.resolve(asset);
13-
try {
14-
if (!fs.existsSync(assetPath)) {
15-
console.error(`Asset ${asset} does not exist.`);
11+
export function prepack(buildDir: string): void {
12+
// copy package-specific assets to build dir
13+
PACKAGE_ASSETS.forEach(asset => {
14+
const assetPath = path.resolve(asset);
15+
try {
16+
if (!fs.existsSync(assetPath)) {
17+
console.error(`Asset ${asset} does not exist.`);
18+
process.exit(1);
19+
}
20+
fs.copyFileSync(assetPath, path.resolve(buildDir, asset));
21+
} catch (error) {
22+
console.error(`Error while copying ${asset} to ${buildDir}`);
1623
process.exit(1);
1724
}
18-
fs.copyFileSync(assetPath, path.resolve(BUILD_DIR, asset));
19-
} catch (error) {
20-
console.error(`Error while copying ${asset} to ${BUILD_DIR}`);
21-
}
22-
});
25+
});
26+
}

scripts/prepack.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
the directory structure inside `build`.
77
*/
88

9-
import * as childProcess from 'child_process';
109
import * as fs from 'fs';
1110
import * as fse from 'fs-extra';
1211
import * as path from 'path';
@@ -89,19 +88,20 @@ try {
8988
process.exit(1);
9089
}
9190

91+
async function runPackagePrepack(packagePrepackPath: string): Promise<void> {
92+
const { prepack } = await import(packagePrepackPath);
93+
if (prepack && typeof prepack === 'function') {
94+
prepack(buildDir);
95+
}
96+
}
97+
9298
// execute package specific settings
9399
// 1. check if a package called `<package-root>/scripts/prepack.ts` exitsts
94100
// if yes, 2.) execute that script for things that are package-specific
95101
const packagePrepackPath = path.resolve('scripts', 'prepack.ts');
96102
try {
97103
if (fs.existsSync(packagePrepackPath)) {
98-
const proc = childProcess.fork(packagePrepackPath);
99-
proc.on('exit', code => {
100-
if (code !== 0) {
101-
console.error(`Error while executing ${packagePrepackPath.toString()}`);
102-
process.exit(1);
103-
}
104-
});
104+
void runPackagePrepack(packagePrepackPath);
105105
}
106106
} catch (error) {
107107
console.error(`Error while trying to access ${packagePrepackPath.toString()}`);

0 commit comments

Comments
 (0)