diff --git a/packages/browser/.npmignore b/packages/browser/.npmignore index 5c6d6b607beb..643a96e2e5f4 100644 --- a/packages/browser/.npmignore +++ b/packages/browser/.npmignore @@ -1,6 +1,6 @@ -# Info: the paths in this file are adjusted to match once this -# file is copied to `./build`. This is done by a postbuild script -# located in sentry-javascript/scripts/postbuild.sh +# 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 +# postbuild script `sentry-javascript/scripts/postbuild.ts`. * diff --git a/packages/browser/package.json b/packages/browser/package.json index da5acad0ed2c..d9d2b7691625 100644 --- a/packages/browser/package.json +++ b/packages/browser/package.json @@ -44,7 +44,7 @@ "webpack": "^4.30.0" }, "scripts": { - "build": "run-p build:cjs build:esm build:bundle build:types && bash ../../scripts/postbuild.sh", + "build": "run-p build:cjs build:esm build:bundle build:types && ts-node ../../scripts/postbuild.ts", "build:bundle": "rollup --config", "build:cjs": "tsc -p tsconfig.cjs.json", "build:dev": "run-p build:cjs build:esm build:types", diff --git a/scripts/postbuild.sh b/scripts/postbuild.sh deleted file mode 100644 index 423f36c076d8..000000000000 --- a/scripts/postbuild.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/sh - -# This script prepares the `build` directory for NPM package creation. -# It first copies all non-code files into the `build` directory, including `package.json` which -# is edited via a few `sed` commands. These edits include corrections of paths (e.g. entry points) -# in the copied package.json so that they align with the directory structure inside `build`. - -BUILD_DIR=build - -ASSETS="README.md -LICENSE -package.json -.npmignore" - -# check if build dir exists -if [ ! -d $BUILD_DIR ]; then - echo "Directory ${BUILD_DIR}/ DOES NOT exist." - echo "This script should only be executed after you've run \`yarn build\`." - exit 1 -fi - -# copy non-code assets to build dir -for f in $ASSETS; do - cp $f $BUILD_DIR/ -done - -# package.json modifications - -# sed command to modify package.json entry points in build dir -# remove `BUILD_DIR` from `main`, `module` and `type` entry point paths -entryPointsCommand="/\"(main|module|types)\": .*,/s/$BUILD_DIR\///" - -# use empty backup file extension for sed in-place editing on MacOS and no backup for other platforms -if [[ $(uname) == "Darwin" ]]; then - sed -i "" -E "$entryPointsCommand" $BUILD_DIR/package.json -else - sed -i -E "$entryPointsCommand#" $BUILD_DIR/package.json -fi - -echo "Successfully finished postbuild commands" diff --git a/scripts/postbuild.ts b/scripts/postbuild.ts new file mode 100644 index 000000000000..959569bec034 --- /dev/null +++ b/scripts/postbuild.ts @@ -0,0 +1,63 @@ +/* + This script prepares the central `build` directory for NPM package creation. + It first copies all non-code files into the `build` directory, including `package.json`, which + is edited to adjust entry point paths. These corrections are performed so that the paths align with + the directory structure inside `build`. +*/ + +import * as fs from 'fs'; + +import * as path from 'path'; + +const BUILD_DIR = 'build'; +const ASSETS = ['README.md', 'LICENSE', 'package.json', '.npmignore']; +const ENTRY_POINTS = ['main', 'module', 'types']; + +// check if build dir exists +try { + if (!fs.existsSync(path.resolve(BUILD_DIR))) { + console.error(`Directory ${BUILD_DIR} 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 ${BUILD_DIR}`); + process.exit(1); +} + +// copy non-code assets to build dir +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}`); + process.exit(1); + } +}); + +// package.json modifications +const packageJsonPath = path.resolve(BUILD_DIR, 'package.json'); +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(`${BUILD_DIR}/`, ''); +}); + +delete pkgJson.scripts; +delete pkgJson.volta; + +// write modified package.json to file (pretty-printed with 2 spaces) +try { + fs.writeFileSync(packageJsonPath, JSON.stringify(pkgJson, null, 2)); +} catch (error) { + console.error(`Error while writing package.json to disk`); + process.exit(1); +} + +console.log(`\nSuccessfully finished postbuild commands for ${pkgJson.name}`);