Skip to content

Commit e058520

Browse files
committed
adjust angular build dir and tarball structure to be more similar to other SDKs
includes deletion of .npmignore and the addition of `scripts/postbuild.ts` to make a few adjustments to folder structure and the shipped `package.json`
1 parent 824544e commit e058520

File tree

6 files changed

+89
-20
lines changed

6 files changed

+89
-20
lines changed

packages/angular/.eslintrc.js

+2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ module.exports = {
22
env: {
33
browser: true,
44
},
5+
// ignore these because they're not covered by a `tsconfig`, which makes eslint throw an error
6+
ignorePatterns: ['scripts/postbuild.ts'],
57
extends: ['../../.eslintrc.js'],
68
};

packages/angular/.npmignore

-4
This file was deleted.

packages/angular/ng-package.json

+1-7
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@
88
"@sentry/utils": "Sentry.util"
99
}
1010
},
11-
"whitelistedNonPeerDependencies": [
12-
"@sentry/browser",
13-
"@sentry/utils",
14-
"@sentry/types",
15-
"tslib",
16-
"rxjs"
17-
],
11+
"whitelistedNonPeerDependencies": ["@sentry/browser", "@sentry/utils", "@sentry/types", "tslib"],
1812
"assets": ["README.md", "LICENSE"]
1913
}

packages/angular/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
"engines": {
1010
"node": ">=8"
1111
},
12-
"main": "cjs/index.js",
13-
"module": "esm/index.js",
14-
"types": "build/types/index.d.ts",
12+
"main": "build/bundles/sentry-angular.umd.js",
13+
"module": "build/fesm2015/sentry-angular.js",
14+
"types": "build/types",
1515
"publishConfig": {
1616
"access": "public"
1717
},
@@ -39,7 +39,7 @@
3939
"typescript": "~4.0.2"
4040
},
4141
"scripts": {
42-
"build": "yarn build:ngc",
42+
"build": "yarn build:ngc && ts-node scripts/postbuild.ts",
4343
"build:legacy": "run-p build:esm build:cjs",
4444
"build:ngc": "ng build --prod",
4545
"build:cjs": "tsc -p tsconfig.cjs.json",

packages/angular/scripts/postbuild.ts

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/* eslint-disable no-console */
2+
3+
// This script changes the file structure inside the `build` directory so that
4+
// the final structure is a) compatible with the Angular package format and
5+
// b) as similar as possible to our other SDKs. In order to achieve b), this
6+
// script moves the type declaration files (+maps) from `build` to `build/types`
7+
// and modifies the entry points in the generated package.json accordingly.
8+
9+
// This script needs to be executed after compiling because our package.json
10+
// (i.e. the one that we maintain and that serves as a basis for ng-packagr)
11+
// also has to point to `build/types`.
12+
13+
import * as fs from 'fs';
14+
import * as path from 'path';
15+
16+
const BUILD_DIR = 'build';
17+
const TYPES_DIR = 'types';
18+
19+
/**
20+
* Moves all type declaration files (*.d.ts, *.d.ts.map) to `build/types` to conform with
21+
* our other SDK's structure.
22+
*/
23+
function moveTypeDeclarations(): void {
24+
try {
25+
const buildDir = path.resolve(BUILD_DIR);
26+
const typesDir = path.resolve(BUILD_DIR, TYPES_DIR);
27+
28+
if (!fs.existsSync(typesDir)) {
29+
fs.mkdirSync(typesDir);
30+
}
31+
32+
const declarationFiles = fs
33+
.readdirSync(buildDir)
34+
.filter(file => file.endsWith('.d.ts') || file.endsWith('.d.ts.map'));
35+
36+
declarationFiles.forEach(f => fs.renameSync(path.resolve(buildDir, f), path.resolve(typesDir, f)));
37+
} catch (error) {
38+
console.error('Error while moving type declaration files');
39+
process.exit(-1);
40+
}
41+
}
42+
43+
/**
44+
* Takes the package.json file that is generated by `ng-packagr` when running `yarn build`
45+
* and modifies the type entry points to point to the `build/types` directory.
46+
* Additionally, this function deletes unnecessary properties that do not need to be
47+
* shipped with the NPM package.
48+
*/
49+
function modifyPkgJson(): void {
50+
const typeEntryPoints = ['types', 'typings'];
51+
const typeEntryFile = 'sentry-angular.d.ts';
52+
53+
try {
54+
// this is the path to the package.json that is generated by ng-packagr
55+
const packageJsonPath = path.resolve(BUILD_DIR, 'package.json');
56+
const pkgJson: { [key: string]: unknown } = JSON.parse(fs.readFileSync(packageJsonPath, { encoding: 'utf8' }));
57+
58+
const newPkgJson = { ...pkgJson };
59+
60+
typeEntryPoints.forEach(entryPoint => {
61+
newPkgJson[entryPoint] = path.join(TYPES_DIR, typeEntryFile);
62+
});
63+
64+
// We do not need volta information in the shipped package.json
65+
delete newPkgJson.volta;
66+
67+
fs.writeFileSync(packageJsonPath, JSON.stringify(newPkgJson, null, 2));
68+
} catch (error) {
69+
console.error('Error while modifying generated package.json');
70+
process.exit(-1);
71+
}
72+
}
73+
74+
function postbuild(): void {
75+
moveTypeDeclarations();
76+
modifyPkgJson();
77+
console.log('\nSuccessfully finished postbuild commands for @sentry/angular\n');
78+
}
79+
80+
postbuild();

packages/angular/tsconfig.ngc.json

+2-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66
"extends": "./tsconfig.json",
77
"compilerOptions": {
88
"target": "es2015",
9-
"lib": [
10-
"dom",
11-
"es2018"
12-
],
13-
"baseUrl": "./",
9+
"lib": ["dom", "es2015"],
10+
"baseUrl": "./"
1411
},
1512
"angularCompilerOptions": {
1613
"skipTemplateCodegen": true,

0 commit comments

Comments
 (0)