UNPKG

@velcro/bundler

Version:

Build a module graph of modules and serialize these to different formats

308 lines 10.2 kB
import { Uri, CancellationToken, Event, Thenable } from "@velcro/common"; import { ResolverContext, Resolver } from "@velcro/resolver"; import MagicString from "magic-string"; import { SourceMapSegment, Bundle } from "magic-string"; declare const version = "__VERSION__"; declare enum SourceModuleDependencyKind { Entrypoint = "Entrypoint", Require = "Require", RequireResolve = "RequireResolve", GlobalObject = "GlobalObject" } interface SourceModuleOptions { exportName?: string; } type SourceLocation = { start: number; end: number; }; declare class SourceModuleDependency { readonly kind: SourceModuleDependencyKind; readonly spec: string; readonly locations: ReadonlyArray<SourceLocation>; readonly options: SourceModuleOptions; locator?: { name: string; spec: string; path: string; version?: string; }; constructor(kind: SourceModuleDependencyKind, spec: string, locations: ReadonlyArray<SourceLocation>, options?: SourceModuleOptions); static areIdentical(l: SourceModuleDependency, r: SourceModuleDependency): boolean; static fromEntrypoint(uri: Uri): SourceModuleDependency; static fromGlobalObject(spec: string, locations: SourceLocation[], exportName?: string): SourceModuleDependency; static fromRequire(spec: string, locations: SourceLocation[]): SourceModuleDependency; static fromRequireResolve(spec: string, locations: SourceLocation[]): SourceModuleDependency; } interface DependencyEdge { dependency: SourceModuleDependency; fromUri: Uri; fromRootUri: Uri; toUri: Uri; toRootUri: Uri; visited: ResolverContext.Visit[]; } interface ISourceMapper { traceMappings(): ReturnType<typeof traceMappings>; } /** * Copyright (c) Rollup 2020 authors: https://github.com/rollup/rollup/graphs/contributors) * * Copied with light modifications from: * https://github.com/rollup/rollup/blob/36a4527473ea1fe678ed866c9f8dfd3c2542cd22/src/utils/collapseSourcemaps.ts */ declare class Source { content: string | null; filename: string; constructor(filename: string, content: string | null); traceSegment(line: number, column: number, name?: string): SourceMapSegmentObject; } interface SourceMapSegmentObject { column: number; line: number; name?: string; source: Source; } declare class Link { mappings: SourceMapSegment[][]; names: string[]; sources: (Source | Link)[]; constructor(map: { mappings: SourceMapSegment[][] | string; names: string[]; }, sources: (Source | Link)[]); traceMappings(): Error | { sources: string[]; sourcesContent: (string | null)[]; names: string[]; mappings: SourceMapSegment[][]; }; traceSegment(line: number, column: number, name: string): SourceMapSegmentObject | null; } declare function traceMappings(this: void, map: { mappings: SourceMapSegment[][]; names: string[]; sources: (Link | Source)[]; }): Error | { sources: string[]; sourcesContent: (string | null)[]; names: string[]; mappings: SourceMapSegment[][]; }; declare class SourceModule { readonly uri: Uri; readonly rootUri: Uri; readonly source: MagicString; readonly dependencies: Set<SourceModuleDependency>; readonly sourceMapsTree: Source | Link; readonly visits: ResolverContext.Visit[]; constructor(uri: Uri, rootUri: Uri, source: MagicString, dependencies: Set<SourceModuleDependency>, sourceMapsTree: Source | Link, visits: ResolverContext.Visit[]); get href(): string; get rootHref(): string; } declare class SourceMap { readonly file?: string; readonly mappings: string; readonly sourceRoot?: string; readonly names: string[]; readonly sources: (string | null)[]; readonly sourcesContent?: (string | null)[]; readonly version: number; constructor(input: { file?: string; mappings: string; sourceRoot?: string; names: string[]; sources: (string | null)[]; sourcesContent?: (string | null)[]; version: string | number; }); toString(): string; toDataUri(): string; } declare class ChunkOutput { private readonly bundle; private readonly sourceMapTree; readonly entrypoints: ReadonlyArray<Uri>; private cachedCode?; private cachedSourceMap?; private cachedSourceMapDataUri?; private cachedSourceMapString?; constructor(bundle: Bundle, sourceMapTree: ISourceMapper, entrypoints: ReadonlyArray<Uri>); get code(): string; get sourceMap(): SourceMap; get sourceMapString(): string; get sourceMapDataUri(): string; private generateSourceMap; } declare class Chunk { private readonly edgesFrom; private readonly edgesTo; private readonly entrypoints; private readonly sourceModules; constructor(options: Chunk.Options); buildForStaticRuntime(options?: Chunk.ToStringOptions): ChunkOutput; } declare namespace Chunk { interface Options { edges: Iterable<DependencyEdge>; entrypoints: Array<Uri>; sourceModules: Iterable<SourceModule>; } interface ToStringOptions { /** * Toggle whether to inject the runtime in the generated code. * * An instance of the runtime is important as it is what will actually schedule * and execute code built for Velcro. * * When `injectRuntime` is `true`, the runtime code will be injected and the * instance of it will be exposed as `Velcro.runtime`. */ injectRuntime?: boolean; invalidations?: string[]; } } declare abstract class BaseError extends Error { readonly name: string; } declare class GraphBuildError extends BaseError { readonly errors: Error[]; constructor(errors: Error[]); } declare class Graph { private readonly edgesFrom; private readonly edgesTo; //@ts-ignore private readonly rootUri; private readonly sourceModules; constructor(options: Graph.Options); splitChunks(): Iterable<Chunk>; } declare namespace Graph { interface Options { edges: Iterable<DependencyEdge>; rootUri: Uri; sourceModules: Iterable<SourceModule>; } } type MaybeThenable<T> = T | Thenable<T>; interface Plugin { name: string; load?(ctx: PluginLoadContext, id: string): MaybeThenable<PluginLoadResult | undefined>; resolveDependency?(ctx: PluginResolveDependencyContext, dependency: SourceModuleDependency, fromModule: SourceModule): MaybeThenable<PluginResolveDependencyResult | undefined>; resolveEntrypoint?(ctx: PluginResolveEntrypointContext, uri: Uri): MaybeThenable<PluginResolveEntrypointResult | undefined>; transform?(ctx: PluginTransformContext, id: Uri, code: string): MaybeThenable<PluginTransformResult | undefined>; } interface PluginLoadContext { nodeEnv: string; resolver: Resolver; token: CancellationToken; } type PluginLoadResult = { code: string; visited?: ResolverContext.Visit[]; }; interface PluginResolveDependencyContext { nodeEnv: string; resolver: Resolver; token: CancellationToken; } type PluginResolveDependencyResult = { uri: Uri; rootUri: Uri; visited?: ResolverContext.Visit[]; }; interface PluginResolveEntrypointContext { nodeEnv: string; resolver: Resolver; token: CancellationToken; } type PluginResolveEntrypointResult = { uri: Uri; rootUri: Uri; visited?: ResolverContext.Visit[]; }; interface PluginTransformContext { nodeEnv: string; resolver: Resolver; token: CancellationToken; createMagicString(): MagicString; } type PluginTransformResult = { code: string; sourceMap?: { mappings: SourceMapSegment[][] | string; names: string[]; }; visited?: ResolverContext.Visit[]; }; type ExternalTestFunction = (dependency: SourceModuleDependency, fromSourceModule: SourceModule) => boolean; declare class Build { readonly rootUri: Uri; private readonly disposer; private readonly edges; readonly errors: Error[]; readonly seen: Set<unknown>; private readonly sourceModules; private readonly pendingModuleOperations; private readonly tokenSource; private readonly onCompletedEmitter; private readonly onErrorEmitter; private readonly onProgressEmitter; readonly done: Promise<Graph>; constructor(rootUri: Uri, options?: { token?: CancellationToken; }); get onCompleted(): Event<{ graph: Graph; }>; get onError(): Event<{ error: Error; }>; get onProgress(): Event<{ progress: { completed: number; pending: number; }; }>; get token(): CancellationToken; addEdge(edge: DependencyEdge): void; addSourceModule(sourceModule: SourceModule): void; cancel(): void; dispose(): void; hasSourceModule(href: string): boolean; runAsync(key: string, fn: () => Promise<unknown>): void; } declare class GraphBuilder { private readonly edgesByDependency; private readonly edgesByInvalidation; private readonly external?; private readonly nodeEnv; private readonly resolver; private readonly pluginManager; private readonly sourceModules; private readonly sourceModulesByInvalidation; constructor(options: GraphBuilder.Options); private loadDependency; private loadEdge; private loadEntrypoint; private visitSourceModule; build(entrypoints: (string | Uri)[], options?: { incremental?: boolean; token?: CancellationToken; }): Build; invalidate(uri: Uri | string): void; private createEdge; } declare namespace GraphBuilder { interface Options { external?: ExternalTestFunction; nodeEnv?: string; plugins?: Plugin[]; resolver: Resolver; } } export { version, Chunk, ChunkOutput, GraphBuildError, Graph, Build, GraphBuilder, SourceModule, SourceModuleDependency, Plugin, PluginLoadContext, PluginLoadResult, PluginTransformContext, PluginTransformResult }; export type { VelcroRuntime } from "@velcro/runtime"; //# sourceMappingURL=index.d.ts.map