-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sourcemaps): Automatically insert Sentry Vite plugin in Vite con…
…fig (#382) This PR adds automatic insertion of the Sentry Vite plugin into users' vite config files: * Check if `vite.config.(js|ts|cjs|mts)` can be found * If found, add plugin and import (`magicast`) * If not found or error during insertion, fall back to copy/paste instructions * Collect telemetry around sucess/failure of modification and failure reasons
- Loading branch information
Showing
7 changed files
with
190 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import * as fs from 'fs'; | ||
// @ts-ignore - magicast is ESM and TS complains about that. It works though | ||
import { ProxifiedModule } from 'magicast'; | ||
|
||
/** | ||
* Checks if a JS/TS file where we don't know its concrete file type yet exists | ||
* and returns the full path to the file with the correct file type. | ||
*/ | ||
export function findScriptFile(hooksFile: string): string | undefined { | ||
const possibleFileTypes = ['.js', '.ts', '.mjs']; | ||
return possibleFileTypes | ||
.map((type) => `${hooksFile}${type}`) | ||
.find((file) => fs.existsSync(file)); | ||
} | ||
|
||
/** Checks if a Sentry package is already mentioned in the file */ | ||
export function hasSentryContent(mod: ProxifiedModule<object>): boolean { | ||
const imports = mod.imports.$items.map((i) => i.from); | ||
return !!imports.find((i) => i.startsWith('@sentry/')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//@ts-ignore | ||
import { parseModule } from 'magicast'; | ||
import { hasSentryContent } from '../../src/utils/ast-utils'; | ||
|
||
describe('AST utils', () => { | ||
describe('hasSentryContent', () => { | ||
it("returns true if a '@sentry/' import was found in the parsed module", () => { | ||
const code = ` | ||
import { sentryVitePlugin } from "@sentry/vite-plugin"; | ||
import * as somethingelse from 'gs'; | ||
export default { | ||
plugins: [sentryVitePlugin()] | ||
} | ||
`; | ||
|
||
expect(hasSentryContent(parseModule(code))).toBe(true); | ||
}); | ||
it.each([ | ||
` | ||
import * as somethingelse from 'gs'; | ||
export default { | ||
plugins: [] | ||
} | ||
`, | ||
`import * as somethingelse from 'gs'; | ||
// import { sentryVitePlugin } from "@sentry/vite-plugin" | ||
export default { | ||
plugins: [] | ||
} | ||
`, | ||
`import * as thirdPartyVitePlugin from "vite-plugin-@sentry" | ||
export default { | ||
plugins: [thirdPartyVitePlugin()] | ||
} | ||
`, | ||
])( | ||
"reutrns false for modules without a valid '@sentry/' import", | ||
(code) => { | ||
expect(hasSentryContent(parseModule(code))).toBe(false); | ||
}, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
"bin.ts", | ||
"lib/**/*", | ||
"spec/**/*", | ||
"src/**/*" | ||
"src/**/*", | ||
"test/**/*" | ||
] | ||
} |