UNPKG

7.75 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
4 * This code may only be used under the BSD style license found at
5 * http://polymer.github.io/LICENSE.txt
6 * The complete set of authors may be found at
7 * http://polymer.github.io/AUTHORS.txt
8 * The complete set of contributors may be found at
9 * http://polymer.github.io/CONTRIBUTORS.txt
10 * Code distributed by Google as part of the polymer project is also
11 * subject to an additional IP rights grant found at
12 * http://polymer.github.io/PATENTS.txt
13 */
14import { PackageRelativeUrl, ResolvedUrl, UrlResolver } from 'polymer-analyzer';
15/**
16 * A bundle strategy function is used to transform an array of bundles.
17 */
18export declare type BundleStrategy = (bundles: Bundle[]) => Bundle[];
19/**
20 * A bundle URL mapper function produces a map of URLs to bundles.
21 */
22export declare type BundleUrlMapper = (bundles: Bundle[]) => Map<ResolvedUrl, Bundle>;
23/**
24 * A mapping of entrypoints to their full set of transitive dependencies,
25 * such that a dependency graph `a->c, c->d, d->e, b->d, b->f` would be
26 * represented `{a:[a,c,d,e], b:[b,d,e,f]}`. Please note that there is an
27 * explicit identity dependency (`a` depends on `a`, `b` depends on `b`).
28 */
29export declare type TransitiveDependenciesMap = Map<ResolvedUrl, Set<ResolvedUrl>>;
30/**
31 * The output format of the bundle.
32 */
33export declare type BundleType = 'html-fragment' | 'es6-module';
34export declare const bundleTypeExtnames: Map<BundleType, string>;
35/**
36 * A bundle is a grouping of files which serve the need of one or more
37 * entrypoint files.
38 */
39export declare class Bundle {
40 type: BundleType;
41 entrypoints: Set<ResolvedUrl>;
42 files: Set<ResolvedUrl>;
43 stripImports: Set<ResolvedUrl>;
44 missingImports: Set<ResolvedUrl>;
45 inlinedHtmlImports: Set<ResolvedUrl>;
46 inlinedScripts: Set<ResolvedUrl>;
47 inlinedStyles: Set<ResolvedUrl>;
48 bundledExports: Map<ResolvedUrl, Map<string, string>>;
49 constructor(type: BundleType, entrypoints?: Set<ResolvedUrl>, files?: Set<ResolvedUrl>);
50 readonly extname: string | undefined;
51}
52/**
53 * Represents a bundle assigned to an output URL.
54 */
55export declare class AssignedBundle {
56 bundle: Bundle;
57 url: ResolvedUrl;
58}
59export interface BundleManifestJson {
60 [entrypoint: string]: PackageRelativeUrl[];
61}
62/**
63 * A bundle manifest is a mapping of URLs to bundles.
64 */
65export declare class BundleManifest {
66 bundles: Map<ResolvedUrl, Bundle>;
67 private _bundleUrlForFile;
68 /**
69 * Given a collection of bundles and a BundleUrlMapper to generate URLs for
70 * them, the constructor populates the `bundles` and `files` index properties.
71 */
72 constructor(bundles: Bundle[], urlMapper: BundleUrlMapper);
73 fork(): BundleManifest;
74 getBundleForFile(url: ResolvedUrl): AssignedBundle | undefined;
75 toJson(urlResolver: UrlResolver): BundleManifestJson;
76}
77/**
78 * Chains multiple bundle strategy functions together so the output of one
79 * becomes the input of the next and so-on.
80 */
81export declare function composeStrategies(strategies: BundleStrategy[]): BundleStrategy;
82/**
83 * Given an index of files and their dependencies, produce an array of bundles,
84 * where a bundle is defined for each set of dependencies.
85 *
86 * For example, a dependency index representing the graph:
87 * `a->b, b->c, b->d, e->c, e->f`
88 *
89 * Would produce an array of three bundles:
90 * `[a]->[a,b,d], [e]->[e,f], [a,e]->[c]`
91 */
92export declare function generateBundles(depsIndex: TransitiveDependenciesMap): Bundle[];
93/**
94 * Instances of `<script type="module">` generate synthetic entrypoints in the
95 * depsIndex and are treated as entrypoints during the initial phase of
96 * `generateBundles`. Any bundle which provides dependencies to a single
97 * synthetic entrypoint of this type (aka a single entrypoint sub-bundle) are
98 * merged back into the bundle for the HTML containing the script tag.
99 *
100 * For example, the following bundles:
101 * `[a]->[a], [a>1]->[x], [a>1,a>2]->[y], [a>2]->[z]`
102 *
103 * Would be merged into the following set of bundles:
104 * `[a]->[a,x,z], [a>1,a>2]->[y]`
105 *
106 * `a>1` and `a>2` represent script tag entrypoints. Only `x` and `z` are
107 * bundled with `a` because they each serve only a single script tag entrypoint.
108 * `y` has to be in a separate bundle so that it is not inlined into bundle `a`
109 * in both script tags.
110 */
111export declare function mergeSingleEntrypointSubBundles(bundles: Bundle[]): void;
112/**
113 * Creates a bundle URL mapper function which takes a prefix and appends an
114 * incrementing value, starting with `1` to the filename.
115 */
116export declare function generateCountingSharedBundleUrlMapper(urlPrefix: ResolvedUrl): BundleUrlMapper;
117/**
118 * Generates a strategy function which finds all non-entrypoint bundles which
119 * are dependencies of the given entrypoint and merges them into that
120 * entrypoint's bundle.
121 */
122export declare function generateEagerMergeStrategy(entrypoint: ResolvedUrl): BundleStrategy;
123/**
124 * Generates a strategy function which finds all bundles matching the predicate
125 * function and merges them into the bundle containing the target file.
126 */
127export declare function generateMatchMergeStrategy(predicate: (b: Bundle) => boolean): BundleStrategy;
128/**
129 * Creates a bundle URL mapper function which maps non-shared bundles to the
130 * URLs of their single entrypoint and yields responsibility for naming
131 * remaining shared bundle URLs to the `mapper` function argument. The
132 * mapper function takes a collection of shared bundles and a URL map, calling
133 * `.set(url, bundle)` for each.
134 */
135export declare function generateSharedBundleUrlMapper(mapper: (sharedBundles: Bundle[]) => ResolvedUrl[]): BundleUrlMapper;
136/**
137 * Generates a strategy function to merge all bundles where the dependencies
138 * for a bundle are shared by at least 2 entrypoints (default; set
139 * `minEntrypoints` to change threshold).
140 *
141 * This function will convert an array of 4 bundles:
142 * `[a]->[a,b], [a,c]->[d], [c]->[c,e], [f,g]->[f,g,h]`
143 *
144 * Into the following 3 bundles, including a single bundle for all of the
145 * dependencies which are shared by at least 2 entrypoints:
146 * `[a]->[a,b], [c]->[c,e], [a,c,f,g]->[d,f,g,h]`
147 */
148export declare function generateSharedDepsMergeStrategy(maybeMinEntrypoints?: number): BundleStrategy;
149/**
150 * A bundle strategy function which merges all shared dependencies into a
151 * bundle for an application shell.
152 */
153export declare function generateShellMergeStrategy(shell: ResolvedUrl, maybeMinEntrypoints?: number): BundleStrategy;
154/**
155 * Generates a strategy function that ensures bundles do not link to given URLs.
156 * Bundles which contain matching files will still have them inlined.
157 */
158export declare function generateNoBackLinkStrategy(urls: ResolvedUrl[]): BundleStrategy;
159/**
160 * Given an Array of bundles, produce a single bundle with the entrypoints and
161 * files of all bundles represented. By default, bundles of different types
162 * can not be merged, but this constraint can be skipped by providing
163 * `ignoreTypeCheck` argument with value `true`, which is necessary to merge a
164 * bundle containining an inline document's unique transitive dependencies, as
165 * inline documents typically are of different type (`<script type="module">`
166 * within HTML document contains JavaScript document).
167 */
168export declare function mergeBundles(bundles: Bundle[], ignoreTypeCheck?: boolean): Bundle;
169/**
170 * Return a new bundle array where bundles within it matching the predicate
171 * are merged together. Note that merge operations are segregated by type so
172 * that no attempt to merge bundles of different types will occur.
173 */
174export declare function mergeMatchingBundles(bundles: Bundle[], predicate: (bundle: Bundle) => boolean): Bundle[];