-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.ts
63 lines (56 loc) · 1.85 KB
/
webpack.config.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
62
63
import * as webpack from 'webpack';
import { validate } from 'jsonschema';
import pad = require('pad');
const metadataPath = './src/meta.json';
const metadata = require(metadataPath);
const metadataSchema = require('./meta.schema.json');
interface IMetadata {
[key: string]: string | boolean | string[];
}
function generateHeader(metadata: IMetadata) {
const validateResult = validate(metadata, metadataSchema);
if (!validateResult.valid) {
throw new Error(`The script metadata at ${metadataPath} is not valid.\n${validateResult}`);
}
const lines: string[] = [];
const padLength = Math.max(...Object.keys(metadata).map(k => k.length));
const makeLine = (key: string, value: string) => `// @${pad(key, padLength)} ${value}`;
lines.push('// ==UserScript==');
for (let key of Object.keys(metadata)) {
if (key[0] === '$') continue;
const value = metadata[key];
if (Array.isArray(value)) {
for (let subValue of value) {
lines.push(makeLine(key, subValue));
}
} else if (typeof (value) === 'string') {
lines.push(makeLine(key, value));
} else if (typeof (value) === 'boolean' && value) {
lines.push(makeLine(key, ''));
}
}
lines.push('// ==/UserScript==\n');
return lines.join('\n');
}
export default <webpack.Configuration>{
entry: './src/index.ts',
output: {
filename: `../douki.user.js`
},
resolve: {
extensions: ['.ts', '.tsx', '.js']
},
mode: 'none',
module: {
rules: [
{ test: /\.tsx?$/, use: 'ts-loader' }
]
},
plugins: [
new webpack.BannerPlugin({
banner: generateHeader(metadata),
raw: true,
entryOnly: true
})
]
};