Skip to content
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

Feature/issue 1 add option to strip sourcemaps #328

Merged
merged 5 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ export interface ViteSentryPluginOptions extends ViteSentryCliOptions, ViteSentr
*/
legacyErrorHandlingMode?: boolean

/**
When is `true` - will drop sourcemap files from resulting bundle
This option works only for Vite >= 4.0.0
For earlier Vite versions see https://github.com/ikenfin/vite-plugin-sentry/issues/1
*/
cleanSourcemapsAfterUpload?: boolean

/*
Remove all artifacts in the release befire upload
*/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@
"vite": "^2.6.0 || ^3.0.0 || ^4.0.0"
},
"engines": {
"node": ">= 12"
"node": ">= 14"
}
}
2 changes: 1 addition & 1 deletion rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const pkg = require('./package.json')
*/
export default {
input: 'src/index.ts',
external: [ 'util', 'vite', '@sentry/cli' ],
external: [ 'util', 'vite', '@sentry/cli', 'node:path', 'node:fs/promises' ],
output: [
{
format: 'es',
Expand Down
39 changes: 36 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
import type { Plugin } from 'vite'
import type { ViteSentryPluginOptions } from '..'

import { unlink } from 'node:fs/promises'
import path from 'node:path'

import { createSentryCli } from './lib/create-cli'
import { getReleasePromise } from './lib/get-release-promise'

const MODULE_ID = 'virtual:vite-plugin-sentry/sentry-config'
const RESOLVED_ID = '\0' + MODULE_ID

export default function ViteSentry (options: ViteSentryPluginOptions) {
const { skipEnvironmentCheck = false, legacyErrorHandlingMode = false } = options
const {
skipEnvironmentCheck = false,
cleanSourcemapsAfterUpload = false,
legacyErrorHandlingMode = false
} = options

const cli = createSentryCli(options)
const currentReleasePromise = getReleasePromise(cli, options)

// plugin state
let pluginState = {
enabled: false,
isProduction: false,
sourcemapsCreated: false,
isProduction: false
baseDir: '',
sourcemapsFilePaths: [] as string[]
}

const viteSentryPlugin: Plugin = {
Expand Down Expand Up @@ -80,6 +89,17 @@ export default function ViteSentry (options: ViteSentryPluginOptions) {
}
},

generateBundle (options, bundle) {
if (cleanSourcemapsAfterUpload) {
pluginState.baseDir = options.dir ?? ''
for (const file in bundle) {
if (file.endsWith('.map')) {
pluginState.sourcemapsFilePaths.push(file)
}
}
}
},

/*
We starting plugin here, because at the moment vite completed with building
so sourcemaps must be ready
Expand Down Expand Up @@ -108,7 +128,9 @@ export default function ViteSentry (options: ViteSentryPluginOptions) {
const currentRelease = await currentReleasePromise

if (!currentRelease) {
reportSentryError('Release returned from sentry is empty! Please check your config')
reportSentryError(
'Release returned from sentry is empty! Please check your config'
)
}
else {
try {
Expand Down Expand Up @@ -149,6 +171,17 @@ export default function ViteSentry (options: ViteSentryPluginOptions) {
if (options.deploy && options.deploy.env) {
await cli.releases.newDeploy(currentRelease, options.deploy)
}

if (
cleanSourcemapsAfterUpload &&
pluginState.sourcemapsCreated &&
pluginState.sourcemapsFilePaths.length > 0
) {
for (const file of pluginState.sourcemapsFilePaths) {
this.info(`Deleting sourcemap file: ${file}`)
await unlink(path.join(pluginState.baseDir, file))
}
}
}
catch (error) {
reportSentryError(
Expand Down
Loading