@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,752 @@
|
|
|
1
|
+
// @flow strict-local
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
FilePath,
|
|
5
|
+
GenerateOutput,
|
|
6
|
+
Transformer,
|
|
7
|
+
TransformerResult,
|
|
8
|
+
PackageName,
|
|
9
|
+
ResolveOptions,
|
|
10
|
+
SemverRange,
|
|
11
|
+
} from '@atlaspack/types';
|
|
12
|
+
import type {WorkerApi} from '@atlaspack/workers';
|
|
13
|
+
import type {
|
|
14
|
+
Asset as AssetValue,
|
|
15
|
+
TransformationRequest,
|
|
16
|
+
Config,
|
|
17
|
+
DevDepRequest,
|
|
18
|
+
AtlaspackOptions,
|
|
19
|
+
InternalDevDepOptions,
|
|
20
|
+
Invalidations,
|
|
21
|
+
} from './types';
|
|
22
|
+
import type {LoadedPlugin} from './AtlaspackConfig';
|
|
23
|
+
|
|
24
|
+
import path from 'path';
|
|
25
|
+
import {Readable} from 'stream';
|
|
26
|
+
import nullthrows from 'nullthrows';
|
|
27
|
+
import logger, {PluginLogger} from '@atlaspack/logger';
|
|
28
|
+
import ThrowableDiagnostic, {
|
|
29
|
+
anyToDiagnostic,
|
|
30
|
+
errorToDiagnostic,
|
|
31
|
+
escapeMarkdown,
|
|
32
|
+
md,
|
|
33
|
+
type Diagnostic,
|
|
34
|
+
} from '@atlaspack/diagnostic';
|
|
35
|
+
import {SOURCEMAP_EXTENSIONS} from '@atlaspack/utils';
|
|
36
|
+
import {hashString} from '@atlaspack/rust';
|
|
37
|
+
|
|
38
|
+
import {createDependency} from './Dependency';
|
|
39
|
+
import AtlaspackConfig from './AtlaspackConfig';
|
|
40
|
+
// TODO: eventually call path request as sub requests
|
|
41
|
+
import {ResolverRunner} from './requests/PathRequest';
|
|
42
|
+
import {
|
|
43
|
+
Asset,
|
|
44
|
+
MutableAsset,
|
|
45
|
+
mutableAssetToUncommittedAsset,
|
|
46
|
+
} from './public/Asset';
|
|
47
|
+
import UncommittedAsset from './UncommittedAsset';
|
|
48
|
+
import {createAsset} from './assetUtils';
|
|
49
|
+
import summarizeRequest from './summarizeRequest';
|
|
50
|
+
import PluginOptions from './public/PluginOptions';
|
|
51
|
+
import {optionsProxy} from './utils';
|
|
52
|
+
import {createConfig} from './InternalConfig';
|
|
53
|
+
import {
|
|
54
|
+
loadPluginConfig,
|
|
55
|
+
getConfigRequests,
|
|
56
|
+
type ConfigRequest,
|
|
57
|
+
} from './requests/ConfigRequest';
|
|
58
|
+
import {
|
|
59
|
+
createDevDependency,
|
|
60
|
+
invalidateDevDeps,
|
|
61
|
+
getWorkerDevDepRequests,
|
|
62
|
+
} from './requests/DevDepRequest';
|
|
63
|
+
import {
|
|
64
|
+
type ProjectPath,
|
|
65
|
+
fromProjectPath,
|
|
66
|
+
fromProjectPathRelative,
|
|
67
|
+
toProjectPathUnsafe,
|
|
68
|
+
toProjectPath,
|
|
69
|
+
} from './projectPath';
|
|
70
|
+
import {invalidateOnFileCreateToInternal, createInvalidations} from './utils';
|
|
71
|
+
import invariant from 'assert';
|
|
72
|
+
import {tracer, PluginTracer} from '@atlaspack/profiler';
|
|
73
|
+
|
|
74
|
+
type GenerateFunc = (input: UncommittedAsset) => Promise<GenerateOutput>;
|
|
75
|
+
|
|
76
|
+
type PostProcessFunc = (
|
|
77
|
+
Array<UncommittedAsset>,
|
|
78
|
+
) => Promise<Array<UncommittedAsset> | null>;
|
|
79
|
+
|
|
80
|
+
export type TransformationOpts = {|
|
|
81
|
+
options: AtlaspackOptions,
|
|
82
|
+
config: AtlaspackConfig,
|
|
83
|
+
request: TransformationRequest,
|
|
84
|
+
workerApi: WorkerApi,
|
|
85
|
+
|};
|
|
86
|
+
|
|
87
|
+
export type TransformationResult = {|
|
|
88
|
+
assets?: Array<AssetValue>,
|
|
89
|
+
error?: Array<Diagnostic>,
|
|
90
|
+
configRequests: Array<ConfigRequest>,
|
|
91
|
+
invalidations: Invalidations,
|
|
92
|
+
devDepRequests: Array<DevDepRequest>,
|
|
93
|
+
|};
|
|
94
|
+
|
|
95
|
+
export default class Transformation {
|
|
96
|
+
request: TransformationRequest;
|
|
97
|
+
configs: Map<string, Config>;
|
|
98
|
+
devDepRequests: Map<string, DevDepRequest>;
|
|
99
|
+
pluginDevDeps: Array<InternalDevDepOptions>;
|
|
100
|
+
options: AtlaspackOptions;
|
|
101
|
+
pluginOptions: PluginOptions;
|
|
102
|
+
workerApi: WorkerApi;
|
|
103
|
+
atlaspackConfig: AtlaspackConfig;
|
|
104
|
+
invalidations: Invalidations;
|
|
105
|
+
resolverRunner: ResolverRunner;
|
|
106
|
+
|
|
107
|
+
constructor({request, options, config, workerApi}: TransformationOpts) {
|
|
108
|
+
this.configs = new Map();
|
|
109
|
+
this.atlaspackConfig = config;
|
|
110
|
+
this.options = options;
|
|
111
|
+
this.request = request;
|
|
112
|
+
this.workerApi = workerApi;
|
|
113
|
+
this.invalidations = createInvalidations();
|
|
114
|
+
this.devDepRequests = new Map();
|
|
115
|
+
this.pluginDevDeps = [];
|
|
116
|
+
this.resolverRunner = new ResolverRunner({
|
|
117
|
+
config,
|
|
118
|
+
options,
|
|
119
|
+
previousDevDeps: request.devDeps,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
this.pluginOptions = new PluginOptions(
|
|
123
|
+
optionsProxy(
|
|
124
|
+
this.options,
|
|
125
|
+
option => {
|
|
126
|
+
this.invalidations.invalidateOnOptionChange.add(option);
|
|
127
|
+
},
|
|
128
|
+
devDep => {
|
|
129
|
+
this.pluginDevDeps.push(devDep);
|
|
130
|
+
},
|
|
131
|
+
),
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async run(): Promise<TransformationResult> {
|
|
136
|
+
let asset = await this.loadAsset();
|
|
137
|
+
let existing;
|
|
138
|
+
|
|
139
|
+
if (!asset.mapBuffer && SOURCEMAP_EXTENSIONS.has(asset.value.type)) {
|
|
140
|
+
// Load existing sourcemaps, this automatically runs the source contents extraction
|
|
141
|
+
try {
|
|
142
|
+
existing = await asset.loadExistingSourcemap();
|
|
143
|
+
} catch (err) {
|
|
144
|
+
logger.verbose([
|
|
145
|
+
{
|
|
146
|
+
origin: '@atlaspack/core',
|
|
147
|
+
message: md`Could not load existing source map for ${fromProjectPathRelative(
|
|
148
|
+
asset.value.filePath,
|
|
149
|
+
)}`,
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
origin: '@atlaspack/core',
|
|
153
|
+
message: escapeMarkdown(err.message),
|
|
154
|
+
},
|
|
155
|
+
]);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (
|
|
160
|
+
existing == null &&
|
|
161
|
+
// Don't buffer an entire stream into memory since it may not need sourceContent,
|
|
162
|
+
// e.g. large binary files
|
|
163
|
+
!(asset.content instanceof Readable)
|
|
164
|
+
) {
|
|
165
|
+
// If no existing sourcemap was found, initialize asset.sourceContent
|
|
166
|
+
// with the original contents. This will be used when the transformer
|
|
167
|
+
// calls setMap to ensure the source content is in the sourcemap.
|
|
168
|
+
asset.sourceContent = await asset.getCode();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
invalidateDevDeps(
|
|
172
|
+
this.request.invalidDevDeps,
|
|
173
|
+
this.options,
|
|
174
|
+
this.atlaspackConfig,
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
let pipeline = await this.loadPipeline(
|
|
178
|
+
this.request.filePath,
|
|
179
|
+
asset.value.isSource,
|
|
180
|
+
asset.value.pipeline,
|
|
181
|
+
);
|
|
182
|
+
let assets, error;
|
|
183
|
+
try {
|
|
184
|
+
let results = await this.runPipelines(pipeline, asset);
|
|
185
|
+
await Promise.all(results.map(asset => asset.commit()));
|
|
186
|
+
assets = results.map(a => a.value);
|
|
187
|
+
} catch (e) {
|
|
188
|
+
error = e;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
let configRequests = getConfigRequests([
|
|
192
|
+
...this.configs.values(),
|
|
193
|
+
...this.resolverRunner.configs.values(),
|
|
194
|
+
]);
|
|
195
|
+
let devDepRequests = getWorkerDevDepRequests([
|
|
196
|
+
...this.devDepRequests.values(),
|
|
197
|
+
...this.resolverRunner.devDepRequests.values(),
|
|
198
|
+
]);
|
|
199
|
+
|
|
200
|
+
// $FlowFixMe because of $$raw
|
|
201
|
+
return {
|
|
202
|
+
$$raw: true,
|
|
203
|
+
assets,
|
|
204
|
+
configRequests,
|
|
205
|
+
// When throwing an error, this (de)serialization is done automatically by the WorkerFarm
|
|
206
|
+
error: error ? anyToDiagnostic(error) : undefined,
|
|
207
|
+
invalidations: this.invalidations,
|
|
208
|
+
devDepRequests,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async loadAsset(): Promise<UncommittedAsset> {
|
|
213
|
+
let {
|
|
214
|
+
filePath,
|
|
215
|
+
env,
|
|
216
|
+
code,
|
|
217
|
+
pipeline,
|
|
218
|
+
isSource: isSourceOverride,
|
|
219
|
+
sideEffects,
|
|
220
|
+
query,
|
|
221
|
+
} = this.request;
|
|
222
|
+
let {
|
|
223
|
+
content,
|
|
224
|
+
size,
|
|
225
|
+
isSource: summarizedIsSource,
|
|
226
|
+
} = await summarizeRequest(this.options.inputFS, {
|
|
227
|
+
filePath: fromProjectPath(this.options.projectRoot, filePath),
|
|
228
|
+
code,
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
// Prefer `isSource` originating from the AssetRequest.
|
|
232
|
+
let isSource = isSourceOverride ?? summarizedIsSource;
|
|
233
|
+
|
|
234
|
+
// If the transformer request passed code, use a hash in addition
|
|
235
|
+
// to the filename as the base for the id to ensure it is unique.
|
|
236
|
+
let idBase = fromProjectPathRelative(filePath);
|
|
237
|
+
if (code != null) {
|
|
238
|
+
idBase += hashString(code);
|
|
239
|
+
}
|
|
240
|
+
return new UncommittedAsset({
|
|
241
|
+
idBase,
|
|
242
|
+
value: createAsset(this.options.projectRoot, {
|
|
243
|
+
idBase,
|
|
244
|
+
filePath,
|
|
245
|
+
isSource,
|
|
246
|
+
type: path.extname(fromProjectPathRelative(filePath)).slice(1),
|
|
247
|
+
pipeline,
|
|
248
|
+
env,
|
|
249
|
+
query,
|
|
250
|
+
stats: {
|
|
251
|
+
time: 0,
|
|
252
|
+
size,
|
|
253
|
+
},
|
|
254
|
+
sideEffects,
|
|
255
|
+
}),
|
|
256
|
+
options: this.options,
|
|
257
|
+
content,
|
|
258
|
+
invalidations: this.invalidations,
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
async runPipelines(
|
|
263
|
+
pipeline: Pipeline,
|
|
264
|
+
initialAsset: UncommittedAsset,
|
|
265
|
+
): Promise<Array<UncommittedAsset>> {
|
|
266
|
+
let initialType = initialAsset.value.type;
|
|
267
|
+
let assets: Array<UncommittedAsset>;
|
|
268
|
+
try {
|
|
269
|
+
assets = await this.runPipeline(pipeline, initialAsset);
|
|
270
|
+
} finally {
|
|
271
|
+
// Add dev dep requests for each transformer
|
|
272
|
+
for (let transformer of pipeline.transformers) {
|
|
273
|
+
await this.addDevDependency({
|
|
274
|
+
specifier: transformer.name,
|
|
275
|
+
resolveFrom: transformer.resolveFrom,
|
|
276
|
+
range: transformer.range,
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Add dev dep requests for dependencies of transformer plugins
|
|
281
|
+
// (via proxied packageManager.require calls).
|
|
282
|
+
for (let devDep of this.pluginDevDeps) {
|
|
283
|
+
await this.addDevDependency(devDep);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
let finalAssets: Array<UncommittedAsset> = [];
|
|
288
|
+
for (let asset of assets) {
|
|
289
|
+
let nextPipeline;
|
|
290
|
+
if (asset.value.type !== initialType) {
|
|
291
|
+
nextPipeline = await this.loadNextPipeline({
|
|
292
|
+
filePath: initialAsset.value.filePath,
|
|
293
|
+
isSource: asset.value.isSource,
|
|
294
|
+
newType: asset.value.type,
|
|
295
|
+
newPipeline: asset.value.pipeline,
|
|
296
|
+
currentPipeline: pipeline,
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (nextPipeline) {
|
|
301
|
+
let nextPipelineAssets = await this.runPipelines(nextPipeline, asset);
|
|
302
|
+
finalAssets = finalAssets.concat(nextPipelineAssets);
|
|
303
|
+
} else {
|
|
304
|
+
finalAssets.push(asset);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if (!pipeline.postProcess) {
|
|
309
|
+
return finalAssets;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
invariant(pipeline.postProcess != null);
|
|
313
|
+
let processedFinalAssets: Array<UncommittedAsset> =
|
|
314
|
+
(await pipeline.postProcess(finalAssets)) ?? [];
|
|
315
|
+
|
|
316
|
+
return processedFinalAssets;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
async addDevDependency(opts: InternalDevDepOptions): Promise<void> {
|
|
320
|
+
let {specifier, resolveFrom, range} = opts;
|
|
321
|
+
let key = `${specifier}:${fromProjectPathRelative(resolveFrom)}`;
|
|
322
|
+
if (this.devDepRequests.has(key)) {
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// Ensure that the package manager has an entry for this resolution.
|
|
327
|
+
try {
|
|
328
|
+
await this.options.packageManager.resolve(
|
|
329
|
+
specifier,
|
|
330
|
+
fromProjectPath(this.options.projectRoot, opts.resolveFrom),
|
|
331
|
+
{
|
|
332
|
+
range,
|
|
333
|
+
},
|
|
334
|
+
);
|
|
335
|
+
} catch (err) {
|
|
336
|
+
// ignore
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
let devDepRequest = await createDevDependency(
|
|
340
|
+
opts,
|
|
341
|
+
this.request.devDeps,
|
|
342
|
+
this.options,
|
|
343
|
+
);
|
|
344
|
+
this.devDepRequests.set(key, devDepRequest);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
async runPipeline(
|
|
348
|
+
pipeline: Pipeline,
|
|
349
|
+
initialAsset: UncommittedAsset,
|
|
350
|
+
): Promise<Array<UncommittedAsset>> {
|
|
351
|
+
if (pipeline.transformers.length === 0) {
|
|
352
|
+
return [initialAsset];
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
let initialType = initialAsset.value.type;
|
|
356
|
+
let inputAssets = [initialAsset];
|
|
357
|
+
let resultingAssets = [];
|
|
358
|
+
let finalAssets = [];
|
|
359
|
+
for (let transformer of pipeline.transformers) {
|
|
360
|
+
resultingAssets = [];
|
|
361
|
+
for (let asset of inputAssets) {
|
|
362
|
+
if (
|
|
363
|
+
asset.value.type !== initialType &&
|
|
364
|
+
(await this.loadNextPipeline({
|
|
365
|
+
filePath: initialAsset.value.filePath,
|
|
366
|
+
isSource: asset.value.isSource,
|
|
367
|
+
newType: asset.value.type,
|
|
368
|
+
newPipeline: asset.value.pipeline,
|
|
369
|
+
currentPipeline: pipeline,
|
|
370
|
+
}))
|
|
371
|
+
) {
|
|
372
|
+
finalAssets.push(asset);
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
try {
|
|
377
|
+
const measurement = tracer.createMeasurement(
|
|
378
|
+
transformer.name,
|
|
379
|
+
'transform',
|
|
380
|
+
fromProjectPathRelative(initialAsset.value.filePath),
|
|
381
|
+
);
|
|
382
|
+
|
|
383
|
+
let transformerResults = await this.runTransformer(
|
|
384
|
+
pipeline,
|
|
385
|
+
asset,
|
|
386
|
+
transformer.plugin,
|
|
387
|
+
transformer.name,
|
|
388
|
+
transformer.config,
|
|
389
|
+
transformer.configKeyPath,
|
|
390
|
+
this.atlaspackConfig,
|
|
391
|
+
);
|
|
392
|
+
|
|
393
|
+
measurement && measurement.end();
|
|
394
|
+
|
|
395
|
+
for (let result of transformerResults) {
|
|
396
|
+
if (result instanceof UncommittedAsset) {
|
|
397
|
+
resultingAssets.push(result);
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
400
|
+
resultingAssets.push(
|
|
401
|
+
asset.createChildAsset(
|
|
402
|
+
result,
|
|
403
|
+
transformer.name,
|
|
404
|
+
this.atlaspackConfig.filePath,
|
|
405
|
+
transformer.configKeyPath,
|
|
406
|
+
),
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
} catch (e) {
|
|
410
|
+
let diagnostic = errorToDiagnostic(e, {
|
|
411
|
+
origin: transformer.name,
|
|
412
|
+
filePath: fromProjectPath(
|
|
413
|
+
this.options.projectRoot,
|
|
414
|
+
asset.value.filePath,
|
|
415
|
+
),
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
// If this request is a virtual asset that might not exist on the filesystem,
|
|
419
|
+
// add the `code` property to each code frame in the diagnostics that match the
|
|
420
|
+
// request's filepath. This can't be done by the transformer because it might not
|
|
421
|
+
// have access to the original code (e.g. an inline script tag in HTML).
|
|
422
|
+
if (this.request.code != null) {
|
|
423
|
+
for (let d of diagnostic) {
|
|
424
|
+
if (d.codeFrames) {
|
|
425
|
+
for (let codeFrame of d.codeFrames) {
|
|
426
|
+
if (
|
|
427
|
+
codeFrame.code == null &&
|
|
428
|
+
codeFrame.filePath === this.request.filePath
|
|
429
|
+
) {
|
|
430
|
+
codeFrame.code = this.request.code;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
throw new ThrowableDiagnostic({
|
|
438
|
+
diagnostic,
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
inputAssets = resultingAssets;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
// Make assets with ASTs generate unless they are CSS modules. This parallelizes generation
|
|
446
|
+
// and distributes work more evenly across workers than if one worker needed to
|
|
447
|
+
// generate all assets in a large bundle during packaging.
|
|
448
|
+
await Promise.all(
|
|
449
|
+
resultingAssets
|
|
450
|
+
.filter(
|
|
451
|
+
asset =>
|
|
452
|
+
asset.ast != null &&
|
|
453
|
+
!(
|
|
454
|
+
this.options.mode === 'production' &&
|
|
455
|
+
asset.value.type === 'css' &&
|
|
456
|
+
asset.value.symbols
|
|
457
|
+
),
|
|
458
|
+
)
|
|
459
|
+
.map(async asset => {
|
|
460
|
+
if (asset.isASTDirty && asset.generate) {
|
|
461
|
+
let output = await asset.generate();
|
|
462
|
+
asset.content = output.content;
|
|
463
|
+
asset.mapBuffer = output.map?.toBuffer();
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
asset.clearAST();
|
|
467
|
+
}),
|
|
468
|
+
);
|
|
469
|
+
|
|
470
|
+
return finalAssets.concat(resultingAssets);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
async loadPipeline(
|
|
474
|
+
filePath: ProjectPath,
|
|
475
|
+
isSource: boolean,
|
|
476
|
+
pipeline: ?string,
|
|
477
|
+
): Promise<Pipeline> {
|
|
478
|
+
let transformers = await this.atlaspackConfig.getTransformers(
|
|
479
|
+
filePath,
|
|
480
|
+
pipeline,
|
|
481
|
+
this.request.isURL,
|
|
482
|
+
);
|
|
483
|
+
|
|
484
|
+
for (let transformer of transformers) {
|
|
485
|
+
let config = await this.loadTransformerConfig(transformer, isSource);
|
|
486
|
+
if (config) {
|
|
487
|
+
this.configs.set(transformer.name, config);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
return {
|
|
492
|
+
id: transformers.map(t => t.name).join(':'),
|
|
493
|
+
transformers: transformers.map(transformer => ({
|
|
494
|
+
name: transformer.name,
|
|
495
|
+
resolveFrom: transformer.resolveFrom,
|
|
496
|
+
config: this.configs.get(transformer.name)?.result,
|
|
497
|
+
configKeyPath: transformer.keyPath,
|
|
498
|
+
plugin: transformer.plugin,
|
|
499
|
+
})),
|
|
500
|
+
options: this.options,
|
|
501
|
+
pluginOptions: this.pluginOptions,
|
|
502
|
+
workerApi: this.workerApi,
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
async loadNextPipeline({
|
|
507
|
+
filePath,
|
|
508
|
+
isSource,
|
|
509
|
+
newType,
|
|
510
|
+
newPipeline,
|
|
511
|
+
currentPipeline,
|
|
512
|
+
}: {|
|
|
513
|
+
filePath: ProjectPath,
|
|
514
|
+
isSource: boolean,
|
|
515
|
+
newType: string,
|
|
516
|
+
newPipeline: ?string,
|
|
517
|
+
currentPipeline: Pipeline,
|
|
518
|
+
|}): Promise<?Pipeline> {
|
|
519
|
+
let filePathRelative = fromProjectPathRelative(filePath);
|
|
520
|
+
let nextFilePath = toProjectPathUnsafe(
|
|
521
|
+
filePathRelative.slice(0, -path.extname(filePathRelative).length) +
|
|
522
|
+
'.' +
|
|
523
|
+
newType,
|
|
524
|
+
);
|
|
525
|
+
let nextPipeline = await this.loadPipeline(
|
|
526
|
+
nextFilePath,
|
|
527
|
+
isSource,
|
|
528
|
+
newPipeline,
|
|
529
|
+
);
|
|
530
|
+
|
|
531
|
+
if (nextPipeline.id === currentPipeline.id) {
|
|
532
|
+
return null;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
return nextPipeline;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
async loadTransformerConfig(
|
|
539
|
+
transformer: LoadedPlugin<Transformer<mixed>>,
|
|
540
|
+
isSource: boolean,
|
|
541
|
+
): Promise<?Config> {
|
|
542
|
+
let loadConfig = transformer.plugin.loadConfig;
|
|
543
|
+
if (!loadConfig) {
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
let config = createConfig({
|
|
548
|
+
plugin: transformer.name,
|
|
549
|
+
isSource,
|
|
550
|
+
searchPath: this.request.filePath,
|
|
551
|
+
env: this.request.env,
|
|
552
|
+
});
|
|
553
|
+
|
|
554
|
+
await loadPluginConfig(transformer, config, this.options);
|
|
555
|
+
|
|
556
|
+
for (let devDep of config.devDeps) {
|
|
557
|
+
await this.addDevDependency(devDep);
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
return config;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
async runTransformer(
|
|
564
|
+
pipeline: Pipeline,
|
|
565
|
+
asset: UncommittedAsset,
|
|
566
|
+
transformer: Transformer<mixed>,
|
|
567
|
+
transformerName: string,
|
|
568
|
+
preloadedConfig: ?Config,
|
|
569
|
+
configKeyPath?: string,
|
|
570
|
+
atlaspackConfig: AtlaspackConfig,
|
|
571
|
+
): Promise<$ReadOnlyArray<TransformerResult | UncommittedAsset>> {
|
|
572
|
+
const logger = new PluginLogger({origin: transformerName});
|
|
573
|
+
const tracer = new PluginTracer({
|
|
574
|
+
origin: transformerName,
|
|
575
|
+
category: 'transform',
|
|
576
|
+
});
|
|
577
|
+
|
|
578
|
+
const resolve = async (
|
|
579
|
+
from: FilePath,
|
|
580
|
+
to: string,
|
|
581
|
+
options?: ResolveOptions,
|
|
582
|
+
): Promise<FilePath> => {
|
|
583
|
+
let result = await this.resolverRunner.resolve(
|
|
584
|
+
createDependency(this.options.projectRoot, {
|
|
585
|
+
env: asset.value.env,
|
|
586
|
+
specifier: to,
|
|
587
|
+
specifierType: options?.specifierType || 'esm',
|
|
588
|
+
packageConditions: options?.packageConditions,
|
|
589
|
+
sourcePath: from,
|
|
590
|
+
}),
|
|
591
|
+
);
|
|
592
|
+
|
|
593
|
+
if (result.invalidateOnFileCreate) {
|
|
594
|
+
this.invalidations.invalidateOnFileCreate.push(
|
|
595
|
+
...result.invalidateOnFileCreate.map(i =>
|
|
596
|
+
invalidateOnFileCreateToInternal(this.options.projectRoot, i),
|
|
597
|
+
),
|
|
598
|
+
);
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
if (result.invalidateOnFileChange) {
|
|
602
|
+
for (let filePath of result.invalidateOnFileChange) {
|
|
603
|
+
this.invalidations.invalidateOnFileChange.add(
|
|
604
|
+
toProjectPath(this.options.projectRoot, filePath),
|
|
605
|
+
);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
if (result.diagnostics && result.diagnostics.length > 0) {
|
|
610
|
+
throw new ThrowableDiagnostic({diagnostic: result.diagnostics});
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
return fromProjectPath(
|
|
614
|
+
this.options.projectRoot,
|
|
615
|
+
nullthrows(result.assetGroup).filePath,
|
|
616
|
+
);
|
|
617
|
+
};
|
|
618
|
+
|
|
619
|
+
// If an ast exists on the asset, but we cannot reuse it,
|
|
620
|
+
// use the previous transform to generate code that we can re-parse.
|
|
621
|
+
if (
|
|
622
|
+
asset.ast &&
|
|
623
|
+
asset.isASTDirty &&
|
|
624
|
+
(!transformer.canReuseAST ||
|
|
625
|
+
!transformer.canReuseAST({
|
|
626
|
+
ast: asset.ast,
|
|
627
|
+
options: pipeline.pluginOptions,
|
|
628
|
+
logger,
|
|
629
|
+
tracer,
|
|
630
|
+
})) &&
|
|
631
|
+
asset.generate
|
|
632
|
+
) {
|
|
633
|
+
let output = await asset.generate();
|
|
634
|
+
asset.content = output.content;
|
|
635
|
+
asset.mapBuffer = output.map?.toBuffer();
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
// Load config for the transformer.
|
|
639
|
+
let config = preloadedConfig;
|
|
640
|
+
|
|
641
|
+
// Parse if there is no AST available from a previous transform.
|
|
642
|
+
let parse = transformer.parse?.bind(transformer);
|
|
643
|
+
if (!asset.ast && parse) {
|
|
644
|
+
let ast = await parse({
|
|
645
|
+
asset: new Asset(asset),
|
|
646
|
+
config,
|
|
647
|
+
options: pipeline.pluginOptions,
|
|
648
|
+
resolve,
|
|
649
|
+
logger,
|
|
650
|
+
tracer,
|
|
651
|
+
});
|
|
652
|
+
if (ast) {
|
|
653
|
+
asset.setAST(ast);
|
|
654
|
+
asset.isASTDirty = false;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
// Transform.
|
|
659
|
+
let transfomerResult: Array<TransformerResult | MutableAsset> =
|
|
660
|
+
// $FlowFixMe the returned IMutableAsset really is a MutableAsset
|
|
661
|
+
await transformer.transform({
|
|
662
|
+
asset: new MutableAsset(asset),
|
|
663
|
+
config,
|
|
664
|
+
options: pipeline.pluginOptions,
|
|
665
|
+
resolve,
|
|
666
|
+
logger,
|
|
667
|
+
tracer,
|
|
668
|
+
});
|
|
669
|
+
let results = await normalizeAssets(this.options, transfomerResult);
|
|
670
|
+
|
|
671
|
+
// Create generate and postProcess function that can be called later
|
|
672
|
+
asset.generate = (): Promise<GenerateOutput> => {
|
|
673
|
+
let publicAsset = new Asset(asset);
|
|
674
|
+
if (transformer.generate && asset.ast) {
|
|
675
|
+
let generated = transformer.generate({
|
|
676
|
+
asset: publicAsset,
|
|
677
|
+
ast: asset.ast,
|
|
678
|
+
options: pipeline.pluginOptions,
|
|
679
|
+
logger,
|
|
680
|
+
tracer,
|
|
681
|
+
});
|
|
682
|
+
asset.clearAST();
|
|
683
|
+
return Promise.resolve(generated);
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
throw new Error(
|
|
687
|
+
'Asset has an AST but no generate method is available on the transform',
|
|
688
|
+
);
|
|
689
|
+
};
|
|
690
|
+
|
|
691
|
+
let postProcess = transformer.postProcess;
|
|
692
|
+
if (postProcess) {
|
|
693
|
+
pipeline.postProcess = async (
|
|
694
|
+
assets: Array<UncommittedAsset>,
|
|
695
|
+
): Promise<Array<UncommittedAsset> | null> => {
|
|
696
|
+
let results = await postProcess.call(transformer, {
|
|
697
|
+
assets: assets.map(asset => new MutableAsset(asset)),
|
|
698
|
+
config,
|
|
699
|
+
options: pipeline.pluginOptions,
|
|
700
|
+
resolve,
|
|
701
|
+
logger,
|
|
702
|
+
tracer,
|
|
703
|
+
});
|
|
704
|
+
|
|
705
|
+
return Promise.all(
|
|
706
|
+
results.map(result =>
|
|
707
|
+
asset.createChildAsset(
|
|
708
|
+
result,
|
|
709
|
+
transformerName,
|
|
710
|
+
atlaspackConfig.filePath,
|
|
711
|
+
// configKeyPath,
|
|
712
|
+
),
|
|
713
|
+
),
|
|
714
|
+
);
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
return results;
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
type Pipeline = {|
|
|
723
|
+
id: string,
|
|
724
|
+
transformers: Array<TransformerWithNameAndConfig>,
|
|
725
|
+
options: AtlaspackOptions,
|
|
726
|
+
pluginOptions: PluginOptions,
|
|
727
|
+
workerApi: WorkerApi,
|
|
728
|
+
postProcess?: PostProcessFunc,
|
|
729
|
+
generate?: GenerateFunc,
|
|
730
|
+
|};
|
|
731
|
+
|
|
732
|
+
type TransformerWithNameAndConfig = {|
|
|
733
|
+
name: PackageName,
|
|
734
|
+
plugin: Transformer<mixed>,
|
|
735
|
+
config: ?Config,
|
|
736
|
+
configKeyPath?: string,
|
|
737
|
+
resolveFrom: ProjectPath,
|
|
738
|
+
range?: ?SemverRange,
|
|
739
|
+
|};
|
|
740
|
+
|
|
741
|
+
function normalizeAssets(
|
|
742
|
+
options,
|
|
743
|
+
results: Array<TransformerResult | MutableAsset>,
|
|
744
|
+
): Array<TransformerResult | UncommittedAsset> {
|
|
745
|
+
return results.map(result => {
|
|
746
|
+
if (result instanceof MutableAsset) {
|
|
747
|
+
return mutableAssetToUncommittedAsset(result);
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
return result;
|
|
751
|
+
});
|
|
752
|
+
}
|