8000 :sparkles: add ability to render headers prettily · momocow/webpack-userscript@14d6497 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 14d6497

Browse files
committed
✨ add ability to render headers prettily
1 parent 8eac19c commit 14d6497

File tree

5 files changed

+40
-20
lines changed

5 files changed

+40
-20
lines changed

lib/headers.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
IsUrl,
1515
validateSync,
1616
} from 'class-validator';
17+
import { getBorderCharacters, table } from 'table';
1718

1819
export interface HeadersFactoryOptions {
1920
strict: boolean;
@@ -251,34 +252,45 @@ export class HeadersImpl implements StrictHeadersProps {
251252
}
252253

253254
public render({
254-
prefix = '// ==UserScript==',
255-
suffix = '// ==/UserScript==',
255+
prefix = '// ==UserScript==\n',
256+
suffix = '// ==/UserScript==\n',
257+
pretty = false,
256258
}: HeadersRenderOptions = {}): string {
257259
const obj = instanceToPlain(this, { exposeUnsetFields: false }) as Record<
258260
TagType,
259261
Exclude<ValueType, undefined>
260262
>;
261-
const body = Object.entries(obj)
262-
.map(([tag, value]) => this.renderTag(tag, value))
263-
.join('\n');
264-
return [prefix, body, suffix].join('\n');
263+
const rows = Object.entries(obj).flatMap(([tag, value]) =>
264+
this.renderTag(tag, value),
265+
);
266+
267+
const body = pretty
268+
? table(rows, {
269+
border: getBorderCharacters('void'),
270+
columnDefault: {
271+
paddingLeft: 0,
272+
paddingRight: 1,
273+
},
274+
drawHorizontalLine: () => false,
275+
})
276+
: rows.map((cols) => cols.join(' ')).join('\n') + '\n';
277+
278+
return prefix + body + suffix;
265279
}
266280

267281
protected renderTag(
268282
tag: TagType,
269283
value: Exclude<ValueType, undefined>,
270-
): string {
284+
): string[][] {
271285
if (Array.isArray(value)) {
272-
return value.map((v) => `// @${tag} ${v}`).join('\n');
286+
return value.map((v) => ['//', `@${tag}`, v]);
273287
}
274288

275289
if (typeof value === 'object') {
276-
return Object.entries(value)
277-
.map(([k, v]) => `// @${tag} ${k} ${v}`)
278-
.join('\n');
290+
return Object.entries(value).map(([k, v]) => ['//', `@${tag}`, k, v]);
279291
}
280292

281-
return `// @${tag} ${String(value)}`;
293+
return [['//', `@${tag}`, String(value)]];
282294
}
283295

284296
public static fromJSON<T extends HeadersImpl>(

lib/plugin.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,12 @@ import {
2222
const { ConcatSource, RawSource } = sources;
2323

2424
export class UserscriptPlugin {
25-
public static readonly DEFAULT_OPTIONS: Readonly<UserscriptOptions> = {};
2625
public readonly hooks = {
2726
processHeaders: new AsyncSeriesWaterfallHook<HeadersWaterfall>(['headers']),
2827
};
2928

30-
// protected readonly headersCache = new WeakMap<Source, CacheEntry>();
31-
3229
public constructor(
33-
public options: UserscriptOptions = {
34-
...UserscriptPlugin.DEFAULT_OPTIONS,
35-
},
30+
public readonly options: Readonly<UserscriptOptions> = {},
3631
) {}
3732

3833
public apply(compiler: Compiler): void {
@@ -218,7 +213,7 @@ export class UserscriptPlugin {
218213
data: CompilerData,
219214
fileInfo: FileInfo,
220215
): Promise<void> {
221-
const { headers: headersOption } = this.options;
216+
const { headers: headersOption, prefix, pretty, suffix } = this.options;
222217

223218
let headers = data.headers;
224219
if (typeof headersOption === 'function') {
@@ -237,7 +232,11 @@ export class UserscriptPlugin {
237232
data.ssriLock = ssriLock;
238233

239234
const { originalFile, chunk, metajsFile, userjsFile } = fileInfo;
240-
const headersStr = headers.render();
235+
const headersStr = headers.render({
236+
prefix,
237+
pretty,
238+
suffix,
239+
});
241240
const sourceAsset = compilation.getAsset(originalFile);
242241
if (!sourceAsset) {
243242
return;

lib/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ export interface UserscriptOptions {
88
root?: string;
99
metajs?: boolean;
1010
headers?: HeadersOption;
11+
pretty?: boolean;
12+
prefix?: string;
13+
suffix?: string;
1114
strict?: boolean;
1215
whitelist?: boolean;
1316
downloadBaseUrl?: string;

package-lock.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,8 @@
7676
"ssri": "^10.0.1",
7777
"table": "^6.8.1",
7878
"tapable": "^2.2.1"
79+
},
80+
"peerDependencies": {
81+
"webpack": "5"
7982
}
8083
}

0 commit comments

Comments
 (0)
0