From 67e59529c2f797f529cda35363566f544ff73f6d Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Tue, 5 Apr 2022 12:57:12 +0200 Subject: [PATCH 01/10] adjust `prepack.ts` to distinguish between packages with/-out bundles --- scripts/prepack.ts | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/scripts/prepack.ts b/scripts/prepack.ts index beecd1230f38..699077c700ee 100644 --- a/scripts/prepack.ts +++ b/scripts/prepack.ts @@ -11,18 +11,23 @@ import * as fse from 'fs-extra'; import * as path from 'path'; const NPM_BUILD_DIR = 'build/npm'; +const BUILD_DIR = 'build'; + const ASSETS = ['README.md', 'LICENSE', 'package.json', '.npmignore']; const ENTRY_POINTS = ['main', 'module', 'types']; +const packageWithBundles = !process.argv.includes('-noBundles'); +const buildDir = packageWithBundles ? NPM_BUILD_DIR : BUILD_DIR; + // check if build dir exists try { - if (!fs.existsSync(path.resolve(NPM_BUILD_DIR))) { - console.error(`Directory ${NPM_BUILD_DIR} DOES NOT exist`); + if (!fs.existsSync(path.resolve(buildDir))) { + console.error(`Directory ${buildDir} DOES NOT exist`); console.error("This script should only be executed after you've run `yarn build`."); process.exit(1); } } catch (error) { - console.error(`Error while looking up directory ${NPM_BUILD_DIR}`); + console.error(`Error while looking up directory ${buildDir}`); process.exit(1); } @@ -34,9 +39,9 @@ ASSETS.forEach(asset => { console.error(`Asset ${asset} does not exist.`); process.exit(1); } - fs.copyFileSync(assetPath, path.resolve(NPM_BUILD_DIR, asset)); + fs.copyFileSync(assetPath, path.resolve(buildDir, asset)); } catch (error) { - console.error(`Error while copying ${asset} to ${NPM_BUILD_DIR}`); + console.error(`Error while copying ${asset} to ${buildDir}`); process.exit(1); } }); @@ -45,9 +50,9 @@ ASSETS.forEach(asset => { // copy CDN bundles into npm dir to temporarily keep bundles in npm tarball // inside the tarball, they are located in `build/` // for now, copy it by default, unless explicitly forbidden via an command line arg -const tmpCopyBundles = !process.argv.includes('-skipBundleCopy'); +const tmpCopyBundles = packageWithBundles && !process.argv.includes('-skipBundleCopy'); if (tmpCopyBundles) { - const npmTmpBundlesPath = path.resolve(NPM_BUILD_DIR, 'build'); + const npmTmpBundlesPath = path.resolve(buildDir, 'build'); const cdnBundlesPath = path.resolve('build', 'bundles'); try { if (!fs.existsSync(npmTmpBundlesPath)) { @@ -55,18 +60,20 @@ if (tmpCopyBundles) { } void fse.copy(cdnBundlesPath, npmTmpBundlesPath); } catch (error) { - console.error(`Error while tmp copying CDN bundles to ${NPM_BUILD_DIR}`); + console.error(`Error while tmp copying CDN bundles to ${buildDir}`); process.exit(1); } } +// end remove + // package.json modifications -const packageJsonPath = path.resolve(NPM_BUILD_DIR, 'package.json'); +const packageJsonPath = path.resolve(buildDir, 'package.json'); // eslint-disable-next-line @typescript-eslint/no-var-requires const pkgJson: { [key: string]: unknown } = require(packageJsonPath); // modify entry points to point to correct paths (i.e. strip out the build directory) ENTRY_POINTS.filter(entryPoint => pkgJson[entryPoint]).forEach(entryPoint => { - pkgJson[entryPoint] = (pkgJson[entryPoint] as string).replace(`${NPM_BUILD_DIR}/`, ''); + pkgJson[entryPoint] = (pkgJson[entryPoint] as string).replace(`${buildDir}/`, ''); }); delete pkgJson.scripts; From 883ba498ae088237d5070d802b0f48c6b5a3dc77 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Tue, 5 Apr 2022 13:00:14 +0200 Subject: [PATCH 02/10] add build dir to `@sentry/core` --- packages/core/.npmignore | 7 ++++++- packages/core/package.json | 8 ++++---- packages/core/tsconfig.cjs.json | 2 +- packages/core/tsconfig.esm.json | 2 +- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/core/.npmignore b/packages/core/.npmignore index 8904efca5aea..0514c96743ba 100644 --- a/packages/core/.npmignore +++ b/packages/core/.npmignore @@ -1,4 +1,9 @@ +# Info: the paths in this file are specified so that they align with the file +# structure in `./build` where this file is copied to. This is done by the +# prepack script `sentry-javascript/scripts/prepack.ts`. + * + !/dist/**/* !/esm/**/* -!/build/types/**/* +!/types/**/* diff --git a/packages/core/package.json b/packages/core/package.json index 1140f6247e10..eea768058926 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -9,8 +9,8 @@ "engines": { "node": ">=6" }, - "main": "dist/index.js", - "module": "esm/index.js", + "main": "build/dist/index.js", + "module": "build/esm/index.js", "types": "build/types/index.d.ts", "publishConfig": { "access": "public" @@ -35,9 +35,9 @@ "build:es5:watch": "yarn build:cjs:watch # *** backwards compatibility - remove in v7 ***", "build:esm:watch": "tsc -p tsconfig.esm.json --watch", "build:types:watch": "tsc -p tsconfig.types.json --watch", - "build:npm": "npm pack", + "build:npm": "ts-node ../../scripts/prepack.ts -noBundles && npm pack ./build", "circularDepCheck": "madge --circular src/index.ts", - "clean": "rimraf dist esm coverage", + "clean": "rimraf dist esm build coverage", "fix": "run-s fix:eslint fix:prettier", "fix:eslint": "eslint . --format stylish --fix", "fix:prettier": "prettier --write \"{src,test,scripts}/**/*.ts\"", diff --git a/packages/core/tsconfig.cjs.json b/packages/core/tsconfig.cjs.json index abd80f77e1ff..e3a918fc70af 100644 --- a/packages/core/tsconfig.cjs.json +++ b/packages/core/tsconfig.cjs.json @@ -3,6 +3,6 @@ "compilerOptions": { "module": "commonjs", - "outDir": "dist" + "outDir": "build/dist" } } diff --git a/packages/core/tsconfig.esm.json b/packages/core/tsconfig.esm.json index b6ee3fa615c0..0b86c52918cc 100644 --- a/packages/core/tsconfig.esm.json +++ b/packages/core/tsconfig.esm.json @@ -3,6 +3,6 @@ "compilerOptions": { "module": "es6", - "outDir": "esm" + "outDir": "build/esm" } } From 92c07de28078a2127a4c36a33eab63ae38e751f4 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Tue, 5 Apr 2022 13:14:02 +0200 Subject: [PATCH 03/10] add build dir to `@sentry/gatsby` --- packages/gatsby/.eslintrc.js | 1 + packages/gatsby/.npmignore | 9 ++++++++- packages/gatsby/package.json | 6 +++--- packages/gatsby/scripts/prepack.ts | 22 ++++++++++++++++++++++ packages/gatsby/tsconfig.cjs.json | 2 +- packages/gatsby/tsconfig.esm.json | 2 +- scripts/prepack.ts | 20 ++++++++++++++++++++ 7 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 packages/gatsby/scripts/prepack.ts diff --git a/packages/gatsby/.eslintrc.js b/packages/gatsby/.eslintrc.js index 54e8382b22a8..cd85e7caf95d 100644 --- a/packages/gatsby/.eslintrc.js +++ b/packages/gatsby/.eslintrc.js @@ -6,5 +6,6 @@ module.exports = { parserOptions: { jsx: true, }, + ignorePatterns: ['scripts/prepack.ts'], extends: ['../../.eslintrc.js'], }; diff --git a/packages/gatsby/.npmignore b/packages/gatsby/.npmignore index 4822f65571c0..fb585a43c956 100644 --- a/packages/gatsby/.npmignore +++ b/packages/gatsby/.npmignore @@ -1,6 +1,13 @@ +# Info: the paths in this file are specified so that they align with the file +# structure in `./build` where this file is copied to. This is done by the +# prepack script `sentry-javascript/scripts/prepack.ts`. + * + !/dist/**/* !/esm/**/* -!/build/types/**/* +!/types/**/* + +# Gatsby specific !gatsby-browser.js !gatsby-node.js diff --git a/packages/gatsby/package.json b/packages/gatsby/package.json index 982795ac1e0c..1ba4b1793c10 100644 --- a/packages/gatsby/package.json +++ b/packages/gatsby/package.json @@ -13,8 +13,8 @@ "engines": { "node": ">=6" }, - "main": "dist/index.js", - "module": "esm/index.js", + "main": "build/dist/index.js", + "module": "build/esm/index.js", "types": "build/types/index.d.ts", "publishConfig": { "access": "public" @@ -46,7 +46,7 @@ "build:es5:watch": "yarn build:cjs:watch # *** backwards compatibility - remove in v7 ***", "build:esm:watch": "tsc -p tsconfig.esm.json --watch", "build:types:watch": "tsc -p tsconfig.types.json --watch", - "build:npm": "npm pack", + "build:npm": "ts-node ../../scripts/prepack.ts -noBundles && npm pack ./build", "circularDepCheck": "madge --circular src/index.ts", "clean": "rimraf dist esm build coverage", "fix": "run-s fix:eslint fix:prettier", diff --git a/packages/gatsby/scripts/prepack.ts b/packages/gatsby/scripts/prepack.ts new file mode 100644 index 000000000000..7fc49ba9deb2 --- /dev/null +++ b/packages/gatsby/scripts/prepack.ts @@ -0,0 +1,22 @@ +/* eslint-disable no-console */ +// DO NOT RUN this script yourself! +// This is invoked from the main `prepack.ts` script in `sentry-javascript/scripts/prepack.ts`. +import * as fs from 'fs'; +import * as path from 'path'; + +const BUILD_DIR = 'build'; +const PACKAGE_ASSETS = ['gatsby-browser.js', 'gatsby-node.js']; + +// copy package-specific assets to build dir +PACKAGE_ASSETS.forEach(asset => { + const assetPath = path.resolve(asset); + try { + if (!fs.existsSync(assetPath)) { + console.error(`Asset ${asset} does not exist.`); + process.exit(1); + } + fs.copyFileSync(assetPath, path.resolve(BUILD_DIR, asset)); + } catch (error) { + console.error(`Error while copying ${asset} to ${BUILD_DIR}`); + } +}); diff --git a/packages/gatsby/tsconfig.cjs.json b/packages/gatsby/tsconfig.cjs.json index abd80f77e1ff..e3a918fc70af 100644 --- a/packages/gatsby/tsconfig.cjs.json +++ b/packages/gatsby/tsconfig.cjs.json @@ -3,6 +3,6 @@ "compilerOptions": { "module": "commonjs", - "outDir": "dist" + "outDir": "build/dist" } } diff --git a/packages/gatsby/tsconfig.esm.json b/packages/gatsby/tsconfig.esm.json index b6ee3fa615c0..0b86c52918cc 100644 --- a/packages/gatsby/tsconfig.esm.json +++ b/packages/gatsby/tsconfig.esm.json @@ -3,6 +3,6 @@ "compilerOptions": { "module": "es6", - "outDir": "esm" + "outDir": "build/esm" } } diff --git a/scripts/prepack.ts b/scripts/prepack.ts index 699077c700ee..19fab8161935 100644 --- a/scripts/prepack.ts +++ b/scripts/prepack.ts @@ -6,6 +6,7 @@ the directory structure inside `build`. */ +import * as childProcess from 'child_process'; import * as fs from 'fs'; import * as fse from 'fs-extra'; import * as path from 'path'; @@ -88,4 +89,23 @@ try { process.exit(1); } +// execute package specific settings +// 1. check if a package called `/scripts/prepack.ts` exitsts +// if yes, 2.) execute that script for things that are package-specific +const packagePrepackPath = path.resolve('scripts', 'prepack.ts'); +try { + if (fs.existsSync(packagePrepackPath)) { + const proc = childProcess.fork(packagePrepackPath); + proc.on('exit', code => { + if (code !== 0) { + console.error(`Error while executing ${packagePrepackPath.toString()}`); + process.exit(1); + } + }); + } +} catch (error) { + console.error(`Error while trying to access ${packagePrepackPath.toString()}`); + process.exit(1); +} + console.log(`\nSuccessfully finished prepack commands for ${pkgJson.name}\n`); From f03d3b6bbab62832c8a5e2c2cb5a9407edd8b105 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Tue, 5 Apr 2022 13:21:23 +0200 Subject: [PATCH 04/10] add build dir to `@sentry/hub` --- packages/hub/.npmignore | 7 ++++++- packages/hub/package.json | 8 ++++---- packages/hub/tsconfig.cjs.json | 2 +- packages/hub/tsconfig.esm.json | 2 +- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/hub/.npmignore b/packages/hub/.npmignore index 8904efca5aea..0514c96743ba 100644 --- a/packages/hub/.npmignore +++ b/packages/hub/.npmignore @@ -1,4 +1,9 @@ +# Info: the paths in this file are specified so that they align with the file +# structure in `./build` where this file is copied to. This is done by the +# prepack script `sentry-javascript/scripts/prepack.ts`. + * + !/dist/**/* !/esm/**/* -!/build/types/**/* +!/types/**/* diff --git a/packages/hub/package.json b/packages/hub/package.json index f274d7d5d5c2..8f40d5e0ffd1 100644 --- a/packages/hub/package.json +++ b/packages/hub/package.json @@ -9,8 +9,8 @@ "engines": { "node": ">=6" }, - "main": "dist/index.js", - "module": "esm/index.js", + "main": "build/dist/index.js", + "module": "build/esm/index.js", "types": "build/types/index.d.ts", "publishConfig": { "access": "public" @@ -33,6 +33,7 @@ "build:es5:watch": "yarn build:cjs:watch # *** backwards compatibility - remove in v7 ***", "build:esm:watch": "tsc -p tsconfig.esm.json --watch", "build:types:watch": "tsc -p tsconfig.types.json --watch", + "build:npm": "ts-node ../../scripts/prepack.ts -noBundles && npm pack ./build", "circularDepCheck": "madge --circular src/index.ts", "clean": "rimraf dist esm coverage", "fix": "run-s fix:eslint fix:prettier", @@ -41,8 +42,7 @@ "link:yarn": "yarn link", "lint": "run-s lint:prettier lint:eslint", "lint:eslint": "eslint . --cache --cache-location '../../eslintcache/' --format stylish", - "lint:prettier": "prettier --check \"{src,test,scripts}/**/*.ts\"", - "build:npm": "npm pack", + "lint:prettier": "prettier --check \"{src,test}/**/*.ts\"", "test": "jest", "test:watch": "jest --watch" }, diff --git a/packages/hub/tsconfig.cjs.json b/packages/hub/tsconfig.cjs.json index abd80f77e1ff..e3a918fc70af 100644 --- a/packages/hub/tsconfig.cjs.json +++ b/packages/hub/tsconfig.cjs.json @@ -3,6 +3,6 @@ "compilerOptions": { "module": "commonjs", - "outDir": "dist" + "outDir": "build/dist" } } diff --git a/packages/hub/tsconfig.esm.json b/packages/hub/tsconfig.esm.json index b6ee3fa615c0..0b86c52918cc 100644 --- a/packages/hub/tsconfig.esm.json +++ b/packages/hub/tsconfig.esm.json @@ -3,6 +3,6 @@ "compilerOptions": { "module": "es6", - "outDir": "esm" + "outDir": "build/esm" } } From 25ff8a66039f3416ff8dfbb0f23c59952e882765 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Tue, 5 Apr 2022 13:23:02 +0200 Subject: [PATCH 05/10] add build dir to `@sentry/minimal` --- packages/minimal/.npmignore | 7 ++++++- packages/minimal/package.json | 8 ++++---- packages/minimal/tsconfig.cjs.json | 2 +- packages/minimal/tsconfig.esm.json | 2 +- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/minimal/.npmignore b/packages/minimal/.npmignore index 8904efca5aea..0514c96743ba 100644 --- a/packages/minimal/.npmignore +++ b/packages/minimal/.npmignore @@ -1,4 +1,9 @@ +# Info: the paths in this file are specified so that they align with the file +# structure in `./build` where this file is copied to. This is done by the +# prepack script `sentry-javascript/scripts/prepack.ts`. + * + !/dist/**/* !/esm/**/* -!/build/types/**/* +!/types/**/* diff --git a/packages/minimal/package.json b/packages/minimal/package.json index 671047736311..7caf902785cb 100644 --- a/packages/minimal/package.json +++ b/packages/minimal/package.json @@ -9,8 +9,8 @@ "engines": { "node": ">=6" }, - "main": "dist/index.js", - "module": "esm/index.js", + "main": "build/dist/index.js", + "module": "build/esm/index.js", "types": "build/types/index.d.ts", "publishConfig": { "access": "public" @@ -33,8 +33,9 @@ "build:es5:watch": "yarn build:cjs:watch # *** backwards compatibility - remove in v7 ***", "build:esm:watch": "tsc -p tsconfig.esm.json --watch", "build:types:watch": "tsc -p tsconfig.types.json --watch", + "build:npm": "ts-node ../../scripts/prepack.ts -noBundles && npm pack ./build", "circularDepCheck": "madge --circular src/index.ts", - "clean": "rimraf dist esm coverage", + "clean": "rimraf dist esm build coverage", "fix": "run-s fix:eslint fix:prettier", "fix:eslint": "eslint . --format stylish --fix", "fix:prettier": "prettier --write \"{src,test,scripts}/**/*.ts\"", @@ -42,7 +43,6 @@ "lint": "run-s lint:prettier lint:eslint", "lint:eslint": "eslint . --cache --cache-location '../../eslintcache/' --format stylish", "lint:prettier": "prettier --check \"{src,test,scripts}/**/*.ts\"", - "build:npm": "npm pack", "test": "jest", "test:watch": "jest --watch" }, diff --git a/packages/minimal/tsconfig.cjs.json b/packages/minimal/tsconfig.cjs.json index abd80f77e1ff..e3a918fc70af 100644 --- a/packages/minimal/tsconfig.cjs.json +++ b/packages/minimal/tsconfig.cjs.json @@ -3,6 +3,6 @@ "compilerOptions": { "module": "commonjs", - "outDir": "dist" + "outDir": "build/dist" } } diff --git a/packages/minimal/tsconfig.esm.json b/packages/minimal/tsconfig.esm.json index b6ee3fa615c0..0b86c52918cc 100644 --- a/packages/minimal/tsconfig.esm.json +++ b/packages/minimal/tsconfig.esm.json @@ -3,6 +3,6 @@ "compilerOptions": { "module": "es6", - "outDir": "esm" + "outDir": "build/esm" } } From 0a56148a98ecdb9107d00e3d813a4f19bf532627 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 6 Apr 2022 10:16:06 +0200 Subject: [PATCH 06/10] prepack: use dynamic imports instead of child process explain ignoring of package specific prepack in `.eslintrc.js` --- packages/gatsby/.eslintrc.js | 2 ++ packages/gatsby/scripts/prepack.ts | 28 ++++++++++++++++------------ scripts/prepack.ts | 16 ++++++++-------- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/packages/gatsby/.eslintrc.js b/packages/gatsby/.eslintrc.js index cd85e7caf95d..ab610a82206a 100644 --- a/packages/gatsby/.eslintrc.js +++ b/packages/gatsby/.eslintrc.js @@ -6,6 +6,8 @@ module.exports = { parserOptions: { jsx: true, }, + // ignoring the package-specific prepack script here b/c it is not + // covered by a `tsconfig` which makes eslint throw an error ignorePatterns: ['scripts/prepack.ts'], extends: ['../../.eslintrc.js'], }; diff --git a/packages/gatsby/scripts/prepack.ts b/packages/gatsby/scripts/prepack.ts index 7fc49ba9deb2..18e078ada1df 100644 --- a/packages/gatsby/scripts/prepack.ts +++ b/packages/gatsby/scripts/prepack.ts @@ -1,22 +1,26 @@ /* eslint-disable no-console */ + // DO NOT RUN this script yourself! // This is invoked from the main `prepack.ts` script in `sentry-javascript/scripts/prepack.ts`. + import * as fs from 'fs'; import * as path from 'path'; -const BUILD_DIR = 'build'; const PACKAGE_ASSETS = ['gatsby-browser.js', 'gatsby-node.js']; -// copy package-specific assets to build dir -PACKAGE_ASSETS.forEach(asset => { - const assetPath = path.resolve(asset); - try { - if (!fs.existsSync(assetPath)) { - console.error(`Asset ${asset} does not exist.`); +export function prepack(buildDir: string): void { + // copy package-specific assets to build dir + PACKAGE_ASSETS.forEach(asset => { + const assetPath = path.resolve(asset); + try { + if (!fs.existsSync(assetPath)) { + console.error(`Asset ${asset} does not exist.`); + process.exit(1); + } + fs.copyFileSync(assetPath, path.resolve(buildDir, asset)); + } catch (error) { + console.error(`Error while copying ${asset} to ${buildDir}`); process.exit(1); } - fs.copyFileSync(assetPath, path.resolve(BUILD_DIR, asset)); - } catch (error) { - console.error(`Error while copying ${asset} to ${BUILD_DIR}`); - } -}); + }); +} diff --git a/scripts/prepack.ts b/scripts/prepack.ts index 19fab8161935..d266a1aa3b61 100644 --- a/scripts/prepack.ts +++ b/scripts/prepack.ts @@ -6,7 +6,6 @@ the directory structure inside `build`. */ -import * as childProcess from 'child_process'; import * as fs from 'fs'; import * as fse from 'fs-extra'; import * as path from 'path'; @@ -89,19 +88,20 @@ try { process.exit(1); } +async function runPackagePrepack(packagePrepackPath: string): Promise { + const { prepack } = await import(packagePrepackPath); + if (prepack && typeof prepack === 'function') { + prepack(buildDir); + } +} + // execute package specific settings // 1. check if a package called `/scripts/prepack.ts` exitsts // if yes, 2.) execute that script for things that are package-specific const packagePrepackPath = path.resolve('scripts', 'prepack.ts'); try { if (fs.existsSync(packagePrepackPath)) { - const proc = childProcess.fork(packagePrepackPath); - proc.on('exit', code => { - if (code !== 0) { - console.error(`Error while executing ${packagePrepackPath.toString()}`); - process.exit(1); - } - }); + void runPackagePrepack(packagePrepackPath); } } catch (error) { console.error(`Error while trying to access ${packagePrepackPath.toString()}`); From 356775d0d35b1a31ba1ccf52b16e9cc0042381bb Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 6 Apr 2022 13:43:29 +0200 Subject: [PATCH 07/10] adjust prepack.ts to exit with error when package-specific prepack.ts errors --- scripts/prepack.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/scripts/prepack.ts b/scripts/prepack.ts index d266a1aa3b61..60a46e65466b 100644 --- a/scripts/prepack.ts +++ b/scripts/prepack.ts @@ -98,14 +98,15 @@ async function runPackagePrepack(packagePrepackPath: string): Promise { // execute package specific settings // 1. check if a package called `/scripts/prepack.ts` exitsts // if yes, 2.) execute that script for things that are package-specific -const packagePrepackPath = path.resolve('scripts', 'prepack.ts'); -try { - if (fs.existsSync(packagePrepackPath)) { - void runPackagePrepack(packagePrepackPath); +void (async () => { + const packagePrepackPath = path.resolve('scripts', 'prepack.ts'); + try { + if (fs.existsSync(packagePrepackPath)) { + await runPackagePrepack(packagePrepackPath); + } + } catch (error) { + console.error(`Error while trying to access ${packagePrepackPath.toString()}`); + process.exit(1); } -} catch (error) { - console.error(`Error while trying to access ${packagePrepackPath.toString()}`); - process.exit(1); -} - -console.log(`\nSuccessfully finished prepack commands for ${pkgJson.name}\n`); + console.log(`\nSuccessfully finished prepack commands for ${pkgJson.name}\n`); +})(); From c0cf3551a45206c89f8d4eb66a2361116bc52c0f Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 6 Apr 2022 16:17:39 +0200 Subject: [PATCH 08/10] apply review suggestions --- packages/gatsby/scripts/prepack.ts | 9 +++++---- scripts/prepack.ts | 5 ++++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/gatsby/scripts/prepack.ts b/packages/gatsby/scripts/prepack.ts index 18e078ada1df..5aa95909d70c 100644 --- a/packages/gatsby/scripts/prepack.ts +++ b/packages/gatsby/scripts/prepack.ts @@ -8,19 +8,20 @@ import * as path from 'path'; const PACKAGE_ASSETS = ['gatsby-browser.js', 'gatsby-node.js']; -export function prepack(buildDir: string): void { +export function prepack(buildDir: string): boolean { // copy package-specific assets to build dir - PACKAGE_ASSETS.forEach(asset => { + return PACKAGE_ASSETS.every(asset => { const assetPath = path.resolve(asset); try { if (!fs.existsSync(assetPath)) { console.error(`Asset ${asset} does not exist.`); - process.exit(1); + return false; } fs.copyFileSync(assetPath, path.resolve(buildDir, asset)); } catch (error) { console.error(`Error while copying ${asset} to ${buildDir}`); - process.exit(1); + return false; } + return true; }); } diff --git a/scripts/prepack.ts b/scripts/prepack.ts index 60a46e65466b..4cde4675a127 100644 --- a/scripts/prepack.ts +++ b/scripts/prepack.ts @@ -91,7 +91,10 @@ try { async function runPackagePrepack(packagePrepackPath: string): Promise { const { prepack } = await import(packagePrepackPath); if (prepack && typeof prepack === 'function') { - prepack(buildDir); + const success = prepack(buildDir); + if (!success) { + process.exit(1); + } } } From d70d4efb1969e27b25db4f3dcb6bdc013e9f98e5 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 6 Apr 2022 17:07:42 +0200 Subject: [PATCH 09/10] change variable name, add error message --- scripts/prepack.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/scripts/prepack.ts b/scripts/prepack.ts index 4cde4675a127..ef08175e13db 100644 --- a/scripts/prepack.ts +++ b/scripts/prepack.ts @@ -91,10 +91,16 @@ try { async function runPackagePrepack(packagePrepackPath: string): Promise { const { prepack } = await import(packagePrepackPath); if (prepack && typeof prepack === 'function') { - const success = prepack(buildDir); - if (!success) { + const isScuccess = prepack(buildDir); + if (!isScuccess) { process.exit(1); } + } else { + console.error(`Could not find a prepack function in ${packagePrepackPath}.`); + console.error( + 'Make sure, your package-specific prepack script exports `function prepack(buildDir: string): boolean`.', + ); + process.exit(1); } } From f918c59571c1a58c2d2f8e0cac26e372f578cf0e Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Wed, 6 Apr 2022 11:23:39 -0400 Subject: [PATCH 10/10] Update scripts/prepack.ts --- scripts/prepack.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/prepack.ts b/scripts/prepack.ts index ef08175e13db..ea8b7ed97770 100644 --- a/scripts/prepack.ts +++ b/scripts/prepack.ts @@ -91,8 +91,8 @@ try { async function runPackagePrepack(packagePrepackPath: string): Promise { const { prepack } = await import(packagePrepackPath); if (prepack && typeof prepack === 'function') { - const isScuccess = prepack(buildDir); - if (!isScuccess) { + const isSuccess = prepack(buildDir); + if (!isSuccess) { process.exit(1); } } else {