-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
62 lines (54 loc) · 2.57 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import path from 'path'
import fs from 'fs-extra'
import type { Compiler, WebpackPluginInstance } from 'webpack'
interface ConstructorProps {
isServer: boolean | null;
keepServerSourcemaps: boolean | null;
silent: boolean | null;
}
export class DeleteSourceMapsPlugin implements WebpackPluginInstance {
readonly isServer: boolean | null = false
readonly keepServerSourcemaps: boolean | null = false
readonly silent: boolean | null = false
constructor({ isServer, keepServerSourcemaps, silent }: ConstructorProps = { isServer: null, keepServerSourcemaps: null, silent: true }) {
if (keepServerSourcemaps && isServer === null) throw new Error('You need to define the "isServer" value if you want to use "keepServerSourcemaps"')
this.isServer = isServer
this.keepServerSourcemaps = keepServerSourcemaps
this.silent = silent
}
apply(compiler: Compiler) {
compiler.hooks.environment.tap('DeleteSourceMaps', () => {
//console.log('WEBPACK SOURCEMAPS PLUGIN environment tap', this.isServer, this.keepServerSourcemaps)
if (this.isServer && this.keepServerSourcemaps) return
// sentry's config currently overrides the devtool value, so we can't set it to hidden-source-map easily
// see: https://github.com/getsentry/sentry-javascript/issues/3549
compiler.options.devtool =
compiler.options.name === 'server' ? false : 'hidden-source-map'
})
compiler.hooks.done.tapPromise('DeleteSourceMaps', async (stats) => {
try {
//console.log('WEBPACK SOURCEMAPS PLUGIN done tap', this.isServer, this.keepServerSourcemaps)
if (this.isServer && this.keepServerSourcemaps) return
const { compilation } = stats
const outputPath = compilation.outputOptions.path
const promises = Object
.keys(compilation.assets)
.filter((filename) => filename.endsWith('.js.map') || filename.endsWith('.css.map'))
.map((filename) => {
if (!outputPath) return Promise.resolve()
const filePath = path.join(outputPath, filename)
return fs.remove(filePath)
})
await Promise.all(promises)
const env = this.isServer ? 'server' : 'client'
if (!this.silent) {
if (this.isServer === null) console.info(`⚠️ Deleted ${promises.length} source map files`)
else console.info(`⚠️ Deleted ${promises.length} ${env} source map files`)
}
} catch (err) {
console.warn('⚠️ DeleteSourceMapsPlugin: Error while deleting source maps after the build')
console.error(err)
}
})
}
}