Skip to content

Commit ab69424

Browse files
committed
build: Fix webpack terser configurations
1 parent fbdc752 commit ab69424

File tree

7 files changed

+412
-2742
lines changed

7 files changed

+412
-2742
lines changed

monkey.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ export default {
44
name: 'Good Monkey',
55
description: 'A good monkey',
66
version: '1.0.0',
7-
match: ['http*://*.google.com/*', 'http*://google.com/*'],
7+
match: ['http*://*.github.com/*'],
88
grant: ['GM_addStyle']
99
} as Monkey.Metadata

package.json

+18-19
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"description": "",
55
"main": "dist/good-monkey.user.js",
66
"scripts": {
7-
"build": "webpack",
8-
"dev": "webpack -c webpack.dev.config.ts",
7+
"build": "webpack && prettier --write dist/*.js",
8+
"dev": "webpack --watch -c webpack.dev.config.ts",
99
"format": "prettier --write **/*.ts"
1010
},
1111
"repository": {
@@ -24,24 +24,23 @@
2424
"homepage": "https://github.com/SettingDust/webpack-tampermonkey#readme",
2525
"devDependencies": {
2626
"@tsconfig/recommended": "^1.0.1",
27-
"@types/node": "^15.12.1",
28-
"@types/webpack-dev-server": "^3.11.4",
29-
"autoprefixer": "^10.2.6",
30-
"chalk": "^4.1.1",
31-
"css-loader": "^5.2.6",
32-
"cssnano": "^5.0.5",
33-
"postcss": "^8.3.0",
34-
"postcss-loader": "^5.3.0",
35-
"prettier": "^2.3.0",
36-
"sass": "^1.34.1",
37-
"sass-loader": "^12.0.0",
27+
"@types/node": "^16.7.13",
28+
"autoprefixer": "^10.3.4",
29+
"chalk": "^4.1.2",
30+
"css-loader": "^6.2.0",
31+
"cssnano": "^5.0.8",
32+
"postcss": "^8.3.6",
33+
"postcss-loader": "^6.1.1",
34+
"prettier": "^2.3.2",
35+
"sass": "^1.39.0",
36+
"sass-loader": "^12.1.0",
37+
"terser-webpack-plugin": "^5.2.3",
3838
"to-string-loader": "github:gajus/to-string-loader",
39-
"ts-loader": "^9.2.2",
40-
"ts-node": "^10.0.0",
41-
"typescript": "^4.3.2",
39+
"ts-loader": "^9.2.5",
40+
"ts-node": "^10.2.1",
41+
"typescript": "^4.4.2",
4242
"url-loader": "^4.1.1",
43-
"webpack": "^5.38.1",
44-
"webpack-cli": "^4.7.0",
45-
"webpack-dev-server": "^3.11.2"
43+
"webpack": "^5.52.0",
44+
"webpack-cli": "^4.8.0"
4645
}
4746
}

scripts/banner.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import metadata from '../monkey.config'
22

3-
const paddingLength =
4-
1 + Object.keys(metadata).reduce((prev, curr) => (curr.length > prev ? curr.length : prev), 0)
3+
const paddingLength = 1 + Object.keys(metadata).reduce((prev, curr) => (curr.length > prev ? curr.length : prev), 0)
54

65
const prefix = (arr: string[]): string[] => {
76
const newArr = arr.map((it) => `// ${it}`)
@@ -19,9 +18,8 @@ const sign = (arr: string[]): string[] => {
1918
export default (dev: boolean = false): string => {
2019
const metas: string[] = []
2120
if (dev) {
22-
if (!metadata.require)
23-
metadata.require = []
24-
metadata.require.push('file://' + __dirname + '\\build\\' + metadata.name.toLowerCase().replace(' ', '-') + '.js')
21+
if (!metadata.require) metadata.require = []
22+
metadata.require.push('file://' + __dirname + '\\build\\' + metadata.name.toLowerCase().replace(' ', '-') + '.user.js')
2523
}
2624
for (const [key, value] of Object.entries(metadata)) {
2725
if (Array.isArray(value) && value.length) {

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
GM_addStyle(require('./styles/style.scss'))
2+
GM_log(GM_addStyle, require('./styles/style.scss'))

webpack.config.ts

+16-13
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,29 @@ import banner from './scripts/banner'
55
import metadata from './monkey.config'
66
import LimitChunkCountPlugin = webpack.optimize.LimitChunkCountPlugin
77
import BannerPlugin = webpack.BannerPlugin
8+
import TerserPlugin from 'terser-webpack-plugin'
89

910
// noinspection JSUnusedGlobalSymbols
1011
export default {
1112
entry: './src/index.ts',
1213
output: {
1314
filename: metadata.name.toLowerCase().replace(' ', '-') + '.user.js'
1415
},
15-
mode: 'production',
16+
mode: 'none',
17+
optimization: {
18+
minimize: true,
19+
minimizer: [
20+
new TerserPlugin({
21+
terserOptions: {
22+
mangle: false,
23+
format: {
24+
comments: (node, comment) => comment.type === 'comment1'
25+
&& /(^\s==\/?UserScript==)|(^\s@.+\s+.+$)|(^\s$)/.test(comment.value)
26+
}
27+
}
28+
})
29+
]
30+
},
1631
module: {
1732
rules: [
1833
{
@@ -29,18 +44,6 @@ export default {
2944
}
3045
]
3146
},
32-
optimization: {
33-
minimizer: [
34-
() => ({
35-
terserOptions: {
36-
mangle: false,
37-
output: {
38-
beautify: true
39-
}
40-
}
41-
})
42-
]
43-
},
4447
plugins: [
4548
new LimitChunkCountPlugin({ maxChunks: 1 }),
4649
new BannerPlugin({

webpack.dev.config.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,15 @@ export default {
1717
entry: './src/index.ts',
1818
output: {
1919
path: path.resolve(__dirname, 'build'),
20-
filename: metadata.name.toLowerCase().replace(' ', '-') + '.js'
20+
filename: metadata.name.toLowerCase().replace(' ', '-') + '.user.js'
2121
},
2222
mode: 'development',
2323
devtool: 'inline-source-map',
2424
module: {
2525
rules: [
2626
{
2727
test: /\.tsx?$/,
28-
use: {
29-
loader: 'ts-loader',
30-
options: {
31-
compilerOptions: {
32-
sourceMap: true
33-
}
34-
}
35-
}
28+
use: 'ts-loader'
3629
},
3730
{
3831
test: /\.s[ac]ss$/i,

0 commit comments

Comments
 (0)