Skip to content

Commit de845a6

Browse files
Lms24billyvg
andauthored
ref(build): Add central build directory from JS SDK (getsentry/sentry-replay#321)
Introduce central `build` directory to the Replay package: * Change build output to `build/npm` (see explanation below) * Change `build:npm` command to execute a prepack script * Temporarily introduce the `./scripts/tmp-prepack.ts` script which copies files like `README.md`, `LICENSE` and `package.json` into `build/npm`. This is a simplified version of the monorepo's prepack script. While doing so, the script makes changes to `package.json` to align the entry points. * This file will be removed once we migrated as we can then use the monorepo's prepack script. * Remove the `files` entry in `package.json` for consistency with the monorepo (we just rely on `.npmignore`s) * Adjust occurrances of `dist` folder, replacing or adding `build` as appropriate As of this patch, the build directory will have the following structure: ``` <replay-root>/ ├─ build/ │ ├─ npm/ <-- everything in this directory goes into the NPM tarball │ │ ├─ types/ │ │ │ ├─ *.d.ts files (+maps) │ │ ├─ index(.es)?.js file + map │ │ ├─ package.json │ │ ├─ LICENSE │ | ├─ README.md ├─ ... ``` Co-authored-by: Billy Vong <[email protected]>
1 parent 9b427b0 commit de845a6

File tree

7 files changed

+107
-18
lines changed

7 files changed

+107
-18
lines changed

.eslintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
node_modules/
2-
dist/
2+
build/
33
demo/build/

.github/workflows/build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ jobs:
2222
- run: |
2323
yarn test
2424
25+
- run: |
26+
yarn build
27+
2528
- run: |
2629
yarn build:npm
2730

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules
2-
dist
32
/*.tgz
43
.eslintcache
4+
dist
5+
build

.npmignore

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
*
2-
!/package.json
3-
!/README.md
4-
!/LICENSE
5-
!dist/**
1+
# TODO after we migrated, we should revisit this file and check if we can use the monorepo's master
2+
# .npmigore file or if we should add custom entries here.
3+
# For the time being, this can stay empty.

config/tsconfig.core.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"baseUrl": "..",
88
"rootDir": "..",
99
"strictNullChecks": true,
10-
"outDir": "dist"
10+
"outDir": "build/npm"
1111
},
1212
"include": ["../src/**/*.ts"],
1313
"exclude": ["../src/**/*.test.ts"]

package.json

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"name": "@sentry/replay",
33
"version": "0.6.14",
44
"description": "User replays for Sentry",
5-
"main": "dist/index.js",
6-
"module": "dist/index.es.js",
7-
"types": "dist/types/src/index.d.ts",
5+
"main": "build/npm/index.js",
6+
"module": "build/npm/index.es.js",
7+
"types": "build/npm/types/src/index.d.ts",
88
"sideEffects": false,
99
"scripts": {
1010
"bootstrap": "yarn && cd demo && yarn #TODO: change after migration",
@@ -17,9 +17,9 @@
1717
"build:watch": "NODE_ENV=production yarn build:all:watch",
1818
"build:dev:watch": "NODE_ENV=development yarn build:all:watch",
1919
"build:all:watch": "yarn clean && run-p \"build:worker --watch\" \"build:core --watch\"",
20-
"build:npm": "yarn pack #TODO: use JS sdk prepack script after migration",
20+
"build:npm": "ts-node ./scripts/tmp-prepack.ts --bundles && npm pack ./build/npm #TODO: use JS sdk prepack script after migration",
2121
"circularDepCheck": "#TODO comment in after migration: madge --circular src/index.ts",
22-
"clean": "rimraf dist sentry-replay-*.tgz",
22+
"clean": "rimraf build sentry-replay-*.tgz",
2323
"fix": "run-s fix:eslint fix:prettier",
2424
"fix:eslint": "eslint . --format stylish --fix",
2525
"fix:prettier": "prettier --write \"{src,test,scripts,worker}/**/*.ts\"",
@@ -29,13 +29,9 @@
2929
"test": "jest",
3030
"test:watch": "jest --watch",
3131
"start:demo": "yarn build:dev && cd demo && yarn start",
32-
"prepack": "yarn build #TODO: remove after migration",
3332
"build:prod": "yarn build #TODO remove, we don't need this anymore after migration",
3433
"dev": "yarn build:dev:watch #TODO remove, we don't need this anymore after migration"
3534
},
36-
"files": [
37-
"dist"
38-
],
3935
"repository": {
4036
"type": "git",
4137
"url": "git+https://github.com/getsentry/sentry-replay.git"
@@ -92,7 +88,7 @@
9288
},
9389
"size-limit": [
9490
{
95-
"path": "dist/index.js",
91+
"path": "build/npm/index.js",
9692
"limit": "4500ms"
9793
}
9894
],

scripts/tmp-prepack.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
/* eslint-disable no-console */
3+
4+
/*
5+
THIS IS ONLY TEMPORARILY HERE
6+
This file is originally from the sentry-javascript repo. It's only here to ease
7+
migration into the monorepo and will be removed once the migration is complete.
8+
TODO: Delete once migrated to sentry-javascript
9+
*/
10+
11+
/*
12+
This script prepares the central `build` directory for NPM package creation.
13+
It first copies all non-code files into the `build` directory, including `package.json`, which
14+
is edited to adjust entry point paths. These corrections are performed so that the paths align with
15+
the directory structure inside `build`.
16+
*/
17+
18+
const fs = require('fs');
19+
const path = require('path');
20+
21+
const NPM_BUILD_DIR = 'build/npm';
22+
const BUILD_DIR = 'build';
23+
24+
const ASSETS = ['README.md', 'LICENSE', 'package.json'];
25+
const ENTRY_POINTS = ['main', 'module', 'types', 'browser'];
26+
27+
const packageWithBundles = process.argv.includes('--bundles');
28+
const buildDir = packageWithBundles ? NPM_BUILD_DIR : BUILD_DIR;
29+
30+
// eslint-disable-next-line @typescript-eslint/no-var-requires
31+
const pkgJson: { [key: string]: unknown } = require(path.resolve(
32+
'package.json'
33+
));
34+
35+
// check if build dir exists
36+
if (!fs.existsSync(path.resolve(buildDir))) {
37+
console.error(
38+
`\nERROR: Directory '${buildDir}' does not exist in ${pkgJson.name}.`
39+
);
40+
console.error(
41+
"This script should only be executed after you've run `yarn build`."
42+
);
43+
process.exit(1);
44+
}
45+
46+
// copy non-code assets to build dir
47+
ASSETS.forEach((asset) => {
48+
const assetPath = path.resolve(asset);
49+
if (!fs.existsSync(assetPath)) {
50+
console.error(`\nERROR: Asset '${asset}' does not exist.`);
51+
process.exit(1);
52+
}
53+
const destinationPath = path.resolve(buildDir, path.basename(asset));
54+
console.log(
55+
`Copying ${path.basename(asset)} to ${path.relative(
56+
'../..',
57+
destinationPath
58+
)}.`
59+
);
60+
fs.copyFileSync(assetPath, destinationPath);
61+
});
62+
63+
// package.json modifications
64+
const newPackageJsonPath = path.resolve(buildDir, 'package.json');
65+
// eslint-disable-next-line @typescript-eslint/no-var-requires
66+
const newPkgJson: { [key: string]: unknown } = require(newPackageJsonPath);
67+
68+
// modify entry points to point to correct paths (i.e. strip out the build directory)
69+
ENTRY_POINTS.filter((entryPoint) => newPkgJson[entryPoint]).forEach(
70+
(entryPoint) => {
71+
newPkgJson[entryPoint] = (newPkgJson[entryPoint] as string).replace(
72+
`${buildDir}/`,
73+
''
74+
);
75+
}
76+
);
77+
78+
delete newPkgJson.scripts;
79+
delete newPkgJson.volta;
80+
delete newPkgJson.jest;
81+
82+
// write modified package.json to file (pretty-printed with 2 spaces)
83+
try {
84+
fs.writeFileSync(newPackageJsonPath, JSON.stringify(newPkgJson, null, 2));
85+
} catch (error) {
86+
console.error(
87+
`\nERROR: Error while writing modified 'package.json' to disk in ${pkgJson.name}:\n`,
88+
error
89+
);
90+
process.exit(1);
91+
}

0 commit comments

Comments
 (0)