Skip to content

Commit 329e033

Browse files
Lms24AbhiPrasad
andauthored
ref(build): Add central build directory to packages without CDN bundles (Part 1) (#4854)
This PR is part 1 of adding the `build` directory to packages _without_ CDN bundles. It is split into two parts to make reviewing easier. It covers the following packages: * Core * Gatsby * Hub * Minimal Additionally, it adjusts `prepack.ts` to handle both kinds of packages (with/without CDN bundles) via a CL argument. For the Gatsby SDK, additional actions have to be performed which are only relevant for this package. Therefore, `prepack.ts` now supports calling a package-specific `prepack.ts` file located in `<package>/scripts/prepack.ts`. While the tarball structure is identical to the structure in #4838 (except for temporary CDN bundles), the `build` directory structure is simplified due to the fact that there are no CDN bundles or legacy NPM packages to be added to it. Therefore we can reduce one hierarchy level, resulting in the following structure: ``` <sdk>/ ├─ build/ │ ├─ cjs/ // dist until v7 │ │ ├─ CJS modules (+maps) │ ├─ esm/ │ │ ├─ ES6 modules (+maps) │ ├─ types/ │ │ ├─ *.d.ts files (+maps) │ ├─ package.json │ ├─ LICENSE │ ├─ README.md ├─ ... ``` Co-authored-by: Abhijeet Prasad <[email protected]>
1 parent eb11979 commit 329e033

19 files changed

+127
-38
lines changed

packages/core/.npmignore

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
# Info: the paths in this file are specified so that they align with the file
2+
# structure in `./build` where this file is copied to. This is done by the
3+
# prepack script `sentry-javascript/scripts/prepack.ts`.
4+
15
*
6+
27
!/dist/**/*
38
!/esm/**/*
4-
!/build/types/**/*
9+
!/types/**/*

packages/core/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"engines": {
1010
"node": ">=6"
1111
},
12-
"main": "dist/index.js",
13-
"module": "esm/index.js",
12+
"main": "build/dist/index.js",
13+
"module": "build/esm/index.js",
1414
"types": "build/types/index.d.ts",
1515
"publishConfig": {
1616
"access": "public"
@@ -35,9 +35,9 @@
3535
"build:es5:watch": "yarn build:cjs:watch # *** backwards compatibility - remove in v7 ***",
3636
"build:esm:watch": "tsc -p tsconfig.esm.json --watch",
3737
"build:types:watch": "tsc -p tsconfig.types.json --watch",
38-
"build:npm": "npm pack",
38+
"build:npm": "ts-node ../../scripts/prepack.ts -noBundles && npm pack ./build",
3939
"circularDepCheck": "madge --circular src/index.ts",
40-
"clean": "rimraf dist esm coverage",
40+
"clean": "rimraf dist esm build coverage",
4141
"fix": "run-s fix:eslint fix:prettier",
4242
"fix:eslint": "eslint . --format stylish --fix",
4343
"fix:prettier": "prettier --write \"{src,test,scripts}/**/*.ts\"",

packages/core/tsconfig.cjs.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
"compilerOptions": {
55
"module": "commonjs",
6-
"outDir": "dist"
6+
"outDir": "build/dist"
77
}
88
}

packages/core/tsconfig.esm.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
"compilerOptions": {
55
"module": "es6",
6-
"outDir": "esm"
6+
"outDir": "build/esm"
77
}
88
}

packages/gatsby/.eslintrc.js

+3
Original file line numberDiff line numberDiff line change
@@ -6,5 +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
11+
ignorePatterns: ['scripts/prepack.ts'],
912
extends: ['../../.eslintrc.js'],
1013
};

packages/gatsby/.npmignore

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
# Info: the paths in this file are specified so that they align with the file
2+
# structure in `./build` where this file is copied to. This is done by the
3+
# prepack script `sentry-javascript/scripts/prepack.ts`.
4+
15
*
6+
27
!/dist/**/*
38
!/esm/**/*
4-
!/build/types/**/*
9+
!/types/**/*
10+
11+
# Gatsby specific
512
!gatsby-browser.js
613
!gatsby-node.js

packages/gatsby/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"engines": {
1414
"node": ">=6"
1515
},
16-
"main": "dist/index.js",
17-
"module": "esm/index.js",
16+
"main": "build/dist/index.js",
17+
"module": "build/esm/index.js",
1818
"types": "build/types/index.d.ts",
1919
"publishConfig": {
2020
"access": "public"
@@ -46,7 +46,7 @@
4646
"build:es5:watch": "yarn build:cjs:watch # *** backwards compatibility - remove in v7 ***",
4747
"build:esm:watch": "tsc -p tsconfig.esm.json --watch",
4848
"build:types:watch": "tsc -p tsconfig.types.json --watch",
49-
"build:npm": "npm pack",
49+
"build:npm": "ts-node ../../scripts/prepack.ts -noBundles && npm pack ./build",
5050
"circularDepCheck": "madge --circular src/index.ts",
5151
"clean": "rimraf dist esm build coverage",
5252
"fix": "run-s fix:eslint fix:prettier",

packages/gatsby/scripts/prepack.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* eslint-disable no-console */
2+
3+
// DO NOT RUN this script yourself!
4+
// This is invoked from the main `prepack.ts` script in `sentry-javascript/scripts/prepack.ts`.
5+
6+
import * as fs from 'fs';
7+
import * as path from 'path';
8+
9+
const PACKAGE_ASSETS = ['gatsby-browser.js', 'gatsby-node.js'];
10+
11+
export function prepack(buildDir: string): boolean {
12+
// copy package-specific assets to build dir
13+
return PACKAGE_ASSETS.every(asset => {
14+
const assetPath = path.resolve(asset);
15+
try {
16+
if (!fs.existsSync(assetPath)) {
17+
console.error(`Asset ${asset} does not exist.`);
18+
return false;
19+
}
20+
fs.copyFileSync(assetPath, path.resolve(buildDir, asset));
21+
} catch (error) {
22+
console.error(`Error while copying ${asset} to ${buildDir}`);
23+
return false;
24+
}
25+
return true;
26+
});
27+
}

packages/gatsby/tsconfig.cjs.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
"compilerOptions": {
55
"module": "commonjs",
6-
"outDir": "dist"
6+
"outDir": "build/dist"
77
}
88
}

packages/gatsby/tsconfig.esm.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
"compilerOptions": {
55
"module": "es6",
6-
"outDir": "esm"
6+
"outDir": "build/esm"
77
}
88
}

packages/hub/.npmignore

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
# Info: the paths in this file are specified so that they align with the file
2+
# structure in `./build` where this file is copied to. This is done by the
3+
# prepack script `sentry-javascript/scripts/prepack.ts`.
4+
15
*
6+
27
!/dist/**/*
38
!/esm/**/*
4-
!/build/types/**/*
9+
!/types/**/*

packages/hub/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"engines": {
1010
"node": ">=6"
1111
},
12-
"main": "dist/index.js",
13-
"module": "esm/index.js",
12+
"main": "build/dist/index.js",
13+
"module": "build/esm/index.js",
1414
"types": "build/types/index.d.ts",
1515
"publishConfig": {
1616
"access": "public"
@@ -33,6 +33,7 @@
3333
"build:es5:watch": "yarn build:cjs:watch # *** backwards compatibility - remove in v7 ***",
3434
"build:esm:watch": "tsc -p tsconfig.esm.json --watch",
3535
"build:types:watch": "tsc -p tsconfig.types.json --watch",
36+
"build:npm": "ts-node ../../scripts/prepack.ts -noBundles && npm pack ./build",
3637
"circularDepCheck": "madge --circular src/index.ts",
3738
"clean": "rimraf dist esm coverage",
3839
"fix": "run-s fix:eslint fix:prettier",
@@ -41,8 +42,7 @@
4142
"link:yarn": "yarn link",
4243
"lint": "run-s lint:prettier lint:eslint",
4344
"lint:eslint": "eslint . --cache --cache-location '../../eslintcache/' --format stylish",
44-
"lint:prettier": "prettier --check \"{src,test,scripts}/**/*.ts\"",
45-
"build:npm": "npm pack",
45+
"lint:prettier": "prettier --check \"{src,test}/**/*.ts\"",
4646
"test": "jest",
4747
"test:watch": "jest --watch"
4848
},

packages/hub/tsconfig.cjs.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
"compilerOptions": {
55
"module": "commonjs",
6-
"outDir": "dist"
6+
"outDir": "build/dist"
77
}
88
}

packages/hub/tsconfig.esm.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
"compilerOptions": {
55
"module": "es6",
6-
"outDir": "esm"
6+
"outDir": "build/esm"
77
}
88
}

packages/minimal/.npmignore

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
# Info: the paths in this file are specified so that they align with the file
2+
# structure in `./build` where this file is copied to. This is done by the
3+
# prepack script `sentry-javascript/scripts/prepack.ts`.
4+
15
*
6+
27
!/dist/**/*
38
!/esm/**/*
4-
!/build/types/**/*
9+
!/types/**/*

packages/minimal/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"engines": {
1010
"node": ">=6"
1111
},
12-
"main": "dist/index.js",
13-
"module": "esm/index.js",
12+
"main": "build/dist/index.js",
13+
"module": "build/esm/index.js",
1414
"types": "build/types/index.d.ts",
1515
"publishConfig": {
1616
"access": "public"
@@ -33,16 +33,16 @@
3333
"build:es5:watch": "yarn build:cjs:watch # *** backwards compatibility - remove in v7 ***",
3434
"build:esm:watch": "tsc -p tsconfig.esm.json --watch",
3535
"build:types:watch": "tsc -p tsconfig.types.json --watch",
36+
"build:npm": "ts-node ../../scripts/prepack.ts -noBundles && npm pack ./build",
3637
"circularDepCheck": "madge --circular src/index.ts",
37-
"clean": "rimraf dist esm coverage",
38+
"clean": "rimraf dist esm build coverage",
3839
"fix": "run-s fix:eslint fix:prettier",
3940
"fix:eslint": "eslint . --format stylish --fix",
4041
"fix:prettier": "prettier --write \"{src,test,scripts}/**/*.ts\"",
4142
"link:yarn": "yarn link",
4243
"lint": "run-s lint:prettier lint:eslint",
4344
"lint:eslint": "eslint . --cache --cache-location '../../eslintcache/' --format stylish",
4445
"lint:prettier": "prettier --check \"{src,test,scripts}/**/*.ts\"",
45-
"build:npm": "npm pack",
4646
"test": "jest",
4747
"test:watch": "jest --watch"
4848
},

packages/minimal/tsconfig.cjs.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
"compilerOptions": {
55
"module": "commonjs",
6-
"outDir": "dist"
6+
"outDir": "build/dist"
77
}
88
}

packages/minimal/tsconfig.esm.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
"compilerOptions": {
55
"module": "es6",
6-
"outDir": "esm"
6+
"outDir": "build/esm"
77
}
88
}

scripts/prepack.ts

+48-11
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,23 @@ import * as fse from 'fs-extra';
1111
import * as path from 'path';
1212

1313
const NPM_BUILD_DIR = 'build/npm';
14+
const BUILD_DIR = 'build';
15+
1416
const ASSETS = ['README.md', 'LICENSE', 'package.json', '.npmignore'];
1517
const ENTRY_POINTS = ['main', 'module', 'types'];
1618

19+
const packageWithBundles = !process.argv.includes('-noBundles');
20+
const buildDir = packageWithBundles ? NPM_BUILD_DIR : BUILD_DIR;
21+
1722
// check if build dir exists
1823
try {
19-
if (!fs.existsSync(path.resolve(NPM_BUILD_DIR))) {
20-
console.error(`Directory ${NPM_BUILD_DIR} DOES NOT exist`);
24+
if (!fs.existsSync(path.resolve(buildDir))) {
25+
console.error(`Directory ${buildDir} DOES NOT exist`);
2126
console.error("This script should only be executed after you've run `yarn build`.");
2227
process.exit(1);
2328
}
2429
} catch (error) {
25-
console.error(`Error while looking up directory ${NPM_BUILD_DIR}`);
30+
console.error(`Error while looking up directory ${buildDir}`);
2631
process.exit(1);
2732
}
2833

@@ -34,9 +39,9 @@ ASSETS.forEach(asset => {
3439
console.error(`Asset ${asset} does not exist.`);
3540
process.exit(1);
3641
}
37-
fs.copyFileSync(assetPath, path.resolve(NPM_BUILD_DIR, asset));
42+
fs.copyFileSync(assetPath, path.resolve(buildDir, asset));
3843
} catch (error) {
39-
console.error(`Error while copying ${asset} to ${NPM_BUILD_DIR}`);
44+
console.error(`Error while copying ${asset} to ${buildDir}`);
4045
process.exit(1);
4146
}
4247
});
@@ -45,28 +50,30 @@ ASSETS.forEach(asset => {
4550
// copy CDN bundles into npm dir to temporarily keep bundles in npm tarball
4651
// inside the tarball, they are located in `build/`
4752
// for now, copy it by default, unless explicitly forbidden via an command line arg
48-
const tmpCopyBundles = !process.argv.includes('-skipBundleCopy');
53+
const tmpCopyBundles = packageWithBundles && !process.argv.includes('-skipBundleCopy');
4954
if (tmpCopyBundles) {
50-
const npmTmpBundlesPath = path.resolve(NPM_BUILD_DIR, 'build');
55+
const npmTmpBundlesPath = path.resolve(buildDir, 'build');
5156
const cdnBundlesPath = path.resolve('build', 'bundles');
5257
try {
5358
if (!fs.existsSync(npmTmpBundlesPath)) {
5459
fs.mkdirSync(npmTmpBundlesPath);
5560
}
5661
void fse.copy(cdnBundlesPath, npmTmpBundlesPath);
5762
} catch (error) {
58-
console.error(`Error while tmp copying CDN bundles to ${NPM_BUILD_DIR}`);
63+
console.error(`Error while tmp copying CDN bundles to ${buildDir}`);
5964
process.exit(1);
6065
}
6166
}
67+
// end remove
68+
6269
// package.json modifications
63-
const packageJsonPath = path.resolve(NPM_BUILD_DIR, 'package.json');
70+
const packageJsonPath = path.resolve(buildDir, 'package.json');
6471
// eslint-disable-next-line @typescript-eslint/no-var-requires
6572
const pkgJson: { [key: string]: unknown } = require(packageJsonPath);
6673

6774
// modify entry points to point to correct paths (i.e. strip out the build directory)
6875
ENTRY_POINTS.filter(entryPoint => pkgJson[entryPoint]).forEach(entryPoint => {
69-
pkgJson[entryPoint] = (pkgJson[entryPoint] as string).replace(`${NPM_BUILD_DIR}/`, '');
76+
pkgJson[entryPoint] = (pkgJson[entryPoint] as string).replace(`${buildDir}/`, '');
7077
});
7178

7279
delete pkgJson.scripts;
@@ -81,4 +88,34 @@ try {
8188
process.exit(1);
8289
}
8390

84-
console.log(`\nSuccessfully finished prepack commands for ${pkgJson.name}\n`);
91+
async function runPackagePrepack(packagePrepackPath: string): Promise<void> {
92+
const { prepack } = await import(packagePrepackPath);
93+
if (prepack && typeof prepack === 'function') {
94+
const isSuccess = prepack(buildDir);
95+
if (!isSuccess) {
96+
process.exit(1);
97+
}
98+
} else {
99+
console.error(`Could not find a prepack function in ${packagePrepackPath}.`);
100+
console.error(
101+
'Make sure, your package-specific prepack script exports `function prepack(buildDir: string): boolean`.',
102+
);
103+
process.exit(1);
104+
}
105+
}
106+
107+
// execute package specific settings
108+
// 1. check if a package called `<package-root>/scripts/prepack.ts` exitsts
109+
// if yes, 2.) execute that script for things that are package-specific
110+
void (async () => {
111+
const packagePrepackPath = path.resolve('scripts', 'prepack.ts');
112+
try {
113+
if (fs.existsSync(packagePrepackPath)) {
114+
await runPackagePrepack(packagePrepackPath);
115+
}
116+
} catch (error) {
117+
console.error(`Error while trying to access ${packagePrepackPath.toString()}`);
118+
process.exit(1);
119+
}
120+
console.log(`\nSuccessfully finished prepack commands for ${pkgJson.name}\n`);
121+
})();

0 commit comments

Comments
 (0)