-
-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Node compatible ESM build #756
Changes from all commits
afbf7e7
5bbddd2
bd70d3f
c7b55d4
f6a0c44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
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 external = [...builtinModules, 'electron', ...dependencies]; | ||
|
||
const outputOptions = { | ||
sourcemap: true, | ||
strict: false, | ||
freeze: false, | ||
externalLiveBindings: false, | ||
generatedCode: { | ||
preset: 'es2015', | ||
symbols: false, | ||
}, | ||
}; | ||
|
||
// a simple plugin that adds a package.json file with type: module | ||
const modulePackageJson = { | ||
name: 'package-json-module-type', | ||
generateBundle(_, __) { | ||
this.emitFile({ | ||
type: 'asset', | ||
fileName: 'package.json', | ||
source: '{"type": "module"}', | ||
}); | ||
}, | ||
}; | ||
|
||
function transpileFiles(format, input, outDir) { | ||
return { | ||
input, | ||
output: { | ||
...outputOptions, | ||
format, | ||
dir: outDir, | ||
preserveModules: true, | ||
}, | ||
treeshake: { moduleSideEffects: false }, | ||
plugins: [ | ||
typescript({ | ||
outDir, | ||
tsconfig: './tsconfig.build.json', | ||
}), | ||
format === 'esm' ? modulePackageJson : {}, | ||
], | ||
external, | ||
}; | ||
} | ||
|
||
function bundlePreload(format, input, output) { | ||
return { | ||
input, | ||
output: { | ||
...outputOptions, | ||
format, | ||
file: output, | ||
}, | ||
plugins: [ | ||
typescript({ | ||
tsconfig: './tsconfig.preload.json', | ||
}), | ||
], | ||
external, | ||
}; | ||
} | ||
|
||
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'), | ||
]; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,34 @@ import { Integration } from '@sentry/types'; | |
import { logger } from '@sentry/utils'; | ||
import { app } from 'electron'; | ||
import { existsSync } from 'fs'; | ||
import { isAbsolute } from 'path'; | ||
import { isAbsolute, resolve } from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
|
||
import { IPCMode } from '../../common'; | ||
import { rendererRequiresCrashReporterStart } from '../electron-normalize'; | ||
import { ElectronMainOptionsInternal } from '../sdk'; | ||
|
||
// After bundling with webpack, require.resolve can return number so we include that in the types | ||
// to ensure we check for that! | ||
function getPreloadPath(): string | number | undefined { | ||
try { | ||
return rendererRequiresCrashReporterStart() | ||
? require.resolve('../../preload/legacy.js') | ||
: require.resolve('../../preload/index.js'); | ||
} catch (_) { | ||
try { | ||
// This could be ESM | ||
const currentDir = fileURLToPath(import.meta.url); | ||
// Use the CJS preload | ||
return resolve(currentDir, '..', '..', '..', '..', 'preload', 'index.js'); | ||
Comment on lines
+19
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if this will break with any bundlers, but if the e2e tests pass we should be fine I guess. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preload injection mostly fails anyway with bundlers anyway and we fallback to custom protocol. Once Electron v28 makes it to beta we can add some ESM main process bundler tests. |
||
} catch (_) { | ||
// | ||
} | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
/** | ||
* Injects the preload script into the provided sessions. | ||
* | ||
|
@@ -45,14 +67,7 @@ export class PreloadInjection implements Integration { | |
* Attempts to add the preload script the the provided sessions | ||
*/ | ||
private _addPreloadToSessions(options: ElectronMainOptionsInternal): void { | ||
let path = undefined; | ||
try { | ||
path = rendererRequiresCrashReporterStart() | ||
? require.resolve('../../preload/legacy.js') | ||
: require.resolve('../../preload/index.js'); | ||
} catch (_) { | ||
// | ||
} | ||
const path = getPreloadPath(); | ||
|
||
if (path && typeof path === 'string' && isAbsolute(path) && existsSync(path)) { | ||
for (const sesh of options.getSessions()) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,5 @@ | ||
{ | ||
"extends": "./tsconfig.build.json", | ||
"include": [ | ||
"src/**/*.ts", | ||
"test/**/*.ts" | ||
], | ||
"exclude": [ | ||
"dist" | ||
], | ||
"compilerOptions": { | ||
"rootDir": ".", | ||
} | ||
"include": ["src/**/*.ts", "test/**/*.ts"], | ||
"exclude": ["node_modules"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "./tsconfig.build.json", | ||
"include": ["src/preload/**/*.ts"], | ||
"exclude": ["node_modules"], | ||
"compilerOptions": { | ||
"declaration": false, | ||
"declarationMap": false | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "./tsconfig.build.json", | ||
"include": ["src/**/*.ts", "test/**/*.ts"], | ||
"exclude": ["node_modules"], | ||
"compilerOptions": { | ||
"module": "CommonJS", | ||
"rootDir": "." | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when is app not defined?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this code gets loaded into the Electron renderer rather than the main process.
We have code that displays a warning if you load the wrong code into the wrong process. Rollup requires tha we target
module: esnext
and it then transpiles to CJS/ESM. The issue with this is that imports/side-effects are changed/moved and these bits of code get hit before our warning code!