From c5ad6df793c8423d4e83c736db712a8095209b48 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Fri, 18 Jan 2019 21:51:03 +0100 Subject: [PATCH] add own webpack-sources typings --- declarations.d.ts | 95 ++++++++++++++++++++++++++ lib/NormalModule.js | 2 +- lib/util/registerExternalSerializer.js | 8 +-- package.json | 1 - yarn.lock | 14 ---- 5 files changed, 97 insertions(+), 23 deletions(-) diff --git a/declarations.d.ts b/declarations.d.ts index 60844ed08..6a62225c9 100644 --- a/declarations.d.ts +++ b/declarations.d.ts @@ -220,6 +220,101 @@ declare module "@webassemblyjs/ast" { export function isFuncImportDescr(n: Node): boolean; } +declare module "webpack-sources" { + type MapOptions = { columns?: boolean; module?: boolean }; + + export abstract class Source { + size(): number; + + map(options?: MapOptions): Object; + + sourceAndMap( + options?: MapOptions + ): { + source: string | Buffer; + map: Object; + }; + + updateHash(hash: import("./lib/util/createHash").Hash): void; + + source(): string | Buffer; + } + + export class RawSource extends Source { + constructor(source: string | Buffer); + + // TODO remove internals + _value: string | Buffer; + } + + export class OriginalSource extends Source { + constructor(source: string | Buffer, name: string); + + // TODO remove internals + _value: string | Buffer; + _name: string; + } + + export class ReplaceSource extends Source { + constructor(source: Source, name?: string); + + replace(start: number, end: number, newValue: string, name?: string): void; + insert(pos: number, newValue: string, name?: string): void; + + // TODO remove internals + _name: string; + _source: string; + _replacements: { + start: number; + end: number; + content: string; + insertIndex: number; + name: string; + }[]; + } + + export class SourceMapSource extends Source { + constructor( + source: string, + name: string, + sourceMap: Object | string, + originalSource?: string, + innerSourceMap?: Object + ); + } + + export class ConcatSource extends Source { + constructor(...args: (string | Source)[]); + + children: (string | Source)[]; + + add(item: string | Source): void; + } + + export class PrefixSource extends Source { + constructor(prefix: string, source: string | Source); + + _source: string | Source; + _prefix: string; + } + + export class CachedSource extends Source { + constructor(source: Source); + + _source: Source; + _cachedSource: string | Buffer; + _cachedBuffer: Buffer; + _cachedSize: number; + _cachedMaps: Object; + } + + export class SizeOnlySource extends Source { + constructor(size: number); + + _size: number; + } +} + // This "hack" is needed because typescript doesn't support recursive type definitions // It's referenced from "ruleSet-conditions" in schemas/WebpackOptions.json interface RuleSetConditionsRecursive diff --git a/lib/NormalModule.js b/lib/NormalModule.js index 4563170b3..4d69c8cc8 100644 --- a/lib/NormalModule.js +++ b/lib/NormalModule.js @@ -574,7 +574,7 @@ class NormalModule extends Module { } const handleParseError = e => { - const source = this._source.source(); + const source = /** @type {string} */ (this._source.source()); const error = new ModuleParseError(source, e); this.markModuleAsErrored(error); this._initBuildHash(compilation); diff --git a/lib/util/registerExternalSerializer.js b/lib/util/registerExternalSerializer.js index 6cc213eaa..703cd00ff 100644 --- a/lib/util/registerExternalSerializer.js +++ b/lib/util/registerExternalSerializer.js @@ -38,7 +38,6 @@ register( */ serialize(source, { write }) { write(source._source); - // @ts-ignore write(source._cachedBuffer); write(source._cachedSource); write(source._cachedSize); @@ -51,7 +50,6 @@ register( */ deserialize({ read }) { const source = new CachedSource(read()); - // @ts-ignore source._cachedBuffer = read(); source._cachedSource = read(); source._cachedSize = read(); @@ -152,10 +150,7 @@ register( serialize(source, { write }) { write(source._source); write(source._name); - const replacements = /** @type {TODO} */ (Array.from( - // @ts-ignore - source._replacements - )); + const replacements = Array.from(source._replacements); replacements.sort((a, b) => { return a.insertIndex - b.insertIndex; }); @@ -176,7 +171,6 @@ register( const source = new ReplaceSource(read(), read()); const len = read(); for (let i = 0; i < len; i++) { - // @ts-ignore TODO signature is missing one argument in typings source.replace(read(), read(), read(), read()); } return source; diff --git a/package.json b/package.json index 9e8e745ae..131d6465e 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,6 @@ "devDependencies": { "@types/node": "^10.12.3", "@types/tapable": "^1.0.1", - "@types/webpack-sources": "^0.1.4", "benchmark": "^2.1.1", "bundle-loader": "~0.5.0", "codacy-coverage": "^3.1.0", diff --git a/yarn.lock b/yarn.lock index a38502362..d842ce96e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -84,25 +84,11 @@ resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-1.13.2.tgz#ffe96278e712a8d4e467e367a338b05e22872646" integrity sha512-k6MCN8WuDiCj6O+UJsVMbrreZxkbrhQbO02oDj6yuRu8UAkp0MDdEcDKif8/gBKuJbT84kkO+VHQAqXkumEklg== -"@types/source-list-map@*": - version "0.1.2" - resolved "https://registry.yarnpkg.com/@types/source-list-map/-/source-list-map-0.1.2.tgz#0078836063ffaf17412349bba364087e0ac02ec9" - integrity sha512-K5K+yml8LTo9bWJI/rECfIPrGgxdpeNbj+d53lwN4QjW1MCwlkhUms+gtdzigTeUyBr09+u8BwOIY3MXvHdcsA== - "@types/tapable@^1.0.1": version "1.0.4" resolved "https://registry.yarnpkg.com/@types/tapable/-/tapable-1.0.4.tgz#b4ffc7dc97b498c969b360a41eee247f82616370" integrity sha512-78AdXtlhpCHT0K3EytMpn4JNxaf5tbqbLcbIRoQIHzpTIyjpxLQKRoxU55ujBXAtg3Nl2h/XWvfDa9dsMOd0pQ== -"@types/webpack-sources@^0.1.4": - version "0.1.5" - resolved "https://registry.yarnpkg.com/@types/webpack-sources/-/webpack-sources-0.1.5.tgz#be47c10f783d3d6efe1471ff7f042611bd464a92" - integrity sha512-zfvjpp7jiafSmrzJ2/i3LqOyTYTuJ7u1KOXlKgDlvsj9Rr0x7ZiYu5lZbXwobL7lmsRNtPXlBfmaUD8eU2Hu8w== - dependencies: - "@types/node" "*" - "@types/source-list-map" "*" - source-map "^0.6.1" - "@webassemblyjs/ast@1.7.11": version "1.7.11" resolved "https://registry.yarnpkg.com/@webassemblyjs/ast/-/ast-1.7.11.tgz#b988582cafbb2b095e8b556526f30c90d057cace"