diff --git a/package.json b/package.json index 61e97690..855da6c0 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,10 @@ "require": "./renderer/index.js", "import": "./esm/renderer/index.js" }, - "./preload": "./preload/index.js" + "./preload": { + "require": "./preload/index.js", + "import": "./esm/preload/index.js" + } }, "repository": "https://github.com/getsentry/sentry-electron.git", "author": "Sentry", @@ -35,17 +38,9 @@ }, "scripts": { "prebuild": "yarn clean && node scripts/update-version.js", - "build": "run-p build:es6 build:esm build:preload", - "build:es6": "tsc -p tsconfig.build.json", - "build:esm": "tsc -p tsconfig.esm.json", - "build:preload": "node scripts/build-preload.js", - "build:watch": "run-p build:watch:es6 build:watch:esm", - "build:watch:es6": "tsc -p tsconfig.build.json -w --preserveWatchOutput", - "build:watch:esm": "tsc -p tsconfig.esm.json -w --preserveWatchOutput", + "build": "rollup --config rollup.config.js", "clean": "rimraf coverage esm main preload renderer index.* integrations.* ipc.* sentry-electron*.tgz", "prelint": "node scripts/update-version.js", - "update-electron-versions": "electron-latest-versions --start 2 > ./test/e2e/versions.json", - "update-sdk-versions": "node ./scripts/update-sdk-versions.mjs", "lint": "run-s lint:prettier lint:eslint", "lint:prettier": "prettier --check \"{src,test}/**/*.ts\"", "lint:eslint": "eslint . --cache --format stylish", @@ -53,6 +48,8 @@ "fix:prettier": "prettier --write \"{src,test}/**/*.ts\"", "fix:eslint": "eslint . --format stylish --fix", "pretest": "yarn build", + "update-electron-versions": "electron-latest-versions --start 2 > ./test/e2e/versions.json", + "update-sdk-versions": "node ./scripts/update-sdk-versions.mjs", "test": "cross-env TS_NODE_PROJECT=tsconfig.json xvfb-maybe electron-mocha --require ts-node/register/transpile-only --timeout 120000 ./test/unit/**/*.ts", "pree2e": "rimraf test/e2e/dist/**/node_modules/@sentry/** test/e2e/dist/**/yarn.lock test/e2e/dist/**/package-lock.json && node scripts/clean-cache.js && yarn build && npm pack", "e2e": "cross-env TS_NODE_PROJECT=tsconfig.json xvfb-maybe mocha --require ts-node/register/transpile-only --retries 3 ./test/e2e/*.ts" @@ -68,6 +65,8 @@ "tslib": "^2.5.0" }, "devDependencies": { + "@rollup/plugin-node-resolve": "^15.2.1", + "@rollup/plugin-typescript": "^11.1.4", "@sentry-internal/eslint-config-sdk": "7.68.0", "@sentry-internal/typescript": "7.68.0", "@types/busboy": "^0.2.3", @@ -97,6 +96,7 @@ "npm-run-all": "^4.1.5", "prettier": "^2.8.4", "rimraf": "^3.0.2", + "rollup": "^3.29.4", "tmp": "^0.2.1", "ts-node": "^10.9.1", "typescript": "^4.9.5", diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 00000000..d380eaa2 --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,63 @@ +const { builtinModules } = require('module'); +const { resolve } = require('path'); + +const typescript = require('@rollup/plugin-typescript'); + +const dependencies = Object.keys(require(resolve(process.cwd(), 'package.json')).dependencies || {}); + +const commonOutputOptions = { + sourcemap: true, + strict: false, + freeze: false, + externalLiveBindings: false, + generatedCode: { + preset: 'es2015', + symbols: false, + }, +}; + +function transpileFiles(format, input, outDir) { + return { + input, + output: { + ...commonOutputOptions, + format, + dir: outDir, + preserveModules: true, + }, + treeshake: { moduleSideEffects: false }, + plugins: [ + typescript({ + outDir, + tsconfig: './tsconfig.build.json', + }), + ], + external: [...builtinModules, 'electron', ...dependencies], + }; +} + +function bundlePreload(format, input, output) { + return { + input, + output: { + ...commonOutputOptions, + format, + file: output, + }, + plugins: [ + typescript({ + tsconfig: './tsconfig.preload.json', + }), + ], + external: [...builtinModules, 'electron', ...dependencies], + }; +} + +module.exports = [ + transpileFiles('cjs', ['src/index.ts', 'src/main/index.ts', 'src/renderer/index.ts'], '.'), + transpileFiles('esm', ['src/index.ts', 'src/main/index.ts', 'src/renderer/index.ts'], './esm'), + bundlePreload('cjs', 'src/preload/index.ts', './preload/index.js'), + bundlePreload('cjs', 'src/preload/legacy.ts', './preload/legacy.js'), + bundlePreload('esm', 'src/preload/index.ts', './esm/preload/index.js'), + bundlePreload('esm', 'src/preload/legacy.ts', './esm/preload/legacy.js'), +]; diff --git a/scripts/build-preload.js b/scripts/build-preload.js deleted file mode 100644 index e13b0b34..00000000 --- a/scripts/build-preload.js +++ /dev/null @@ -1,45 +0,0 @@ -const { readFileSync, writeFileSync, mkdirSync } = require('fs'); -const { join, dirname } = require('path'); -const ts = require('typescript'); - -function readFile(path) { - return readFileSync(join(__dirname, path), { encoding: 'utf8' }); -} - -function ensureOnlyRequiresElectron(code) { - const result = code.match(/require\("(\S+?)"\)/g); - if (result) { - if (!result.every((r) => r === 'require("electron")')) { - throw new Error('Bundling error. The preload script should only require electron!'); - } - } -} - -const ipcModule = readFile('../src/common/ipc.ts').replace(/export/g, ''); - -function transpileFile(input, output, esm) { - const module = esm ? ts.ModuleKind.ES2020 : ts.ModuleKind.CommonJS; - - // Because we're not using a proper bundler, we need to replace the import for ipc with inlined code - const file = readFile(input).replace("import { IPCChannel } from '../common/ipc'", ipcModule); - const code = ts.transpile(file, { removeComments: true, module }); - - if (!esm) { - ensureOnlyRequiresElectron(code); - } - - const outPath = join(__dirname, output); - try { - mkdirSync(dirname(outPath), { recursive: true }); - } catch (_) {} - writeFileSync(outPath, code); -} - -// Output to ./preload so '@sentry/electron/preload' can be resolved -transpileFile('../src/preload/index.ts', '../preload/index.js', false); -transpileFile('../src/preload/legacy.ts', '../preload/legacy.js', false); - -// The ESM output will never get used, but they need to be there for when webpack tries to -// find them due to `require.resolve('../../preload/index.js')` from esm/main code -transpileFile('../src/preload/index.ts', '../esm/preload/index.js', true); -transpileFile('../src/preload/legacy.ts', '../esm/preload/legacy.js', true); diff --git a/src/main/electron-normalize.ts b/src/main/electron-normalize.ts index e4f698c7..8917e1be 100644 --- a/src/main/electron-normalize.ts +++ b/src/main/electron-normalize.ts @@ -20,13 +20,17 @@ export const isPackaged = (() => { /** A promise that is resolved when the app is ready */ export const whenAppReady: Promise = (() => { - return app.isReady() - ? Promise.resolve() - : new Promise((resolve) => { - app.once('ready', () => { - resolve(); + if (app) { + return app.isReady() + ? Promise.resolve() + : new Promise((resolve) => { + app.once('ready', () => { + resolve(); + }); }); - }); + } else { + return Promise.resolve(); + } })(); /** diff --git a/src/main/fs.ts b/src/main/fs.ts index d174effc..29d01507 100644 --- a/src/main/fs.ts +++ b/src/main/fs.ts @@ -1,9 +1,9 @@ import { app } from 'electron'; -import { mkdir, readdir, readFile, rename, stat, statSync, unlink, writeFile } from 'fs'; +import { mkdir, readdir, readFile, stat, statSync, unlink, writeFile } from 'fs'; import { dirname, join, resolve } from 'path'; import { promisify } from 'util'; -export const sentryCachePath = join(app.getPath('userData'), 'sentry'); +export const sentryCachePath = join(app ? app.getPath('userData') : '', 'sentry'); export const writeFileAsync = promisify(writeFile); export const readFileAsync = promisify(readFile); @@ -11,7 +11,6 @@ export const mkdirAsync = promisify(mkdir); export const statAsync = promisify(stat); export const unlinkAsync = promisify(unlink); export const readDirAsync = promisify(readdir); -export const renameAsync = promisify(rename); // mkdir with recursive was only added in Node 10+ diff --git a/src/preload/index.ts b/src/preload/index.ts index e7cb8617..99391f04 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -1,7 +1,5 @@ /** * This preload script may be used with sandbox mode enabled which means regular require is not available. - * - * npm script `build:preload` calls `node ./scripts/build-preload.js` which inlines ipc and transpiles to JavaScript */ import { contextBridge, ipcRenderer } from 'electron'; diff --git a/src/preload/legacy.ts b/src/preload/legacy.ts index 38ea7b31..e9d367b0 100644 --- a/src/preload/legacy.ts +++ b/src/preload/legacy.ts @@ -1,7 +1,5 @@ /** * This preload script may be used with sandbox mode enabled which means regular require is not available. - * - * npm script `build:preload` calls `node ./scripts/build-preload.js` which inlines ipc and transpiles to JavaScript */ import { contextBridge, crashReporter, ipcRenderer } from 'electron'; diff --git a/tsconfig.build.json b/tsconfig.build.json index e5d3482a..71c012f2 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -1,23 +1,15 @@ { "extends": "./node_modules/@sentry-internal/typescript/tsconfig.json", - "include": [ - "src/**/*.ts" - ], - "exclude": [ - "node_modules", - "src/preload/*.ts" - ], + "include": ["src/**/*.ts"], + "exclude": ["node_modules", "src/preload/**/*.ts"], "compilerOptions": { "skipLibCheck": true, "baseUrl": ".", - "lib": [ - "es7", - "dom" - ], - "module": "commonjs", + "lib": ["ES7", "DOM"], + "module": "ESNext", "outDir": ".", "rootDir": "src", - "target": "es6", - "esModuleInterop": true, + "target": "ES6", + "esModuleInterop": true } } diff --git a/tsconfig.esm.json b/tsconfig.esm.json deleted file mode 100644 index b4713d16..00000000 --- a/tsconfig.esm.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "extends": "./tsconfig.build.json", - "compilerOptions": { - "outDir": "esm", - "module": "es6", - "esModuleInterop": true, - } -} diff --git a/tsconfig.json b/tsconfig.json index 4c6c154d..b3f843b4 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,13 +1,9 @@ { "extends": "./tsconfig.build.json", - "include": [ - "src/**/*.ts", - "test/**/*.ts" - ], - "exclude": [ - "dist" - ], + "include": ["src/**/*.ts", "test/**/*.ts"], + "exclude": ["node_modules"], "compilerOptions": { - "rootDir": ".", + "module": "CommonJS", + "rootDir": "." } } diff --git a/tsconfig.preload.json b/tsconfig.preload.json new file mode 100644 index 00000000..067a5bbd --- /dev/null +++ b/tsconfig.preload.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.build.json", + "include": ["src/preload/**/*.ts"], + "exclude": ["node_modules"], + "compilerOptions": { + "declaration": false, + "declarationMap": false + } +} diff --git a/yarn.lock b/yarn.lock index f882142a..e5cf4dd9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -135,6 +135,35 @@ "@pnpm/network.ca-file" "^1.0.1" config-chain "^1.1.11" +"@rollup/plugin-node-resolve@^15.2.1": + version "15.2.1" + resolved "https://registry.yarnpkg.com/@rollup/plugin-node-resolve/-/plugin-node-resolve-15.2.1.tgz#a15b14fb7969229e26a30feff2816d39eff503f0" + integrity sha512-nsbUg588+GDSu8/NS8T4UAshO6xeaOfINNuXeVHcKV02LJtoRaM1SiOacClw4kws1SFiNhdLGxlbMY9ga/zs/w== + dependencies: + "@rollup/pluginutils" "^5.0.1" + "@types/resolve" "1.20.2" + deepmerge "^4.2.2" + is-builtin-module "^3.2.1" + is-module "^1.0.0" + resolve "^1.22.1" + +"@rollup/plugin-typescript@^11.1.4": + version "11.1.4" + resolved "https://registry.yarnpkg.com/@rollup/plugin-typescript/-/plugin-typescript-11.1.4.tgz#cecb82180563e143481d979e5191953cbd0ee4c9" + integrity sha512-WZRh5LBVLQXdKFICUId5J3eIpmjGURaBqntfg3GSZACgeOAFS+lOSMGTwfzDkELTaZVp/lWdMVNU3UkwCUBg/Q== + dependencies: + "@rollup/pluginutils" "^5.0.1" + resolve "^1.22.1" + +"@rollup/pluginutils@^5.0.1": + version "5.0.4" + resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-5.0.4.tgz#74f808f9053d33bafec0cc98e7b835c9667d32ba" + integrity sha512-0KJnIoRI8A+a1dqOYLxH8vBf8bphDmty5QvIm2hqm7oFCFYKCAZWWd2hXgMibaPsNDhI0AtpYfQZJG47pt/k4g== + dependencies: + "@types/estree" "^1.0.0" + estree-walker "^2.0.2" + picomatch "^2.3.1" + "@sentry-internal/eslint-config-sdk@7.68.0": version "7.68.0" resolved "https://registry.yarnpkg.com/@sentry-internal/eslint-config-sdk/-/eslint-config-sdk-7.68.0.tgz#6154af30c5b1125419fb3192c97d96a8ddb98310" @@ -347,6 +376,11 @@ "@types/keygrip" "*" "@types/node" "*" +"@types/estree@^1.0.0": + version "1.0.2" + resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.2.tgz#ff02bc3dc8317cd668dfec247b750ba1f1d62453" + integrity sha512-VeiPZ9MMwXjO32/Xu7+OwflfmeoRwkE/qzndw42gGtgJwZopBnzy2gD//NN1+go1mADzkDcqf/KnFRSjTJ8xJA== + "@types/express-serve-static-core@^4.17.33": version "4.17.33" resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.33.tgz#de35d30a9d637dc1450ad18dd583d75d5733d543" @@ -468,6 +502,11 @@ resolved "https://registry.yarnpkg.com/@types/range-parser/-/range-parser-1.2.4.tgz#cd667bcfdd025213aafb7ca5915a932590acdcdc" integrity sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw== +"@types/resolve@1.20.2": + version "1.20.2" + resolved "https://registry.yarnpkg.com/@types/resolve/-/resolve-1.20.2.tgz#97d26e00cd4a0423b4af620abecf3e6f442b7975" + integrity sha512-60BCwRFOZCQhDncwQdxxeOEEkbc5dIMccYLwbxsS4TUNeVECQ/pBJ0j09mrHOl/JJvpRPGwO9SvE4nR2Nb/a4Q== + "@types/responselike@^1.0.0": version "1.0.0" resolved "https://registry.yarnpkg.com/@types/responselike/-/responselike-1.0.0.tgz#251f4fe7d154d2bad125abe1b429b23afd262e29" @@ -856,6 +895,11 @@ buffer-crc32@~0.2.3: resolved "https://registry.yarnpkg.com/buffer-crc32/-/buffer-crc32-0.2.13.tgz#0d333e3f00eac50aa1454abd30ef8c2a5d9a7242" integrity sha512-VO9Ht/+p3SN7SKWqcrgEzjGbRSJYTx+Q1pTQC0wrWqHx0vpJraQ6GtHx8tvcg1rlK1byhU5gccxgOgj7B0TDkQ== +builtin-modules@^3.3.0: + version "3.3.0" + resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-3.3.0.tgz#cae62812b89801e9656336e46223e030386be7b6" + integrity sha512-zhaCDicdLuWN5UbN5IMnFqNMhNfo919sH85y2/ea+5Yg9TsTkeZxpL+JLbp6cgYFS4sRLp3YV4S6yDuqVWHYOw== + busboy@^0.3.1: version "0.3.1" resolved "https://registry.yarnpkg.com/busboy/-/busboy-0.3.1.tgz#170899274c5bf38aae27d5c62b71268cd585fd1b" @@ -1201,6 +1245,11 @@ deepmerge@4.3.0: resolved "https://registry.yarnpkg.com/deepmerge/-/deepmerge-4.3.0.tgz#65491893ec47756d44719ae520e0e2609233b59b" integrity sha512-z2wJZXrmeHdvYJp/Ux55wIjqo81G5Bp4c+oELTW+7ar6SogWHajt5a9gO3s3IDaGSAXjDk0vlQKN3rms8ab3og== +deepmerge@^4.2.2: + version "4.3.1" + resolved "https://registry.yarnpkg.com/deepmerge/-/deepmerge-4.3.1.tgz#44b5f2147cd3b00d4b56137685966f26fd25dd4a" + integrity sha512-3sUqbMEc77XqpdNO7FRyRog+eW3ph+GYCbj+rK+uYyRMuwsVy0rMiVtPn+QJlKFvWP/1PYpapqYn0Me2knFn+A== + defer-to-connect@^2.0.0, defer-to-connect@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/defer-to-connect/-/defer-to-connect-2.0.1.tgz#8016bdb4143e4632b77a3449c6236277de520587" @@ -1646,6 +1695,11 @@ estraverse@^5.1.0, estraverse@^5.2.0: resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-5.3.0.tgz#2eea5290702f26ab8fe5370370ff86c965d21123" integrity sha512-MMdARuVEQziNTeJD8DgMqmhwR11BRQ/cBP+pLtYdSTnf3MIO8fFeiINEbX36ZdNlfU/7A9f3gUw49B3oQsvwBA== +estree-walker@^2.0.2: + version "2.0.2" + resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-2.0.2.tgz#52f010178c2a4c117a7757cfe942adb7d2da4cac" + integrity sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w== + esutils@^2.0.2: version "2.0.3" resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64" @@ -2203,6 +2257,13 @@ is-boolean-object@^1.1.0: call-bind "^1.0.2" has-tostringtag "^1.0.0" +is-builtin-module@^3.2.1: + version "3.2.1" + resolved "https://registry.yarnpkg.com/is-builtin-module/-/is-builtin-module-3.2.1.tgz#f03271717d8654cfcaf07ab0463faa3571581169" + integrity sha512-BSLE3HnV2syZ0FK0iMA/yUGplUeMmNz4AW5fnTunbCIqZi4vG3WjJT9FHMy5D69xmAYBHXQhJdALdpwVxV501A== + dependencies: + builtin-modules "^3.3.0" + is-callable@^1.1.3, is-callable@^1.1.4, is-callable@^1.2.7: version "1.2.7" resolved "https://registry.yarnpkg.com/is-callable/-/is-callable-1.2.7.tgz#3bc2a85ea742d9e36205dcacdd72ca1fdc51b055" @@ -2251,6 +2312,11 @@ is-glob@^4.0.0, is-glob@^4.0.1, is-glob@^4.0.3, is-glob@~4.0.1: dependencies: is-extglob "^2.1.1" +is-module@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/is-module/-/is-module-1.0.0.tgz#3258fb69f78c14d5b815d664336b4cffb6441591" + integrity sha512-51ypPSPCoTEIN9dy5Oy+h4pShgJmPCygKfyRCISBI+JoWT/2oJvK8QPxmwv7b/p239jXrm9M1mlQbyKJ5A152g== + is-negative-zero@^2.0.2: version "2.0.2" resolved "https://registry.yarnpkg.com/is-negative-zero/-/is-negative-zero-2.0.2.tgz#7bf6f03a28003b8b3965de3ac26f664d765f3150" @@ -3168,6 +3234,13 @@ roarr@^2.15.3: semver-compare "^1.0.0" sprintf-js "^1.1.2" +rollup@^3.29.4: + version "3.29.4" + resolved "https://registry.yarnpkg.com/rollup/-/rollup-3.29.4.tgz#4d70c0f9834146df8705bfb69a9a19c9e1109981" + integrity sha512-oWzmBZwvYrU0iJHtDmhsm662rC15FRXmcjCk1xD771dFDx5jJ02ufAQQTn0etB2emNk4J9EZg/yWKpsn9BWGRw== + optionalDependencies: + fsevents "~2.3.2" + run-parallel@^1.1.9: version "1.2.0" resolved "https://registry.yarnpkg.com/run-parallel/-/run-parallel-1.2.0.tgz#66d1368da7bdf921eb9d95bd1a9229e7f21a43ee"