webpack-userscript
Advanced tools
export interface HeadersFactoryOptions { | ||
strict: boolean; | ||
whitelist: boolean; | ||
whitelist?: boolean; | ||
} | ||
export declare enum RunAtValue { | ||
DocumentStart = "document-start", | ||
DocumentBody = "document-body", | ||
DocumentEnd = "document-end", | ||
DocumentIdle = "document-idle", | ||
ContextMenu = "context-menu" | ||
} | ||
export interface HeadersRenderOptions { | ||
@@ -17,2 +9,5 @@ prefix?: string; | ||
} | ||
export interface HeadersValidateOptions { | ||
whitelist?: boolean; | ||
} | ||
export type TagType = string; | ||
@@ -24,2 +19,9 @@ export type ValueType = Record<string, string> | string[] | string | boolean | undefined; | ||
export type SwitchValue = boolean; | ||
export declare enum RunAtValue { | ||
DocumentStart = "document-start", | ||
DocumentBody = "document-body", | ||
DocumentEnd = "document-end", | ||
DocumentIdle = "document-idle", | ||
ContextMenu = "context-menu" | ||
} | ||
export interface StrictHeadersProps { | ||
@@ -60,4 +62,3 @@ name?: SingleValue; | ||
} | ||
export type Headers = Readonly<HeadersImpl>; | ||
export declare class HeadersImpl implements StrictHeadersProps { | ||
export declare class Headers implements StrictHeadersProps { | ||
protected constructor(); | ||
@@ -95,6 +96,6 @@ readonly name: SingleValue; | ||
toJSON(): HeadersProps; | ||
update(props: HeadersProps): Headers; | ||
render({ prefix, suffix, }?: HeadersRenderOptions): string; | ||
protected renderTag(tag: TagType, value: Exclude<ValueType, undefined>): string; | ||
static fromJSON<T extends HeadersImpl>(props: HeadersProps, { strict, whitelist }?: Partial<HeadersFactoryOptions>): Readonly<T>; | ||
render({ prefix, suffix, pretty, }?: HeadersRenderOptions): string; | ||
protected renderTag(tag: TagType, value: ValueType): string[][]; | ||
validate({ whitelist }?: HeadersValidateOptions): void; | ||
static fromJSON<T extends Headers>(props: HeadersProps, { whitelist }?: HeadersFactoryOptions): Readonly<T>; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.HeadersImpl = exports.RunAtValue = void 0; | ||
exports.Headers = exports.RunAtValue = void 0; | ||
const tslib_1 = require("tslib"); | ||
const class_transformer_1 = require("class-transformer"); | ||
const class_validator_1 = require("class-validator"); | ||
const table_1 = require("table"); | ||
var RunAtValue; | ||
@@ -15,3 +16,3 @@ (function (RunAtValue) { | ||
})(RunAtValue = exports.RunAtValue || (exports.RunAtValue = {})); | ||
class HeadersImpl { | ||
class Headers { | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
@@ -22,24 +23,43 @@ constructor() { } | ||
} | ||
update(props) { | ||
return this.constructor.fromJSON(Object.assign(Object.assign({}, this.toJSON()), props)); | ||
render({ prefix = '// ==UserScript==\n', suffix = '// ==/UserScript==\n', pretty = false, } = {}) { | ||
const obj = this.toJSON(); | ||
const rows = Object.entries(obj).flatMap(([tag, value]) => this.renderTag(tag, value)); | ||
const body = pretty | ||
? (0, table_1.table)(rows, { | ||
border: (0, table_1.getBorderCharacters)('void'), | ||
columnDefault: { | ||
paddingLeft: 0, | ||
paddingRight: 1, | ||
}, | ||
drawHorizontalLine: () => false, | ||
}) | ||
: rows.map((cols) => cols.join(' ')).join('\n') + '\n'; | ||
return prefix + body + suffix; | ||
} | ||
render({ prefix = '// ==UserScript==', suffix = '// ==/UserScript==', } = {}) { | ||
const obj = (0, class_transformer_1.instanceToPlain)(this, { exposeUnsetFields: false }); | ||
const body = Object.entries(obj) | ||
.map(([tag, value]) => this.renderTag(tag, value)) | ||
.join('\n'); | ||
return [prefix, body, suffix].join('\n'); | ||
} | ||
renderTag(tag, value) { | ||
if (Array.isArray(value)) { | ||
return value.map((v) => `// @${tag} ${v}`).join('\n'); | ||
return value.map((v) => ['//', `@${tag}`, v]); | ||
} | ||
if (typeof value === 'object') { | ||
return Object.entries(value) | ||
.map(([k, v]) => `// @${tag} ${k} ${v}`) | ||
.join('\n'); | ||
return Object.entries(value).map(([k, v]) => ['//', `@${tag}`, k, v]); | ||
} | ||
return `// @${tag} ${String(value)}`; | ||
if (typeof value === 'string') { | ||
return [['//', `@${tag}`, value]]; | ||
} | ||
if (value === true) { | ||
return [['//', `@${tag}`]]; | ||
} | ||
return []; | ||
} | ||
static fromJSON(props, { strict = false, whitelist = false } = {}) { | ||
validate({ whitelist } = {}) { | ||
const errors = (0, class_validator_1.validateSync)(this, { | ||
forbidNonWhitelisted: whitelist, | ||
whitelist, | ||
stopAtFirstError: false, | ||
}); | ||
if (errors.length > 0) { | ||
throw new Error(errors.map((err) => err.toString()).join('\n')); | ||
} | ||
} | ||
static fromJSON(props, { whitelist = false } = {}) { | ||
const headers = (0, class_transformer_1.plainToInstance)(this, props, { | ||
@@ -49,12 +69,2 @@ exposeDefaultValues: true, | ||
}); | ||
if (strict) { | ||
const errors = (0, class_validator_1.validateSync)(headers, { | ||
forbidNonWhitelisted: whitelist, | ||
whitelist, | ||
stopAtFirstError: false, | ||
}); | ||
if (errors.length > 0) { | ||
throw new Error(errors.map((err) => err.toString()).join('\n')); | ||
} | ||
} | ||
return headers; | ||
@@ -67,3 +77,3 @@ } | ||
tslib_1.__metadata("design:type", String) | ||
], HeadersImpl.prototype, "name", void 0); | ||
], Headers.prototype, "name", void 0); | ||
tslib_1.__decorate([ | ||
@@ -74,3 +84,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", String) | ||
], HeadersImpl.prototype, "version", void 0); | ||
], Headers.prototype, "version", void 0); | ||
tslib_1.__decorate([ | ||
@@ -81,3 +91,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", String) | ||
], HeadersImpl.prototype, "namespace", void 0); | ||
], Headers.prototype, "namespace", void 0); | ||
tslib_1.__decorate([ | ||
@@ -88,3 +98,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", String) | ||
], HeadersImpl.prototype, "author", void 0); | ||
], Headers.prototype, "author", void 0); | ||
tslib_1.__decorate([ | ||
@@ -95,3 +105,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", String) | ||
], HeadersImpl.prototype, "description", void 0); | ||
], Headers.prototype, "description", void 0); | ||
tslib_1.__decorate([ | ||
@@ -102,3 +112,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", String) | ||
], HeadersImpl.prototype, "homepage", void 0); | ||
], Headers.prototype, "homepage", void 0); | ||
tslib_1.__decorate([ | ||
@@ -109,3 +119,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", String) | ||
], HeadersImpl.prototype, "homepageURL", void 0); | ||
], Headers.prototype, "homepageURL", void 0); | ||
tslib_1.__decorate([ | ||
@@ -116,3 +126,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", String) | ||
], HeadersImpl.prototype, "website", void 0); | ||
], Headers.prototype, "website", void 0); | ||
tslib_1.__decorate([ | ||
@@ -123,3 +133,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", String) | ||
], HeadersImpl.prototype, "source", void 0); | ||
], Headers.prototype, "source", void 0); | ||
tslib_1.__decorate([ | ||
@@ -130,3 +140,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", String) | ||
], HeadersImpl.prototype, "icon", void 0); | ||
], Headers.prototype, "icon", void 0); | ||
tslib_1.__decorate([ | ||
@@ -137,3 +147,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", String) | ||
], HeadersImpl.prototype, "iconURL", void 0); | ||
], Headers.prototype, "iconURL", void 0); | ||
tslib_1.__decorate([ | ||
@@ -144,3 +154,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", String) | ||
], HeadersImpl.prototype, "defaulticon", void 0); | ||
], Headers.prototype, "defaulticon", void 0); | ||
tslib_1.__decorate([ | ||
@@ -151,3 +161,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", String) | ||
], HeadersImpl.prototype, "icon64", void 0); | ||
], Headers.prototype, "icon64", void 0); | ||
tslib_1.__decorate([ | ||
@@ -158,3 +168,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", String) | ||
], HeadersImpl.prototype, "icon64URL", void 0); | ||
], Headers.prototype, "icon64URL", void 0); | ||
tslib_1.__decorate([ | ||
@@ -165,3 +175,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", String) | ||
], HeadersImpl.prototype, "updateURL", void 0); | ||
], Headers.prototype, "updateURL", void 0); | ||
tslib_1.__decorate([ | ||
@@ -172,3 +182,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", String) | ||
], HeadersImpl.prototype, "downloadURL", void 0); | ||
], Headers.prototype, "downloadURL", void 0); | ||
tslib_1.__decorate([ | ||
@@ -179,3 +189,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", String) | ||
], HeadersImpl.prototype, "installURL", void 0); | ||
], Headers.prototype, "installURL", void 0); | ||
tslib_1.__decorate([ | ||
@@ -186,3 +196,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", String) | ||
], HeadersImpl.prototype, "supportURL", void 0); | ||
], Headers.prototype, "supportURL", void 0); | ||
tslib_1.__decorate([ | ||
@@ -193,3 +203,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", Object) | ||
], HeadersImpl.prototype, "include", void 0); | ||
], Headers.prototype, "include", void 0); | ||
tslib_1.__decorate([ | ||
@@ -200,3 +210,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", Object) | ||
], HeadersImpl.prototype, "match", void 0); | ||
], Headers.prototype, "match", void 0); | ||
tslib_1.__decorate([ | ||
@@ -207,3 +217,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", Object) | ||
], HeadersImpl.prototype, "exclude", void 0); | ||
], Headers.prototype, "exclude", void 0); | ||
tslib_1.__decorate([ | ||
@@ -214,3 +224,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", Object) | ||
], HeadersImpl.prototype, "require", void 0); | ||
], Headers.prototype, "require", void 0); | ||
tslib_1.__decorate([ | ||
@@ -221,3 +231,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", Object) | ||
], HeadersImpl.prototype, "resource", void 0); | ||
], Headers.prototype, "resource", void 0); | ||
tslib_1.__decorate([ | ||
@@ -228,3 +238,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", Object) | ||
], HeadersImpl.prototype, "connect", void 0); | ||
], Headers.prototype, "connect", void 0); | ||
tslib_1.__decorate([ | ||
@@ -235,3 +245,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", Object) | ||
], HeadersImpl.prototype, "grant", void 0); | ||
], Headers.prototype, "grant", void 0); | ||
tslib_1.__decorate([ | ||
@@ -242,3 +252,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", Object) | ||
], HeadersImpl.prototype, "webRequest", void 0); | ||
], Headers.prototype, "webRequest", void 0); | ||
tslib_1.__decorate([ | ||
@@ -249,3 +259,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", Boolean) | ||
], HeadersImpl.prototype, "noframes", void 0); | ||
], Headers.prototype, "noframes", void 0); | ||
tslib_1.__decorate([ | ||
@@ -256,3 +266,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", Boolean) | ||
], HeadersImpl.prototype, "unwrap", void 0); | ||
], Headers.prototype, "unwrap", void 0); | ||
tslib_1.__decorate([ | ||
@@ -263,3 +273,3 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", Object) | ||
], HeadersImpl.prototype, "antifeature", void 0); | ||
], Headers.prototype, "antifeature", void 0); | ||
tslib_1.__decorate([ | ||
@@ -270,4 +280,4 @@ (0, class_transformer_1.Expose)(), | ||
tslib_1.__metadata("design:type", String) | ||
], HeadersImpl.prototype, 'run-at', void 0); | ||
exports.HeadersImpl = HeadersImpl; | ||
], Headers.prototype, 'run-at', void 0); | ||
exports.Headers = Headers; | ||
//# sourceMappingURL=headers.js.map |
import 'reflect-metadata'; | ||
import { UserscriptPlugin } from './plugin'; | ||
export default UserscriptPlugin; | ||
export * from './headers'; | ||
export * from './plugin'; | ||
export * from './types'; |
@@ -7,3 +7,5 @@ "use strict"; | ||
exports.default = plugin_1.UserscriptPlugin; | ||
tslib_1.__exportStar(require("./headers"), exports); | ||
tslib_1.__exportStar(require("./plugin"), exports); | ||
tslib_1.__exportStar(require("./types"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -6,12 +6,14 @@ import { AsyncSeriesWaterfallHook } from 'tapable'; | ||
export declare class UserscriptPlugin { | ||
options: UserscriptOptions; | ||
static readonly DEFAULT_OPTIONS: Readonly<UserscriptOptions>; | ||
readonly options: Readonly<UserscriptOptions>; | ||
readonly hooks: { | ||
processHeaders: AsyncSeriesWaterfallHook<HeadersWaterfall, import("tapable").UnsetAdditionalOptions>; | ||
processProxyHeaders: AsyncSeriesWaterfallHook<HeadersWaterfall, import("tapable").UnsetAdditionalOptions>; | ||
}; | ||
constructor(options?: UserscriptOptions); | ||
constructor(options?: Readonly<UserscriptOptions>); | ||
apply(compiler: Compiler): void; | ||
protected headersFactory(props: HeadersProps): Headers; | ||
protected applyHooks(): void; | ||
protected headersFactory(props: HeadersProps): Readonly<Headers>; | ||
protected loadDefault({ context, inputFileSystem, }: Compiler): Promise<HeadersProps>; | ||
protected prepare(compiler: Compiler): Promise<CompilerData>; | ||
protected init(compiler: Compiler): Promise<CompilerData>; | ||
protected prepare(compilation: Compilation, data: CompilerData): Promise<void>; | ||
protected emit(compilation: Compilation, data: CompilerData): Promise<void>; | ||
@@ -23,3 +25,5 @@ protected shutdown(_: Compiler, { lockfile, ssriLock }: CompilerData): Promise<void>; | ||
interface CompilerData { | ||
headers: Headers; | ||
buildNo: number; | ||
headers: HeadersProps; | ||
headersFile?: string; | ||
ssriLock?: SSRILock; | ||
@@ -26,0 +30,0 @@ lockfile?: string; |
@@ -10,11 +10,15 @@ "use strict"; | ||
const headers_1 = require("./headers"); | ||
const hooks_1 = require("./hooks"); | ||
const ssri_1 = require("./ssri"); | ||
const hook_1 = require("./hook"); | ||
const reducers_1 = require("./reducers"); | ||
const proxy_script_1 = require("./reducers/proxy-script"); | ||
const util_1 = require("./util"); | ||
const { ConcatSource, RawSource } = webpack_1.sources; | ||
class UserscriptPlugin { | ||
// protected readonly headersCache = new WeakMap<Source, CacheEntry>(); | ||
constructor(options = Object.assign({}, UserscriptPlugin.DEFAULT_OPTIONS)) { | ||
constructor(options = {}) { | ||
this.options = options; | ||
this.hooks = { | ||
processHeaders: new tapable_1.AsyncSeriesWaterfallHook(['headers']), | ||
processProxyHeaders: new tapable_1.AsyncSeriesWaterfallHook([ | ||
'headers', | ||
]), | ||
}; | ||
@@ -24,34 +28,55 @@ } | ||
const PLUGIN = this.constructor.name; | ||
let compilerData; | ||
let data; | ||
compiler.hooks.beforeCompile.tapPromise(PLUGIN, () => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
compilerData = yield this.prepare(compiler); | ||
if (!data) { | ||
data = yield this.init(compiler); | ||
} | ||
})); | ||
compiler.hooks.compilation.tap(PLUGIN, (compilation) => { | ||
compilation.hooks.processAssets.tapPromise({ name: PLUGIN, stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_PRE_PROCESS }, () => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
if (!data) | ||
return; | ||
yield this.prepare(compilation, data); | ||
})); | ||
compilation.hooks.processAssets.tapPromise({ | ||
name: PLUGIN, | ||
// we should generate userscript files | ||
// only if optimization of source files are complete | ||
stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE, | ||
}, () => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
if (!compilerData) | ||
if (!data) | ||
return; | ||
yield this.emit(compilation, compilerData); | ||
yield this.emit(compilation, data); | ||
})); | ||
}); | ||
compiler.hooks.shutdown.tapPromise(PLUGIN, () => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
if (!compilerData) | ||
if (!data) | ||
return; | ||
yield this.shutdown(compiler, compilerData); | ||
yield this.shutdown(compiler, data); | ||
})); | ||
this.hooks.processHeaders.tap(PLUGIN, (0, hooks_1.wrapHook)(hooks_1.setDefaultMatch)); | ||
if (this.options.downloadBaseUrl !== undefined) { | ||
this.hooks.processHeaders.tap(PLUGIN, (0, hooks_1.wrapHook)(hooks_1.resolveDownloadBaseUrl)); | ||
this.applyHooks(); | ||
} | ||
applyHooks() { | ||
const { downloadBaseUrl, updateBaseUrl, ssri, headers, proxyScript } = this.options; | ||
if (typeof headers === 'function') { | ||
this.hooks.processHeaders.tapPromise(headers.name, (0, hook_1.wrapHook)((data) => tslib_1.__awaiter(this, void 0, void 0, function* () { return headers(data); }))); | ||
} | ||
if (this.options.updateBaseUrl !== undefined) { | ||
this.hooks.processHeaders.tap(PLUGIN, (0, hooks_1.wrapHook)(hooks_1.resolveUpdateBaseUrl)); | ||
this.hooks.processHeaders.tap(reducers_1.fixTagNames.name, (0, hook_1.wrapHook)(reducers_1.fixTagNames)); | ||
if (downloadBaseUrl !== undefined) { | ||
this.hooks.processHeaders.tap(reducers_1.resolveDownloadBaseUrl.name, (0, hook_1.wrapHook)(reducers_1.resolveDownloadBaseUrl)); | ||
} | ||
if (this.options.ssri) { | ||
this.hooks.processHeaders.tapPromise(PLUGIN, (0, hooks_1.wrapHook)(ssri_1.processSSRI)); | ||
if (updateBaseUrl !== undefined) { | ||
this.hooks.processHeaders.tap(reducers_1.resolveUpdateBaseUrl.name, (0, hook_1.wrapHook)(reducers_1.resolveUpdateBaseUrl)); | ||
} | ||
if (ssri) { | ||
this.hooks.processHeaders.tapPromise(reducers_1.processSSRI.name, (0, hook_1.wrapHook)(reducers_1.processSSRI)); | ||
} | ||
this.hooks.processHeaders.tap(reducers_1.setDefaultMatch.name, (0, hook_1.wrapHook)(reducers_1.setDefaultMatch)); | ||
this.hooks.processHeaders.tap(reducers_1.interpolateValues.name, (0, hook_1.wrapHook)(reducers_1.interpolateValues)); | ||
if (proxyScript) { | ||
this.hooks.processProxyHeaders.tap(proxy_script_1.processProxyScript.name, (0, hook_1.wrapHook)(proxy_script_1.processProxyScript)); | ||
} | ||
} | ||
headersFactory(props) { | ||
return headers_1.HeadersImpl.fromJSON(props); | ||
return headers_1.Headers.fromJSON(props); | ||
} | ||
@@ -83,3 +108,3 @@ loadDefault({ context, inputFileSystem, }) { | ||
} | ||
prepare(compiler) { | ||
init(compiler) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
@@ -89,6 +114,3 @@ const { context, inputFileSystem } = compiler; | ||
const headersProps = yield this.loadDefault(compiler); | ||
if (typeof headers === 'string') { | ||
Object.assign(headersProps, yield (0, fs_1.readJSON)(headers, inputFileSystem)); | ||
} | ||
else if (typeof headers === 'object') { | ||
if (typeof headers === 'object') { | ||
Object.assign(headersProps, headers); | ||
@@ -100,3 +122,3 @@ } | ||
if (typeof ssri === 'object' && typeof ssri.lock === 'string') { | ||
lockfile = ssri.lock; | ||
lockfile = node_path_1.default.join(root !== null && root !== void 0 ? root : context, ssri.lock); | ||
} | ||
@@ -113,5 +135,19 @@ else if (ssri === true || ssri.lock === true) { | ||
} | ||
return { headers: this.headersFactory(headersProps), ssriLock, lockfile }; | ||
return { headers: headersProps, ssriLock, lockfile, buildNo: 0 }; | ||
}); | ||
} | ||
prepare(compilation, data) { | ||
var _a; | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const { inputFileSystem, compiler: { context }, } = compilation; | ||
const { headers, root } = this.options; | ||
if (typeof headers === 'string') { | ||
const headersFile = (_a = data.headersFile) !== null && _a !== void 0 ? _a : node_path_1.default.join(root !== null && root !== void 0 ? root : context, headers); | ||
Object.assign(data.headers, yield (0, fs_1.readJSON)(headersFile, inputFileSystem)); | ||
compilation.fileDependencies.add(headersFile); | ||
data.headersFile = headersFile; | ||
} | ||
data.buildNo++; | ||
}); | ||
} | ||
emit(compilation, data) { | ||
@@ -161,2 +197,5 @@ return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
metajsFile, | ||
filename, | ||
basename, | ||
query, | ||
}); | ||
@@ -169,18 +208,57 @@ } | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const { headers: headersOption } = this.options; | ||
let headers = data.headers; | ||
if (typeof headersOption === 'function') { | ||
headers = headers.update(yield headersOption(fileInfo)); | ||
} | ||
const { headers: processedHeaders, ssriLock } = yield this.hooks.processHeaders.promise({ | ||
headers, | ||
const { prefix, pretty, suffix, whitelist, strict, proxyScript, metajs } = this.options; | ||
const { headers: headersProps, ssriLock } = yield this.hooks.processHeaders.promise({ | ||
headers: data.headers, | ||
ssriLock: data.ssriLock, | ||
fileInfo, | ||
compilation, | ||
buildNo: data.buildNo, | ||
options: this.options, | ||
}); | ||
headers = processedHeaders; | ||
data.ssriLock = ssriLock; | ||
const headers = this.headersFactory(headersProps); | ||
if (strict) { | ||
headers.validate({ whitelist: whitelist !== null && whitelist !== void 0 ? whitelist : true }); | ||
} | ||
let proxyScriptFile; | ||
let proxyHeaders; | ||
if (proxyScript) { | ||
const { headers: proxyHeadersProps } = yield this.hooks.processProxyHeaders.promise({ | ||
headers: headersProps, | ||
ssriLock, | ||
fileInfo, | ||
compilation, | ||
buildNo: data.buildNo, | ||
options: this.options, | ||
}); | ||
proxyHeaders = this.headersFactory(proxyHeadersProps); | ||
if (strict) { | ||
proxyHeaders.validate({ whitelist: whitelist !== null && whitelist !== void 0 ? whitelist : true }); | ||
} | ||
if (proxyScript === true || proxyScript.filename === undefined) { | ||
proxyScriptFile = '[basename].proxy.user.js'; | ||
} | ||
else { | ||
proxyScriptFile = (0, util_1.interpolate)(proxyScript.filename, { | ||
chunkName: fileInfo.chunk.name, | ||
file: fileInfo.originalFile, | ||
filename: fileInfo.filename, | ||
basename: fileInfo.basename, | ||
query: fileInfo.query, | ||
buildNo: data.buildNo.toString(), | ||
buildTime: Date.now().toString(), | ||
}); | ||
} | ||
} | ||
const { originalFile, chunk, metajsFile, userjsFile } = fileInfo; | ||
const headersStr = headers.render(); | ||
const headersStr = headers.render({ | ||
prefix, | ||
pretty, | ||
suffix, | ||
}); | ||
const proxyHeadersStr = proxyHeaders === null || proxyHeaders === void 0 ? void 0 : proxyHeaders.render({ | ||
prefix, | ||
pretty, | ||
suffix, | ||
}); | ||
const sourceAsset = compilation.getAsset(originalFile); | ||
@@ -191,9 +269,14 @@ if (!sourceAsset) { | ||
compilation.emitAsset(userjsFile, new ConcatSource(headersStr, '\n', sourceAsset.source), { | ||
related: { metajs: metajsFile }, | ||
minimized: true, | ||
}); | ||
compilation.emitAsset(metajsFile, new RawSource(headersStr), { | ||
related: { userjs: userjsFile }, | ||
minimized: true, | ||
}); | ||
if (metajs !== false) { | ||
compilation.emitAsset(metajsFile, new RawSource(proxyHeadersStr !== null && proxyHeadersStr !== void 0 ? proxyHeadersStr : headersStr), { | ||
minimized: true, | ||
}); | ||
} | ||
if (proxyHeadersStr !== undefined && proxyScriptFile !== undefined) { | ||
compilation.emitAsset(proxyScriptFile, new RawSource(proxyHeadersStr), { | ||
minimized: true, | ||
}); | ||
} | ||
chunk.files.add(userjsFile); | ||
@@ -205,3 +288,2 @@ chunk.auxiliaryFiles.add(metajsFile); | ||
exports.UserscriptPlugin = UserscriptPlugin; | ||
UserscriptPlugin.DEFAULT_OPTIONS = {}; | ||
//# sourceMappingURL=plugin.js.map |
@@ -5,3 +5,3 @@ /// <reference types="node" /> | ||
import { Chunk, Compilation } from 'webpack'; | ||
import { Headers, HeadersProps } from './headers'; | ||
import { HeadersProps } from './headers'; | ||
export interface UserscriptOptions { | ||
@@ -11,2 +11,5 @@ root?: string; | ||
headers?: HeadersOption; | ||
pretty?: boolean; | ||
prefix?: string; | ||
suffix?: string; | ||
strict?: boolean; | ||
@@ -17,4 +20,5 @@ whitelist?: boolean; | ||
ssri?: true | SSRIOptions; | ||
proxyScript?: true | ProxyScriptOptions; | ||
} | ||
export type HeadersProvider = (fileInfo: FileInfo) => HeadersProps | Promise<HeadersProps>; | ||
export type HeadersProvider = HeadersReducer | AsyncHeadersReducer; | ||
export type HeadersFile = string; | ||
@@ -33,2 +37,6 @@ export type HeadersOption = HeadersProps | HeadersProvider | HeadersFile | undefined; | ||
} | ||
export interface ProxyScriptOptions { | ||
filename?: string; | ||
baseUrl?: string; | ||
} | ||
export interface FileInfo { | ||
@@ -39,2 +47,5 @@ chunk: Chunk; | ||
metajsFile: string; | ||
filename: string; | ||
basename: string; | ||
query: string; | ||
} | ||
@@ -44,9 +55,10 @@ export type SSRILock = Record<string, string>; | ||
export interface HeadersWaterfall { | ||
headers: Headers; | ||
headers: HeadersProps; | ||
fileInfo: FileInfo; | ||
compilation: Compilation; | ||
buildNo: number; | ||
options: UserscriptOptions; | ||
ssriLock?: SSRILock; | ||
} | ||
export type ProcessHeadersHook = (data: HeadersWaterfall) => Headers; | ||
export type ProcessHeadersAsyncHook = (data: HeadersWaterfall) => Promise<Headers>; | ||
export type HeadersReducer = (data: HeadersWaterfall) => HeadersProps; | ||
export type AsyncHeadersReducer = (data: HeadersWaterfall) => Promise<HeadersProps>; |
{ | ||
"name": "webpack-userscript", | ||
"version": "3.0.0-alpha.1", | ||
"version": "3.0.0-alpha.2", | ||
"description": "A Webpack plugin for userscript projects.", | ||
@@ -79,3 +79,6 @@ "repository": { | ||
"tapable": "^2.2.1" | ||
}, | ||
"peerDependencies": { | ||
"webpack": "5" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
88897
26.84%45
87.5%1146
26.21%0
-100%9
12.5%