Skip to content

Commit

Permalink
Merge pull request #328 from ikenfin/feature/issue-1__add_option_to_s…
Browse files Browse the repository at this point in the history
…trip_sourcemaps

Feature/issue 1  add option to strip sourcemaps
  • Loading branch information
ikenfin authored Jun 15, 2023
2 parents 95055fd + f843ef3 commit 36e501f
Show file tree
Hide file tree
Showing 5 changed files with 439 additions and 399 deletions.
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

0 comments on commit 36e501f

Please sign in to comment.