@atlaspack/core 2.12.1-canary.3354
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/index.d.ts +22 -0
- package/lib/AssetGraph.js +518 -0
- package/lib/Atlaspack.js +587 -0
- package/lib/AtlaspackConfig.js +298 -0
- package/lib/AtlaspackConfig.schema.js +125 -0
- package/lib/BundleGraph.js +1445 -0
- package/lib/CommittedAsset.js +133 -0
- package/lib/Dependency.js +88 -0
- package/lib/Environment.js +128 -0
- package/lib/InternalConfig.js +48 -0
- package/lib/PackagerRunner.js +519 -0
- package/lib/ReporterRunner.js +151 -0
- package/lib/RequestTracker.js +1089 -0
- package/lib/SymbolPropagation.js +625 -0
- package/lib/TargetDescriptor.schema.js +115 -0
- package/lib/Transformation.js +536 -0
- package/lib/UncommittedAsset.js +342 -0
- package/lib/Validation.js +215 -0
- package/lib/applyRuntimes.js +279 -0
- package/lib/assetUtils.js +189 -0
- package/lib/atlaspack-v3/AtlaspackV3.js +74 -0
- package/lib/atlaspack-v3/fs.js +30 -0
- package/lib/atlaspack-v3/index.js +19 -0
- package/lib/atlaspack-v3/jsCallable.js +15 -0
- package/lib/atlaspack-v3/plugins/Resolver.js +12 -0
- package/lib/atlaspack-v3/plugins/index.js +16 -0
- package/lib/atlaspack-v3/worker/index.js +3 -0
- package/lib/atlaspack-v3/worker/worker.js +30 -0
- package/lib/buildCache.js +18 -0
- package/lib/constants.js +21 -0
- package/lib/dumpGraphToGraphViz.js +206 -0
- package/lib/index.js +106 -0
- package/lib/loadAtlaspackPlugin.js +148 -0
- package/lib/loadDotEnv.js +54 -0
- package/lib/projectPath.js +86 -0
- package/lib/public/Asset.js +259 -0
- package/lib/public/Bundle.js +231 -0
- package/lib/public/BundleGraph.js +193 -0
- package/lib/public/BundleGroup.js +44 -0
- package/lib/public/Config.js +202 -0
- package/lib/public/Dependency.js +131 -0
- package/lib/public/Environment.js +244 -0
- package/lib/public/MutableBundleGraph.js +184 -0
- package/lib/public/PluginOptions.js +71 -0
- package/lib/public/Symbols.js +247 -0
- package/lib/public/Target.js +64 -0
- package/lib/registerCoreWithSerializer.js +45 -0
- package/lib/requests/AssetGraphRequest.js +427 -0
- package/lib/requests/AssetGraphRequestRust.js +187 -0
- package/lib/requests/AssetRequest.js +137 -0
- package/lib/requests/AtlaspackBuildRequest.js +78 -0
- package/lib/requests/AtlaspackConfigRequest.js +473 -0
- package/lib/requests/BundleGraphRequest.js +419 -0
- package/lib/requests/ConfigRequest.js +198 -0
- package/lib/requests/DevDepRequest.js +166 -0
- package/lib/requests/EntryRequest.js +295 -0
- package/lib/requests/PackageRequest.js +88 -0
- package/lib/requests/PathRequest.js +349 -0
- package/lib/requests/TargetRequest.js +1177 -0
- package/lib/requests/ValidationRequest.js +66 -0
- package/lib/requests/WriteBundleRequest.js +252 -0
- package/lib/requests/WriteBundlesRequest.js +153 -0
- package/lib/resolveOptions.js +234 -0
- package/lib/serializer.js +216 -0
- package/lib/serializerCore.browser.js +29 -0
- package/lib/serializerCore.js +16 -0
- package/lib/summarizeRequest.js +55 -0
- package/lib/types.js +35 -0
- package/lib/utils.js +160 -0
- package/lib/worker.js +170 -0
- package/package.json +59 -0
- package/src/AssetGraph.js +646 -0
- package/src/Atlaspack.js +632 -0
- package/src/AtlaspackConfig.js +490 -0
- package/src/AtlaspackConfig.schema.js +141 -0
- package/src/BundleGraph.js +2193 -0
- package/src/CommittedAsset.js +143 -0
- package/src/Dependency.js +142 -0
- package/src/Environment.js +151 -0
- package/src/InternalConfig.js +72 -0
- package/src/PackagerRunner.js +790 -0
- package/src/ReporterRunner.js +158 -0
- package/src/RequestTracker.js +1697 -0
- package/src/SymbolPropagation.js +794 -0
- package/src/TargetDescriptor.schema.js +136 -0
- package/src/Transformation.js +752 -0
- package/src/UncommittedAsset.js +426 -0
- package/src/Validation.js +223 -0
- package/src/applyRuntimes.js +341 -0
- package/src/assetUtils.js +254 -0
- package/src/atlaspack-v3/AtlaspackV3.js +73 -0
- package/src/atlaspack-v3/fs.js +36 -0
- package/src/atlaspack-v3/index.js +5 -0
- package/src/atlaspack-v3/jsCallable.js +13 -0
- package/src/atlaspack-v3/plugins/Resolver.js +9 -0
- package/src/atlaspack-v3/plugins/index.js +3 -0
- package/src/atlaspack-v3/worker/index.js +8 -0
- package/src/atlaspack-v3/worker/worker.js +14 -0
- package/src/buildCache.js +15 -0
- package/src/constants.js +22 -0
- package/src/dumpGraphToGraphViz.js +244 -0
- package/src/index.js +22 -0
- package/src/loadAtlaspackPlugin.js +239 -0
- package/src/loadDotEnv.js +56 -0
- package/src/projectPath.js +87 -0
- package/src/public/Asset.js +359 -0
- package/src/public/Bundle.js +337 -0
- package/src/public/BundleGraph.js +335 -0
- package/src/public/BundleGroup.js +55 -0
- package/src/public/Config.js +261 -0
- package/src/public/Dependency.js +176 -0
- package/src/public/Environment.js +316 -0
- package/src/public/MutableBundleGraph.js +320 -0
- package/src/public/PluginOptions.js +99 -0
- package/src/public/Symbols.js +315 -0
- package/src/public/Target.js +71 -0
- package/src/registerCoreWithSerializer.js +34 -0
- package/src/requests/AssetGraphRequest.js +577 -0
- package/src/requests/AssetGraphRequestRust.js +222 -0
- package/src/requests/AssetRequest.js +179 -0
- package/src/requests/AtlaspackBuildRequest.js +109 -0
- package/src/requests/AtlaspackConfigRequest.js +709 -0
- package/src/requests/BundleGraphRequest.js +547 -0
- package/src/requests/ConfigRequest.js +287 -0
- package/src/requests/DevDepRequest.js +246 -0
- package/src/requests/EntryRequest.js +385 -0
- package/src/requests/PackageRequest.js +105 -0
- package/src/requests/PathRequest.js +454 -0
- package/src/requests/TargetRequest.js +1632 -0
- package/src/requests/ValidationRequest.js +79 -0
- package/src/requests/WriteBundleRequest.js +362 -0
- package/src/requests/WriteBundlesRequest.js +209 -0
- package/src/resolveOptions.js +278 -0
- package/src/serializer.js +255 -0
- package/src/serializerCore.browser.js +8 -0
- package/src/serializerCore.js +5 -0
- package/src/summarizeRequest.js +47 -0
- package/src/types.js +581 -0
- package/src/utils.js +211 -0
- package/src/worker.js +189 -0
- package/test/AssetGraph.test.js +679 -0
- package/test/Atlaspack.test.js +142 -0
- package/test/AtlaspackConfig.test.js +405 -0
- package/test/AtlaspackConfigRequest.test.js +993 -0
- package/test/BundleGraph.test.js +121 -0
- package/test/EntryRequest.test.js +215 -0
- package/test/Environment.test.js +99 -0
- package/test/InternalAsset.test.js +76 -0
- package/test/PackagerRunner.test.js +27 -0
- package/test/PublicAsset.test.js +40 -0
- package/test/PublicBundle.test.js +68 -0
- package/test/PublicDependency.test.js +22 -0
- package/test/PublicEnvironment.test.js +27 -0
- package/test/PublicMutableBundleGraph.test.js +192 -0
- package/test/RequestTracker.test.js +448 -0
- package/test/SymbolPropagation.test.js +716 -0
- package/test/TargetRequest.test.js +1715 -0
- package/test/fixtures/application-targets/package.json +3 -0
- package/test/fixtures/atlaspack/index.js +1 -0
- package/test/fixtures/atlaspack/other.js +3 -0
- package/test/fixtures/atlaspack/package.json +1 -0
- package/test/fixtures/atlaspack/yarn.lock +0 -0
- package/test/fixtures/bundle.js +10 -0
- package/test/fixtures/common-targets/package.json +24 -0
- package/test/fixtures/common-targets-ignore/package.json +13 -0
- package/test/fixtures/config/.atlaspackrc +6 -0
- package/test/fixtures/config/subfolder/.atlaspackrc +6 -0
- package/test/fixtures/config-extends-not-found/.atlaspackrc +3 -0
- package/test/fixtures/config-extends-not-found/.atlaspackrc-json5 +3 -0
- package/test/fixtures/config-extends-not-found/.atlaspackrc-multiple +3 -0
- package/test/fixtures/config-extends-not-found/.atlaspackrc-node-modules +3 -0
- package/test/fixtures/config-malformed/.atlaspackrc +3 -0
- package/test/fixtures/config-node-pipeline/.atlaspackrc +6 -0
- package/test/fixtures/config-plugin-not-found/.atlaspackrc +6 -0
- package/test/fixtures/context/package.json +8 -0
- package/test/fixtures/custom-format-infer-ext/package.json +6 -0
- package/test/fixtures/custom-format-infer-type/package.json +7 -0
- package/test/fixtures/custom-format-mismatch/package.json +8 -0
- package/test/fixtures/custom-targets/package.json +20 -0
- package/test/fixtures/custom-targets-distdir/package.json +11 -0
- package/test/fixtures/duplicate-targets/package.json +4 -0
- package/test/fixtures/glob-like/[entry].js +0 -0
- package/test/fixtures/invalid-distpath/package.json +11 -0
- package/test/fixtures/invalid-engines/package.json +12 -0
- package/test/fixtures/invalid-source-missing/package.json +5 -0
- package/test/fixtures/invalid-source-not-file/package.json +5 -0
- package/test/fixtures/invalid-source-not-file/src/index.js +1 -0
- package/test/fixtures/invalid-target-source-missing/package.json +9 -0
- package/test/fixtures/invalid-target-source-not-file/package.json +9 -0
- package/test/fixtures/invalid-target-source-not-file/src/index.js +1 -0
- package/test/fixtures/invalid-targets/package.json +19 -0
- package/test/fixtures/library-custom-scopehoist/package.json +9 -0
- package/test/fixtures/library-scopehoist/package.json +8 -0
- package/test/fixtures/local-plugin-config-pkg/.atlaspackrc +3 -0
- package/test/fixtures/local-plugin-config-pkg/node_modules/atlaspack-config-local/index.json +8 -0
- package/test/fixtures/local-plugin-config-pkg/node_modules/atlaspack-config-local/local-plugin.js +7 -0
- package/test/fixtures/local-plugin-config-pkg/node_modules/atlaspack-config-local/package.json +7 -0
- package/test/fixtures/main-format-mismatch/package.json +8 -0
- package/test/fixtures/main-global/package.json +8 -0
- package/test/fixtures/main-mjs/package.json +8 -0
- package/test/fixtures/module-a.js +1 -0
- package/test/fixtures/module-b.js +1 -0
- package/test/fixtures/plugins/local-plugin.js +7 -0
- package/test/fixtures/plugins/node_modules/atlaspack-transformer-bad-engines/index.js +7 -0
- package/test/fixtures/plugins/node_modules/atlaspack-transformer-bad-engines/package.json +7 -0
- package/test/fixtures/plugins/node_modules/atlaspack-transformer-no-engines/index.js +7 -0
- package/test/fixtures/plugins/node_modules/atlaspack-transformer-no-engines/package.json +4 -0
- package/test/fixtures/targets-default-distdir-none/package.json +5 -0
- package/test/fixtures/targets-default-distdir-one/package.json +8 -0
- package/test/fixtures/targets-default-distdir-two/package.json +18 -0
- package/test/requests/ConfigRequest.test.js +254 -0
- package/test/serializer.test.js +302 -0
- package/test/test-utils.js +80 -0
- package/test/utils.test.js +43 -0
|
@@ -0,0 +1,790 @@
|
|
|
1
|
+
// @flow strict-local
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
Blob,
|
|
5
|
+
FilePath,
|
|
6
|
+
BundleResult,
|
|
7
|
+
Bundle as BundleType,
|
|
8
|
+
BundleGraph as BundleGraphType,
|
|
9
|
+
NamedBundle as NamedBundleType,
|
|
10
|
+
Async,
|
|
11
|
+
} from '@atlaspack/types';
|
|
12
|
+
import type SourceMap from '@parcel/source-map';
|
|
13
|
+
import type {
|
|
14
|
+
Bundle as InternalBundle,
|
|
15
|
+
Config,
|
|
16
|
+
DevDepRequest,
|
|
17
|
+
AtlaspackOptions,
|
|
18
|
+
ReportFn,
|
|
19
|
+
RequestInvalidation,
|
|
20
|
+
} from './types';
|
|
21
|
+
import type AtlaspackConfig, {LoadedPlugin} from './AtlaspackConfig';
|
|
22
|
+
import type InternalBundleGraph from './BundleGraph';
|
|
23
|
+
import type {ConfigRequest} from './requests/ConfigRequest';
|
|
24
|
+
import type {DevDepSpecifier} from './requests/DevDepRequest';
|
|
25
|
+
|
|
26
|
+
import invariant from 'assert';
|
|
27
|
+
import {blobToStream, TapStream} from '@atlaspack/utils';
|
|
28
|
+
import {PluginLogger} from '@atlaspack/logger';
|
|
29
|
+
import ThrowableDiagnostic, {errorToDiagnostic} from '@atlaspack/diagnostic';
|
|
30
|
+
import {Readable} from 'stream';
|
|
31
|
+
import nullthrows from 'nullthrows';
|
|
32
|
+
import path from 'path';
|
|
33
|
+
import url from 'url';
|
|
34
|
+
import {hashString, hashBuffer, Hash} from '@atlaspack/rust';
|
|
35
|
+
|
|
36
|
+
import {NamedBundle, bundleToInternalBundle} from './public/Bundle';
|
|
37
|
+
import BundleGraph, {
|
|
38
|
+
bundleGraphToInternalBundleGraph,
|
|
39
|
+
} from './public/BundleGraph';
|
|
40
|
+
import PluginOptions from './public/PluginOptions';
|
|
41
|
+
import PublicConfig from './public/Config';
|
|
42
|
+
import {ATLASPACK_VERSION, HASH_REF_PREFIX, HASH_REF_REGEX} from './constants';
|
|
43
|
+
import {
|
|
44
|
+
fromProjectPath,
|
|
45
|
+
toProjectPathUnsafe,
|
|
46
|
+
fromProjectPathRelative,
|
|
47
|
+
joinProjectPath,
|
|
48
|
+
} from './projectPath';
|
|
49
|
+
import {createConfig} from './InternalConfig';
|
|
50
|
+
import {
|
|
51
|
+
loadPluginConfig,
|
|
52
|
+
getConfigHash,
|
|
53
|
+
getConfigRequests,
|
|
54
|
+
type PluginWithBundleConfig,
|
|
55
|
+
} from './requests/ConfigRequest';
|
|
56
|
+
import {
|
|
57
|
+
createDevDependency,
|
|
58
|
+
getWorkerDevDepRequests,
|
|
59
|
+
} from './requests/DevDepRequest';
|
|
60
|
+
import {createBuildCache} from './buildCache';
|
|
61
|
+
import {getInvalidationId, getInvalidationHash} from './assetUtils';
|
|
62
|
+
import {optionsProxy} from './utils';
|
|
63
|
+
import {invalidateDevDeps} from './requests/DevDepRequest';
|
|
64
|
+
import {tracer, PluginTracer} from '@atlaspack/profiler';
|
|
65
|
+
|
|
66
|
+
type Opts = {|
|
|
67
|
+
config: AtlaspackConfig,
|
|
68
|
+
options: AtlaspackOptions,
|
|
69
|
+
report: ReportFn,
|
|
70
|
+
previousDevDeps: Map<string, string>,
|
|
71
|
+
previousInvalidations: Array<RequestInvalidation>,
|
|
72
|
+
|};
|
|
73
|
+
|
|
74
|
+
export type RunPackagerRunnerResult = {|
|
|
75
|
+
bundleInfo: BundleInfo,
|
|
76
|
+
configRequests: Array<ConfigRequest>,
|
|
77
|
+
devDepRequests: Array<DevDepRequest>,
|
|
78
|
+
invalidations: Array<RequestInvalidation>,
|
|
79
|
+
|};
|
|
80
|
+
|
|
81
|
+
export type BundleInfo = {|
|
|
82
|
+
+type: string,
|
|
83
|
+
+size: number,
|
|
84
|
+
+hash: string,
|
|
85
|
+
+hashReferences: Array<string>,
|
|
86
|
+
+time?: number,
|
|
87
|
+
+cacheKeys: CacheKeyMap,
|
|
88
|
+
+isLargeBlob: boolean,
|
|
89
|
+
|};
|
|
90
|
+
|
|
91
|
+
type CacheKeyMap = {|
|
|
92
|
+
content: string,
|
|
93
|
+
map: string,
|
|
94
|
+
info: string,
|
|
95
|
+
|};
|
|
96
|
+
|
|
97
|
+
const BOUNDARY_LENGTH = HASH_REF_PREFIX.length + 32 - 1;
|
|
98
|
+
|
|
99
|
+
// Packager/optimizer configs are not bundle-specific, so we only need to
|
|
100
|
+
// load them once per build.
|
|
101
|
+
const pluginConfigs = createBuildCache();
|
|
102
|
+
|
|
103
|
+
export default class PackagerRunner {
|
|
104
|
+
config: AtlaspackConfig;
|
|
105
|
+
options: AtlaspackOptions;
|
|
106
|
+
pluginOptions: PluginOptions;
|
|
107
|
+
distDir: FilePath;
|
|
108
|
+
distExists: Set<FilePath>;
|
|
109
|
+
report: ReportFn;
|
|
110
|
+
previousDevDeps: Map<string, string>;
|
|
111
|
+
devDepRequests: Map<string, DevDepRequest>;
|
|
112
|
+
invalidations: Map<string, RequestInvalidation>;
|
|
113
|
+
previousInvalidations: Array<RequestInvalidation>;
|
|
114
|
+
|
|
115
|
+
constructor({
|
|
116
|
+
config,
|
|
117
|
+
options,
|
|
118
|
+
report,
|
|
119
|
+
previousDevDeps,
|
|
120
|
+
previousInvalidations,
|
|
121
|
+
}: Opts) {
|
|
122
|
+
this.config = config;
|
|
123
|
+
this.options = options;
|
|
124
|
+
this.report = report;
|
|
125
|
+
this.previousDevDeps = previousDevDeps;
|
|
126
|
+
this.devDepRequests = new Map();
|
|
127
|
+
this.previousInvalidations = previousInvalidations;
|
|
128
|
+
this.invalidations = new Map();
|
|
129
|
+
this.pluginOptions = new PluginOptions(
|
|
130
|
+
optionsProxy(this.options, option => {
|
|
131
|
+
let invalidation: RequestInvalidation = {
|
|
132
|
+
type: 'option',
|
|
133
|
+
key: option,
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
this.invalidations.set(getInvalidationId(invalidation), invalidation);
|
|
137
|
+
}),
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async run(
|
|
142
|
+
bundleGraph: InternalBundleGraph,
|
|
143
|
+
bundle: InternalBundle,
|
|
144
|
+
invalidDevDeps: Array<DevDepSpecifier>,
|
|
145
|
+
): Promise<RunPackagerRunnerResult> {
|
|
146
|
+
invalidateDevDeps(invalidDevDeps, this.options, this.config);
|
|
147
|
+
|
|
148
|
+
let {configs, bundleConfigs} = await this.loadConfigs(bundleGraph, bundle);
|
|
149
|
+
let bundleInfo =
|
|
150
|
+
(await this.getBundleInfoFromCache(
|
|
151
|
+
bundleGraph,
|
|
152
|
+
bundle,
|
|
153
|
+
configs,
|
|
154
|
+
bundleConfigs,
|
|
155
|
+
)) ??
|
|
156
|
+
(await this.getBundleInfo(bundle, bundleGraph, configs, bundleConfigs));
|
|
157
|
+
|
|
158
|
+
let configRequests = getConfigRequests([
|
|
159
|
+
...configs.values(),
|
|
160
|
+
...bundleConfigs.values(),
|
|
161
|
+
]);
|
|
162
|
+
let devDepRequests = getWorkerDevDepRequests([
|
|
163
|
+
...this.devDepRequests.values(),
|
|
164
|
+
]);
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
bundleInfo,
|
|
168
|
+
configRequests,
|
|
169
|
+
devDepRequests,
|
|
170
|
+
invalidations: [...this.invalidations.values()],
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async loadConfigs(
|
|
175
|
+
bundleGraph: InternalBundleGraph,
|
|
176
|
+
bundle: InternalBundle,
|
|
177
|
+
): Promise<{|
|
|
178
|
+
configs: Map<string, Config>,
|
|
179
|
+
bundleConfigs: Map<string, Config>,
|
|
180
|
+
|}> {
|
|
181
|
+
let configs = new Map();
|
|
182
|
+
let bundleConfigs = new Map();
|
|
183
|
+
|
|
184
|
+
await this.loadConfig(bundleGraph, bundle, configs, bundleConfigs);
|
|
185
|
+
for (let inlineBundle of bundleGraph.getInlineBundles(bundle)) {
|
|
186
|
+
await this.loadConfig(bundleGraph, inlineBundle, configs, bundleConfigs);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return {configs, bundleConfigs};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async loadConfig(
|
|
193
|
+
bundleGraph: InternalBundleGraph,
|
|
194
|
+
bundle: InternalBundle,
|
|
195
|
+
configs: Map<string, Config>,
|
|
196
|
+
bundleConfigs: Map<string, Config>,
|
|
197
|
+
): Promise<void> {
|
|
198
|
+
let name = nullthrows(bundle.name);
|
|
199
|
+
let plugin = await this.config.getPackager(name);
|
|
200
|
+
await this.loadPluginConfig(
|
|
201
|
+
bundleGraph,
|
|
202
|
+
bundle,
|
|
203
|
+
plugin,
|
|
204
|
+
configs,
|
|
205
|
+
bundleConfigs,
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
let optimizers = await this.config.getOptimizers(name, bundle.pipeline);
|
|
209
|
+
for (let optimizer of optimizers) {
|
|
210
|
+
await this.loadPluginConfig(
|
|
211
|
+
bundleGraph,
|
|
212
|
+
bundle,
|
|
213
|
+
optimizer,
|
|
214
|
+
configs,
|
|
215
|
+
bundleConfigs,
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async loadPluginConfig<T: PluginWithBundleConfig>(
|
|
221
|
+
bundleGraph: InternalBundleGraph,
|
|
222
|
+
bundle: InternalBundle,
|
|
223
|
+
plugin: LoadedPlugin<T>,
|
|
224
|
+
configs: Map<string, Config>,
|
|
225
|
+
bundleConfigs: Map<string, Config>,
|
|
226
|
+
): Promise<void> {
|
|
227
|
+
if (!configs.has(plugin.name)) {
|
|
228
|
+
// Only load config for a plugin once per build.
|
|
229
|
+
let existing = pluginConfigs.get(plugin.name);
|
|
230
|
+
if (existing != null) {
|
|
231
|
+
configs.set(plugin.name, existing);
|
|
232
|
+
} else {
|
|
233
|
+
if (plugin.plugin.loadConfig != null) {
|
|
234
|
+
let config = createConfig({
|
|
235
|
+
plugin: plugin.name,
|
|
236
|
+
searchPath: toProjectPathUnsafe('index'),
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
await loadPluginConfig(plugin, config, this.options);
|
|
240
|
+
|
|
241
|
+
for (let devDep of config.devDeps) {
|
|
242
|
+
let devDepRequest = await createDevDependency(
|
|
243
|
+
devDep,
|
|
244
|
+
this.previousDevDeps,
|
|
245
|
+
this.options,
|
|
246
|
+
);
|
|
247
|
+
let key = `${devDep.specifier}:${fromProjectPath(
|
|
248
|
+
this.options.projectRoot,
|
|
249
|
+
devDep.resolveFrom,
|
|
250
|
+
)}`;
|
|
251
|
+
this.devDepRequests.set(key, devDepRequest);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
pluginConfigs.set(plugin.name, config);
|
|
255
|
+
configs.set(plugin.name, config);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
let loadBundleConfig = plugin.plugin.loadBundleConfig;
|
|
261
|
+
if (!bundleConfigs.has(plugin.name) && loadBundleConfig != null) {
|
|
262
|
+
let config = createConfig({
|
|
263
|
+
plugin: plugin.name,
|
|
264
|
+
searchPath: joinProjectPath(
|
|
265
|
+
bundle.target.distDir,
|
|
266
|
+
bundle.name ?? bundle.id,
|
|
267
|
+
),
|
|
268
|
+
});
|
|
269
|
+
config.result = await loadBundleConfig({
|
|
270
|
+
bundle: NamedBundle.get(bundle, bundleGraph, this.options),
|
|
271
|
+
bundleGraph: new BundleGraph<NamedBundleType>(
|
|
272
|
+
bundleGraph,
|
|
273
|
+
NamedBundle.get.bind(NamedBundle),
|
|
274
|
+
this.options,
|
|
275
|
+
),
|
|
276
|
+
config: new PublicConfig(config, this.options),
|
|
277
|
+
options: new PluginOptions(this.options),
|
|
278
|
+
logger: new PluginLogger({origin: plugin.name}),
|
|
279
|
+
tracer: new PluginTracer({origin: plugin.name, category: 'loadConfig'}),
|
|
280
|
+
});
|
|
281
|
+
bundleConfigs.set(plugin.name, config);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
async getBundleInfoFromCache(
|
|
286
|
+
bundleGraph: InternalBundleGraph,
|
|
287
|
+
bundle: InternalBundle,
|
|
288
|
+
configs: Map<string, Config>,
|
|
289
|
+
bundleConfigs: Map<string, Config>,
|
|
290
|
+
): Async<?BundleInfo> {
|
|
291
|
+
if (this.options.shouldDisableCache) {
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
let cacheKey = await this.getCacheKey(
|
|
296
|
+
bundle,
|
|
297
|
+
bundleGraph,
|
|
298
|
+
configs,
|
|
299
|
+
bundleConfigs,
|
|
300
|
+
this.previousInvalidations,
|
|
301
|
+
);
|
|
302
|
+
let infoKey = PackagerRunner.getInfoKey(cacheKey);
|
|
303
|
+
return this.options.cache.get<BundleInfo>(infoKey);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
async getBundleInfo(
|
|
307
|
+
bundle: InternalBundle,
|
|
308
|
+
bundleGraph: InternalBundleGraph,
|
|
309
|
+
configs: Map<string, Config>,
|
|
310
|
+
bundleConfigs: Map<string, Config>,
|
|
311
|
+
): Promise<BundleInfo> {
|
|
312
|
+
let {type, contents, map} = await this.getBundleResult(
|
|
313
|
+
bundle,
|
|
314
|
+
bundleGraph,
|
|
315
|
+
configs,
|
|
316
|
+
bundleConfigs,
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
// Recompute cache keys as they may have changed due to dev dependencies.
|
|
320
|
+
let cacheKey = await this.getCacheKey(
|
|
321
|
+
bundle,
|
|
322
|
+
bundleGraph,
|
|
323
|
+
configs,
|
|
324
|
+
bundleConfigs,
|
|
325
|
+
[...this.invalidations.values()],
|
|
326
|
+
);
|
|
327
|
+
let cacheKeys = {
|
|
328
|
+
content: PackagerRunner.getContentKey(cacheKey),
|
|
329
|
+
map: PackagerRunner.getMapKey(cacheKey),
|
|
330
|
+
info: PackagerRunner.getInfoKey(cacheKey),
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
return this.writeToCache(cacheKeys, type, contents, map);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
async getBundleResult(
|
|
337
|
+
bundle: InternalBundle,
|
|
338
|
+
bundleGraph: InternalBundleGraph,
|
|
339
|
+
configs: Map<string, Config>,
|
|
340
|
+
bundleConfigs: Map<string, Config>,
|
|
341
|
+
): Promise<{|
|
|
342
|
+
type: string,
|
|
343
|
+
contents: Blob,
|
|
344
|
+
map: ?string,
|
|
345
|
+
|}> {
|
|
346
|
+
let packaged = await this.package(
|
|
347
|
+
bundle,
|
|
348
|
+
bundleGraph,
|
|
349
|
+
configs,
|
|
350
|
+
bundleConfigs,
|
|
351
|
+
);
|
|
352
|
+
let type = packaged.type ?? bundle.type;
|
|
353
|
+
let res = await this.optimize(
|
|
354
|
+
bundle,
|
|
355
|
+
bundleGraph,
|
|
356
|
+
type,
|
|
357
|
+
packaged.contents,
|
|
358
|
+
packaged.map,
|
|
359
|
+
configs,
|
|
360
|
+
bundleConfigs,
|
|
361
|
+
);
|
|
362
|
+
|
|
363
|
+
let map =
|
|
364
|
+
res.map != null ? await this.generateSourceMap(bundle, res.map) : null;
|
|
365
|
+
return {
|
|
366
|
+
type: res.type ?? type,
|
|
367
|
+
contents: res.contents,
|
|
368
|
+
map,
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
getSourceMapReference(bundle: NamedBundle, map: ?SourceMap): Async<?string> {
|
|
373
|
+
if (map && bundle.env.sourceMap && bundle.bundleBehavior !== 'inline') {
|
|
374
|
+
if (bundle.env.sourceMap && bundle.env.sourceMap.inline) {
|
|
375
|
+
return this.generateSourceMap(bundleToInternalBundle(bundle), map);
|
|
376
|
+
} else {
|
|
377
|
+
return path.basename(bundle.name) + '.map';
|
|
378
|
+
}
|
|
379
|
+
} else {
|
|
380
|
+
return null;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
async package(
|
|
385
|
+
internalBundle: InternalBundle,
|
|
386
|
+
bundleGraph: InternalBundleGraph,
|
|
387
|
+
configs: Map<string, Config>,
|
|
388
|
+
bundleConfigs: Map<string, Config>,
|
|
389
|
+
): Promise<BundleResult> {
|
|
390
|
+
let bundle = NamedBundle.get(internalBundle, bundleGraph, this.options);
|
|
391
|
+
this.report({
|
|
392
|
+
type: 'buildProgress',
|
|
393
|
+
phase: 'packaging',
|
|
394
|
+
bundle,
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
let packager = await this.config.getPackager(bundle.name);
|
|
398
|
+
let {name, resolveFrom, plugin} = packager;
|
|
399
|
+
let measurement;
|
|
400
|
+
try {
|
|
401
|
+
measurement = tracer.createMeasurement(name, 'packaging', bundle.name, {
|
|
402
|
+
type: bundle.type,
|
|
403
|
+
});
|
|
404
|
+
return await plugin.package({
|
|
405
|
+
config: configs.get(name)?.result,
|
|
406
|
+
bundleConfig: bundleConfigs.get(name)?.result,
|
|
407
|
+
bundle,
|
|
408
|
+
bundleGraph: new BundleGraph<NamedBundleType>(
|
|
409
|
+
bundleGraph,
|
|
410
|
+
NamedBundle.get.bind(NamedBundle),
|
|
411
|
+
this.options,
|
|
412
|
+
),
|
|
413
|
+
getSourceMapReference: map => {
|
|
414
|
+
return this.getSourceMapReference(bundle, map);
|
|
415
|
+
},
|
|
416
|
+
options: this.pluginOptions,
|
|
417
|
+
logger: new PluginLogger({origin: name}),
|
|
418
|
+
tracer: new PluginTracer({origin: name, category: 'package'}),
|
|
419
|
+
getInlineBundleContents: async (
|
|
420
|
+
bundle: BundleType,
|
|
421
|
+
bundleGraph: BundleGraphType<NamedBundleType>,
|
|
422
|
+
) => {
|
|
423
|
+
if (bundle.bundleBehavior !== 'inline') {
|
|
424
|
+
throw new Error(
|
|
425
|
+
'Bundle is not inline and unable to retrieve contents',
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
let res = await this.getBundleResult(
|
|
430
|
+
bundleToInternalBundle(bundle),
|
|
431
|
+
// $FlowFixMe
|
|
432
|
+
bundleGraphToInternalBundleGraph(bundleGraph),
|
|
433
|
+
configs,
|
|
434
|
+
bundleConfigs,
|
|
435
|
+
);
|
|
436
|
+
|
|
437
|
+
return {contents: res.contents};
|
|
438
|
+
},
|
|
439
|
+
});
|
|
440
|
+
} catch (e) {
|
|
441
|
+
throw new ThrowableDiagnostic({
|
|
442
|
+
diagnostic: errorToDiagnostic(e, {
|
|
443
|
+
origin: name,
|
|
444
|
+
filePath: path.join(bundle.target.distDir, bundle.name),
|
|
445
|
+
}),
|
|
446
|
+
});
|
|
447
|
+
} finally {
|
|
448
|
+
measurement && measurement.end();
|
|
449
|
+
// Add dev dependency for the packager. This must be done AFTER running it due to
|
|
450
|
+
// the potential for lazy require() that aren't executed until the request runs.
|
|
451
|
+
let devDepRequest = await createDevDependency(
|
|
452
|
+
{
|
|
453
|
+
specifier: name,
|
|
454
|
+
resolveFrom,
|
|
455
|
+
},
|
|
456
|
+
this.previousDevDeps,
|
|
457
|
+
this.options,
|
|
458
|
+
);
|
|
459
|
+
this.devDepRequests.set(
|
|
460
|
+
`${name}:${fromProjectPathRelative(resolveFrom)}`,
|
|
461
|
+
devDepRequest,
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
async optimize(
|
|
467
|
+
internalBundle: InternalBundle,
|
|
468
|
+
internalBundleGraph: InternalBundleGraph,
|
|
469
|
+
type: string,
|
|
470
|
+
contents: Blob,
|
|
471
|
+
map?: ?SourceMap,
|
|
472
|
+
configs: Map<string, Config>,
|
|
473
|
+
bundleConfigs: Map<string, Config>,
|
|
474
|
+
): Promise<BundleResult> {
|
|
475
|
+
let bundle = NamedBundle.get(
|
|
476
|
+
internalBundle,
|
|
477
|
+
internalBundleGraph,
|
|
478
|
+
this.options,
|
|
479
|
+
);
|
|
480
|
+
let bundleGraph = new BundleGraph<NamedBundleType>(
|
|
481
|
+
internalBundleGraph,
|
|
482
|
+
NamedBundle.get.bind(NamedBundle),
|
|
483
|
+
this.options,
|
|
484
|
+
);
|
|
485
|
+
let optimizers = await this.config.getOptimizers(
|
|
486
|
+
bundle.name,
|
|
487
|
+
internalBundle.pipeline,
|
|
488
|
+
);
|
|
489
|
+
if (!optimizers.length) {
|
|
490
|
+
return {type: bundle.type, contents, map};
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
this.report({
|
|
494
|
+
type: 'buildProgress',
|
|
495
|
+
phase: 'optimizing',
|
|
496
|
+
bundle,
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
let optimized = {
|
|
500
|
+
type,
|
|
501
|
+
contents,
|
|
502
|
+
map,
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
for (let optimizer of optimizers) {
|
|
506
|
+
let measurement;
|
|
507
|
+
try {
|
|
508
|
+
measurement = tracer.createMeasurement(
|
|
509
|
+
optimizer.name,
|
|
510
|
+
'optimize',
|
|
511
|
+
bundle.name,
|
|
512
|
+
);
|
|
513
|
+
let next = await optimizer.plugin.optimize({
|
|
514
|
+
config: configs.get(optimizer.name)?.result,
|
|
515
|
+
bundleConfig: bundleConfigs.get(optimizer.name)?.result,
|
|
516
|
+
bundle,
|
|
517
|
+
bundleGraph,
|
|
518
|
+
contents: optimized.contents,
|
|
519
|
+
map: optimized.map,
|
|
520
|
+
getSourceMapReference: map => {
|
|
521
|
+
return this.getSourceMapReference(bundle, map);
|
|
522
|
+
},
|
|
523
|
+
options: this.pluginOptions,
|
|
524
|
+
logger: new PluginLogger({origin: optimizer.name}),
|
|
525
|
+
tracer: new PluginTracer({
|
|
526
|
+
origin: optimizer.name,
|
|
527
|
+
category: 'optimize',
|
|
528
|
+
}),
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
optimized.type = next.type ?? optimized.type;
|
|
532
|
+
optimized.contents = next.contents;
|
|
533
|
+
optimized.map = next.map;
|
|
534
|
+
} catch (e) {
|
|
535
|
+
throw new ThrowableDiagnostic({
|
|
536
|
+
diagnostic: errorToDiagnostic(e, {
|
|
537
|
+
origin: optimizer.name,
|
|
538
|
+
filePath: path.join(bundle.target.distDir, bundle.name),
|
|
539
|
+
}),
|
|
540
|
+
});
|
|
541
|
+
} finally {
|
|
542
|
+
measurement && measurement.end();
|
|
543
|
+
// Add dev dependency for the optimizer. This must be done AFTER running it due to
|
|
544
|
+
// the potential for lazy require() that aren't executed until the request runs.
|
|
545
|
+
let devDepRequest = await createDevDependency(
|
|
546
|
+
{
|
|
547
|
+
specifier: optimizer.name,
|
|
548
|
+
resolveFrom: optimizer.resolveFrom,
|
|
549
|
+
},
|
|
550
|
+
this.previousDevDeps,
|
|
551
|
+
this.options,
|
|
552
|
+
);
|
|
553
|
+
this.devDepRequests.set(
|
|
554
|
+
`${optimizer.name}:${fromProjectPathRelative(optimizer.resolveFrom)}`,
|
|
555
|
+
devDepRequest,
|
|
556
|
+
);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
return optimized;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
async generateSourceMap(
|
|
564
|
+
bundle: InternalBundle,
|
|
565
|
+
map: SourceMap,
|
|
566
|
+
): Promise<string> {
|
|
567
|
+
// sourceRoot should be a relative path between outDir and rootDir for node.js targets
|
|
568
|
+
let filePath = joinProjectPath(
|
|
569
|
+
bundle.target.distDir,
|
|
570
|
+
nullthrows(bundle.name),
|
|
571
|
+
);
|
|
572
|
+
let fullPath = fromProjectPath(this.options.projectRoot, filePath);
|
|
573
|
+
let sourceRoot: string = path.relative(
|
|
574
|
+
path.dirname(fullPath),
|
|
575
|
+
this.options.projectRoot,
|
|
576
|
+
);
|
|
577
|
+
let inlineSources = false;
|
|
578
|
+
|
|
579
|
+
if (bundle.target) {
|
|
580
|
+
if (
|
|
581
|
+
bundle.env.sourceMap &&
|
|
582
|
+
bundle.env.sourceMap.sourceRoot !== undefined
|
|
583
|
+
) {
|
|
584
|
+
sourceRoot = bundle.env.sourceMap.sourceRoot;
|
|
585
|
+
} else if (
|
|
586
|
+
this.options.serveOptions &&
|
|
587
|
+
bundle.target.env.context === 'browser'
|
|
588
|
+
) {
|
|
589
|
+
sourceRoot = '/__atlaspack_source_root';
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
if (
|
|
593
|
+
bundle.env.sourceMap &&
|
|
594
|
+
bundle.env.sourceMap.inlineSources !== undefined
|
|
595
|
+
) {
|
|
596
|
+
inlineSources = bundle.env.sourceMap.inlineSources;
|
|
597
|
+
} else if (bundle.target.env.context !== 'node') {
|
|
598
|
+
// inlining should only happen in production for browser targets by default
|
|
599
|
+
inlineSources = this.options.mode === 'production';
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
let mapFilename = fullPath + '.map';
|
|
604
|
+
let isInlineMap = bundle.env.sourceMap && bundle.env.sourceMap.inline;
|
|
605
|
+
|
|
606
|
+
let stringified = await map.stringify({
|
|
607
|
+
file: path.basename(mapFilename),
|
|
608
|
+
// $FlowFixMe
|
|
609
|
+
fs: this.options.inputFS,
|
|
610
|
+
rootDir: this.options.projectRoot,
|
|
611
|
+
sourceRoot: !inlineSources
|
|
612
|
+
? url.format(url.parse(sourceRoot + '/'))
|
|
613
|
+
: undefined,
|
|
614
|
+
inlineSources,
|
|
615
|
+
format: isInlineMap ? 'inline' : 'string',
|
|
616
|
+
});
|
|
617
|
+
|
|
618
|
+
invariant(typeof stringified === 'string');
|
|
619
|
+
return stringified;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
async getCacheKey(
|
|
623
|
+
bundle: InternalBundle,
|
|
624
|
+
bundleGraph: InternalBundleGraph,
|
|
625
|
+
configs: Map<string, Config>,
|
|
626
|
+
bundleConfigs: Map<string, Config>,
|
|
627
|
+
invalidations: Array<RequestInvalidation>,
|
|
628
|
+
): Promise<string> {
|
|
629
|
+
let configResults = {};
|
|
630
|
+
for (let [pluginName, config] of configs) {
|
|
631
|
+
if (config) {
|
|
632
|
+
configResults[pluginName] = await getConfigHash(
|
|
633
|
+
config,
|
|
634
|
+
pluginName,
|
|
635
|
+
this.options,
|
|
636
|
+
);
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
let globalInfoResults = {};
|
|
640
|
+
for (let [pluginName, config] of bundleConfigs) {
|
|
641
|
+
if (config) {
|
|
642
|
+
globalInfoResults[pluginName] = await getConfigHash(
|
|
643
|
+
config,
|
|
644
|
+
pluginName,
|
|
645
|
+
this.options,
|
|
646
|
+
);
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
let devDepHashes = await this.getDevDepHashes(bundle);
|
|
651
|
+
for (let inlineBundle of bundleGraph.getInlineBundles(bundle)) {
|
|
652
|
+
devDepHashes += await this.getDevDepHashes(inlineBundle);
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
let invalidationHash = await getInvalidationHash(
|
|
656
|
+
invalidations,
|
|
657
|
+
this.options,
|
|
658
|
+
);
|
|
659
|
+
|
|
660
|
+
return hashString(
|
|
661
|
+
ATLASPACK_VERSION +
|
|
662
|
+
devDepHashes +
|
|
663
|
+
invalidationHash +
|
|
664
|
+
bundle.target.publicUrl +
|
|
665
|
+
bundleGraph.getHash(bundle) +
|
|
666
|
+
JSON.stringify(configResults) +
|
|
667
|
+
JSON.stringify(globalInfoResults) +
|
|
668
|
+
this.options.mode +
|
|
669
|
+
(this.options.shouldBuildLazily ? 'lazy' : 'eager'),
|
|
670
|
+
);
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
async getDevDepHashes(bundle: InternalBundle): Promise<string> {
|
|
674
|
+
let name = nullthrows(bundle.name);
|
|
675
|
+
let packager = await this.config.getPackager(name);
|
|
676
|
+
let optimizers = await this.config.getOptimizers(name);
|
|
677
|
+
|
|
678
|
+
let key = `${packager.name}:${fromProjectPathRelative(
|
|
679
|
+
packager.resolveFrom,
|
|
680
|
+
)}`;
|
|
681
|
+
let devDepHashes =
|
|
682
|
+
this.devDepRequests.get(key)?.hash ?? this.previousDevDeps.get(key) ?? '';
|
|
683
|
+
for (let {name, resolveFrom} of optimizers) {
|
|
684
|
+
let key = `${name}:${fromProjectPathRelative(resolveFrom)}`;
|
|
685
|
+
devDepHashes +=
|
|
686
|
+
this.devDepRequests.get(key)?.hash ??
|
|
687
|
+
this.previousDevDeps.get(key) ??
|
|
688
|
+
'';
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
return devDepHashes;
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
async readFromCache(cacheKey: string): Promise<?{|
|
|
695
|
+
contents: Readable,
|
|
696
|
+
map: ?Readable,
|
|
697
|
+
|}> {
|
|
698
|
+
let contentKey = PackagerRunner.getContentKey(cacheKey);
|
|
699
|
+
let mapKey = PackagerRunner.getMapKey(cacheKey);
|
|
700
|
+
|
|
701
|
+
let isLargeBlob = await this.options.cache.hasLargeBlob(contentKey);
|
|
702
|
+
let contentExists =
|
|
703
|
+
isLargeBlob || (await this.options.cache.has(contentKey));
|
|
704
|
+
if (!contentExists) {
|
|
705
|
+
return null;
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
let mapExists = await this.options.cache.has(mapKey);
|
|
709
|
+
|
|
710
|
+
return {
|
|
711
|
+
contents: isLargeBlob
|
|
712
|
+
? this.options.cache.getStream(contentKey)
|
|
713
|
+
: blobToStream(await this.options.cache.getBlob(contentKey)),
|
|
714
|
+
map: mapExists
|
|
715
|
+
? blobToStream(await this.options.cache.getBlob(mapKey))
|
|
716
|
+
: null,
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
async writeToCache(
|
|
721
|
+
cacheKeys: CacheKeyMap,
|
|
722
|
+
type: string,
|
|
723
|
+
contents: Blob,
|
|
724
|
+
map: ?string,
|
|
725
|
+
): Promise<BundleInfo> {
|
|
726
|
+
let size = 0;
|
|
727
|
+
let hash;
|
|
728
|
+
let hashReferences = [];
|
|
729
|
+
let isLargeBlob = false;
|
|
730
|
+
|
|
731
|
+
// TODO: don't replace hash references in binary files??
|
|
732
|
+
if (contents instanceof Readable) {
|
|
733
|
+
isLargeBlob = true;
|
|
734
|
+
let boundaryStr = '';
|
|
735
|
+
let h = new Hash();
|
|
736
|
+
await this.options.cache.setStream(
|
|
737
|
+
cacheKeys.content,
|
|
738
|
+
blobToStream(contents).pipe(
|
|
739
|
+
new TapStream(buf => {
|
|
740
|
+
let str = boundaryStr + buf.toString();
|
|
741
|
+
hashReferences = hashReferences.concat(
|
|
742
|
+
str.match(HASH_REF_REGEX) ?? [],
|
|
743
|
+
);
|
|
744
|
+
size += buf.length;
|
|
745
|
+
h.writeBuffer(buf);
|
|
746
|
+
boundaryStr = str.slice(str.length - BOUNDARY_LENGTH);
|
|
747
|
+
}),
|
|
748
|
+
),
|
|
749
|
+
);
|
|
750
|
+
hash = h.finish();
|
|
751
|
+
} else if (typeof contents === 'string') {
|
|
752
|
+
let buffer = Buffer.from(contents);
|
|
753
|
+
size = buffer.byteLength;
|
|
754
|
+
hash = hashBuffer(buffer);
|
|
755
|
+
hashReferences = contents.match(HASH_REF_REGEX) ?? [];
|
|
756
|
+
await this.options.cache.setBlob(cacheKeys.content, buffer);
|
|
757
|
+
} else {
|
|
758
|
+
size = contents.length;
|
|
759
|
+
hash = hashBuffer(contents);
|
|
760
|
+
hashReferences = contents.toString().match(HASH_REF_REGEX) ?? [];
|
|
761
|
+
await this.options.cache.setBlob(cacheKeys.content, contents);
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
if (map != null) {
|
|
765
|
+
await this.options.cache.setBlob(cacheKeys.map, map);
|
|
766
|
+
}
|
|
767
|
+
let info = {
|
|
768
|
+
type,
|
|
769
|
+
size,
|
|
770
|
+
hash,
|
|
771
|
+
hashReferences,
|
|
772
|
+
cacheKeys,
|
|
773
|
+
isLargeBlob,
|
|
774
|
+
};
|
|
775
|
+
await this.options.cache.set(cacheKeys.info, info);
|
|
776
|
+
return info;
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
static getContentKey(cacheKey: string): string {
|
|
780
|
+
return hashString(`${cacheKey}:content`);
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
static getMapKey(cacheKey: string): string {
|
|
784
|
+
return hashString(`${cacheKey}:map`);
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
static getInfoKey(cacheKey: string): string {
|
|
788
|
+
return hashString(`${cacheKey}:info`);
|
|
789
|
+
}
|
|
790
|
+
}
|