@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,278 @@
|
|
|
1
|
+
// @flow strict-local
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
FilePath,
|
|
5
|
+
InitialAtlaspackOptions,
|
|
6
|
+
DependencySpecifier,
|
|
7
|
+
InitialServerOptions,
|
|
8
|
+
} from '@atlaspack/types';
|
|
9
|
+
import type {FileSystem} from '@atlaspack/fs';
|
|
10
|
+
import type {AtlaspackOptions} from './types';
|
|
11
|
+
|
|
12
|
+
import path from 'path';
|
|
13
|
+
import {hashString} from '@atlaspack/rust';
|
|
14
|
+
import {NodeFS} from '@atlaspack/fs';
|
|
15
|
+
import {LMDBCache, FSCache} from '@atlaspack/cache';
|
|
16
|
+
import {NodePackageManager} from '@atlaspack/package-manager';
|
|
17
|
+
import {
|
|
18
|
+
getRootDir,
|
|
19
|
+
relativePath,
|
|
20
|
+
resolveConfig,
|
|
21
|
+
isGlob,
|
|
22
|
+
globToRegex,
|
|
23
|
+
} from '@atlaspack/utils';
|
|
24
|
+
import loadDotEnv from './loadDotEnv';
|
|
25
|
+
import {toProjectPath} from './projectPath';
|
|
26
|
+
import {getResolveFrom} from './requests/AtlaspackConfigRequest';
|
|
27
|
+
|
|
28
|
+
import {DEFAULT_FEATURE_FLAGS} from '@atlaspack/feature-flags';
|
|
29
|
+
import {ATLASPACK_VERSION} from './constants';
|
|
30
|
+
|
|
31
|
+
// Default cache directory name
|
|
32
|
+
const DEFAULT_CACHE_DIRNAME = '.atlaspack-cache';
|
|
33
|
+
const LOCK_FILE_NAMES = ['yarn.lock', 'package-lock.json', 'pnpm-lock.yaml'];
|
|
34
|
+
|
|
35
|
+
// Generate a unique instanceId, will change on every run of atlaspack
|
|
36
|
+
function generateInstanceId(entries: Array<FilePath>): string {
|
|
37
|
+
return hashString(
|
|
38
|
+
`${entries.join(',')}-${Date.now()}-${Math.round(Math.random() * 100)}`,
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Compiles an array of globs to regex - used for lazy include/excludes
|
|
43
|
+
function compileGlobs(globs: string[]): RegExp[] {
|
|
44
|
+
return globs.map(glob => globToRegex(glob));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export default async function resolveOptions(
|
|
48
|
+
initialOptions: InitialAtlaspackOptions,
|
|
49
|
+
): Promise<AtlaspackOptions> {
|
|
50
|
+
let inputFS = initialOptions.inputFS || new NodeFS();
|
|
51
|
+
let outputFS = initialOptions.outputFS || new NodeFS();
|
|
52
|
+
|
|
53
|
+
let inputCwd = inputFS.cwd();
|
|
54
|
+
let outputCwd = outputFS.cwd();
|
|
55
|
+
|
|
56
|
+
let entries: Array<FilePath>;
|
|
57
|
+
if (initialOptions.entries == null || initialOptions.entries === '') {
|
|
58
|
+
entries = [];
|
|
59
|
+
} else if (Array.isArray(initialOptions.entries)) {
|
|
60
|
+
entries = initialOptions.entries.map(entry =>
|
|
61
|
+
path.resolve(inputCwd, entry),
|
|
62
|
+
);
|
|
63
|
+
} else {
|
|
64
|
+
entries = [path.resolve(inputCwd, initialOptions.entries)];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
let shouldMakeEntryReferFolder = false;
|
|
68
|
+
if (entries.length === 1 && !isGlob(entries[0])) {
|
|
69
|
+
let [entry] = entries;
|
|
70
|
+
try {
|
|
71
|
+
shouldMakeEntryReferFolder = (await inputFS.stat(entry)).isDirectory();
|
|
72
|
+
} catch {
|
|
73
|
+
// ignore failing stat call
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// getRootDir treats the input as files, so getRootDir(["/home/user/myproject"]) returns "/home/user".
|
|
78
|
+
// Instead we need to make the the entry refer to some file inside the specified folders if entries refers to the directory.
|
|
79
|
+
let entryRoot = getRootDir(
|
|
80
|
+
shouldMakeEntryReferFolder ? [path.join(entries[0], 'index')] : entries,
|
|
81
|
+
);
|
|
82
|
+
let projectRootFile =
|
|
83
|
+
(await resolveConfig(
|
|
84
|
+
inputFS,
|
|
85
|
+
path.join(entryRoot, 'index'),
|
|
86
|
+
[...LOCK_FILE_NAMES, '.git', '.hg'],
|
|
87
|
+
path.parse(entryRoot).root,
|
|
88
|
+
)) || path.join(inputCwd, 'index'); // ? Should this just be rootDir
|
|
89
|
+
|
|
90
|
+
let projectRoot = path.dirname(projectRootFile);
|
|
91
|
+
|
|
92
|
+
let packageManager =
|
|
93
|
+
initialOptions.packageManager ||
|
|
94
|
+
new NodePackageManager(inputFS, projectRoot);
|
|
95
|
+
|
|
96
|
+
let cacheDir =
|
|
97
|
+
// If a cacheDir is provided, resolve it relative to cwd. Otherwise,
|
|
98
|
+
// use a default directory resolved relative to the project root.
|
|
99
|
+
initialOptions.cacheDir != null
|
|
100
|
+
? path.resolve(outputCwd, initialOptions.cacheDir)
|
|
101
|
+
: path.resolve(projectRoot, DEFAULT_CACHE_DIRNAME);
|
|
102
|
+
|
|
103
|
+
// Make the root watch directory configurable. This is useful in some cases
|
|
104
|
+
// where symlinked dependencies outside the project root need to trigger HMR
|
|
105
|
+
// updates. Default to the project root if not provided.
|
|
106
|
+
let watchDir =
|
|
107
|
+
initialOptions.watchDir != null
|
|
108
|
+
? path.resolve(initialOptions.watchDir)
|
|
109
|
+
: projectRoot;
|
|
110
|
+
|
|
111
|
+
let cache =
|
|
112
|
+
initialOptions.cache ??
|
|
113
|
+
(outputFS instanceof NodeFS
|
|
114
|
+
? new LMDBCache(cacheDir)
|
|
115
|
+
: new FSCache(outputFS, cacheDir));
|
|
116
|
+
|
|
117
|
+
let mode = initialOptions.mode ?? 'development';
|
|
118
|
+
let shouldOptimize =
|
|
119
|
+
initialOptions?.defaultTargetOptions?.shouldOptimize ??
|
|
120
|
+
mode === 'production';
|
|
121
|
+
|
|
122
|
+
let publicUrl = initialOptions?.defaultTargetOptions?.publicUrl ?? '/';
|
|
123
|
+
let distDir =
|
|
124
|
+
initialOptions?.defaultTargetOptions?.distDir != null
|
|
125
|
+
? path.resolve(inputCwd, initialOptions?.defaultTargetOptions?.distDir)
|
|
126
|
+
: undefined;
|
|
127
|
+
|
|
128
|
+
let shouldBuildLazily = initialOptions.shouldBuildLazily ?? false;
|
|
129
|
+
let lazyIncludes = compileGlobs(initialOptions.lazyIncludes ?? []);
|
|
130
|
+
if (lazyIncludes.length > 0 && !shouldBuildLazily) {
|
|
131
|
+
throw new Error(
|
|
132
|
+
'Lazy includes can only be provided when lazy building is enabled',
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
let lazyExcludes = compileGlobs(initialOptions.lazyExcludes ?? []);
|
|
136
|
+
if (lazyExcludes.length > 0 && !shouldBuildLazily) {
|
|
137
|
+
throw new Error(
|
|
138
|
+
'Lazy excludes can only be provided when lazy building is enabled',
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
let shouldContentHash =
|
|
143
|
+
initialOptions.shouldContentHash ?? initialOptions.mode === 'production';
|
|
144
|
+
if (shouldBuildLazily && shouldContentHash) {
|
|
145
|
+
throw new Error('Lazy bundling does not work with content hashing');
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
let env = {
|
|
149
|
+
...(await loadDotEnv(
|
|
150
|
+
initialOptions.env ?? {},
|
|
151
|
+
inputFS,
|
|
152
|
+
path.join(projectRoot, 'index'),
|
|
153
|
+
projectRoot,
|
|
154
|
+
)),
|
|
155
|
+
...process.env,
|
|
156
|
+
...initialOptions.env,
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
let port = determinePort(initialOptions.serveOptions, env.PORT);
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
config: getRelativeConfigSpecifier(
|
|
163
|
+
inputFS,
|
|
164
|
+
projectRoot,
|
|
165
|
+
initialOptions.config,
|
|
166
|
+
),
|
|
167
|
+
defaultConfig: getRelativeConfigSpecifier(
|
|
168
|
+
inputFS,
|
|
169
|
+
projectRoot,
|
|
170
|
+
initialOptions.defaultConfig,
|
|
171
|
+
),
|
|
172
|
+
shouldPatchConsole: initialOptions.shouldPatchConsole ?? false,
|
|
173
|
+
env,
|
|
174
|
+
mode,
|
|
175
|
+
shouldAutoInstall: initialOptions.shouldAutoInstall ?? false,
|
|
176
|
+
hmrOptions: initialOptions.hmrOptions ?? null,
|
|
177
|
+
shouldBuildLazily,
|
|
178
|
+
lazyIncludes,
|
|
179
|
+
lazyExcludes,
|
|
180
|
+
unstableFileInvalidations: initialOptions.unstableFileInvalidations,
|
|
181
|
+
shouldBundleIncrementally: initialOptions.shouldBundleIncrementally ?? true,
|
|
182
|
+
shouldContentHash,
|
|
183
|
+
serveOptions: initialOptions.serveOptions
|
|
184
|
+
? {
|
|
185
|
+
...initialOptions.serveOptions,
|
|
186
|
+
distDir: distDir ?? path.join(outputCwd, 'dist'),
|
|
187
|
+
port,
|
|
188
|
+
}
|
|
189
|
+
: false,
|
|
190
|
+
shouldDisableCache: initialOptions.shouldDisableCache ?? false,
|
|
191
|
+
shouldProfile: initialOptions.shouldProfile ?? false,
|
|
192
|
+
shouldTrace: initialOptions.shouldTrace ?? false,
|
|
193
|
+
cacheDir,
|
|
194
|
+
watchDir,
|
|
195
|
+
watchBackend: initialOptions.watchBackend,
|
|
196
|
+
watchIgnore: initialOptions.watchIgnore,
|
|
197
|
+
entries: entries.map(e => toProjectPath(projectRoot, e)),
|
|
198
|
+
targets: initialOptions.targets,
|
|
199
|
+
logLevel: initialOptions.logLevel ?? 'info',
|
|
200
|
+
projectRoot,
|
|
201
|
+
inputFS,
|
|
202
|
+
outputFS,
|
|
203
|
+
cache,
|
|
204
|
+
packageManager,
|
|
205
|
+
additionalReporters:
|
|
206
|
+
initialOptions.additionalReporters?.map(({packageName, resolveFrom}) => ({
|
|
207
|
+
packageName,
|
|
208
|
+
resolveFrom: toProjectPath(projectRoot, resolveFrom),
|
|
209
|
+
})) ?? [],
|
|
210
|
+
instanceId: generateInstanceId(entries),
|
|
211
|
+
detailedReport: initialOptions.detailedReport,
|
|
212
|
+
defaultTargetOptions: {
|
|
213
|
+
shouldOptimize,
|
|
214
|
+
shouldScopeHoist: initialOptions?.defaultTargetOptions?.shouldScopeHoist,
|
|
215
|
+
sourceMaps: initialOptions?.defaultTargetOptions?.sourceMaps ?? true,
|
|
216
|
+
publicUrl,
|
|
217
|
+
...(distDir != null
|
|
218
|
+
? {distDir: toProjectPath(projectRoot, distDir)}
|
|
219
|
+
: {
|
|
220
|
+
/*::...null*/
|
|
221
|
+
}),
|
|
222
|
+
engines: initialOptions?.defaultTargetOptions?.engines,
|
|
223
|
+
outputFormat: initialOptions?.defaultTargetOptions?.outputFormat,
|
|
224
|
+
isLibrary: initialOptions?.defaultTargetOptions?.isLibrary,
|
|
225
|
+
},
|
|
226
|
+
featureFlags: {...DEFAULT_FEATURE_FLAGS, ...initialOptions?.featureFlags},
|
|
227
|
+
atlaspackVersion: ATLASPACK_VERSION,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function getRelativeConfigSpecifier(
|
|
232
|
+
fs: FileSystem,
|
|
233
|
+
projectRoot: FilePath,
|
|
234
|
+
specifier: ?DependencySpecifier,
|
|
235
|
+
) {
|
|
236
|
+
if (specifier == null) {
|
|
237
|
+
return undefined;
|
|
238
|
+
} else if (path.isAbsolute(specifier)) {
|
|
239
|
+
let resolveFrom = getResolveFrom(fs, projectRoot);
|
|
240
|
+
let relative = relativePath(path.dirname(resolveFrom), specifier);
|
|
241
|
+
// If the config is outside the project root, use an absolute path so that if the project root
|
|
242
|
+
// moves the path still works. Otherwise, use a relative path so that the cache is portable.
|
|
243
|
+
return relative.startsWith('..') ? specifier : relative;
|
|
244
|
+
} else {
|
|
245
|
+
return specifier;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function determinePort(
|
|
250
|
+
initialServerOptions: InitialServerOptions | false | void,
|
|
251
|
+
portInEnv: string | void,
|
|
252
|
+
defaultPort: number = 1234,
|
|
253
|
+
): number {
|
|
254
|
+
function parsePort(port: string): number | void {
|
|
255
|
+
let parsedPort = Number(port);
|
|
256
|
+
|
|
257
|
+
// return undefined if port number defined in .env is not valid integer
|
|
258
|
+
if (!Number.isInteger(parsedPort)) {
|
|
259
|
+
return undefined;
|
|
260
|
+
}
|
|
261
|
+
return parsedPort;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (!initialServerOptions) {
|
|
265
|
+
return typeof portInEnv !== 'undefined'
|
|
266
|
+
? parsePort(portInEnv) ?? defaultPort
|
|
267
|
+
: defaultPort;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// if initialServerOptions.port is equal to defaultPort, then this means that port number is provided via PORT=~~~~ on cli. In this case, we should ignore port number defined in .env.
|
|
271
|
+
if (initialServerOptions.port !== defaultPort) {
|
|
272
|
+
return initialServerOptions.port;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return typeof portInEnv !== 'undefined'
|
|
276
|
+
? parsePort(portInEnv) ?? defaultPort
|
|
277
|
+
: defaultPort;
|
|
278
|
+
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
import {createBuildCache} from './buildCache';
|
|
3
|
+
import {serializeRaw, deserializeRaw} from './serializerCore';
|
|
4
|
+
|
|
5
|
+
export {serializeRaw, deserializeRaw} from './serializerCore';
|
|
6
|
+
|
|
7
|
+
const nameToCtor: Map<string, Class<any>> = new Map();
|
|
8
|
+
const ctorToName: Map<Class<any>, string> = new Map();
|
|
9
|
+
|
|
10
|
+
export function registerSerializableClass(name: string, ctor: Class<any>) {
|
|
11
|
+
if (ctorToName.has(ctor)) {
|
|
12
|
+
throw new Error('Class already registered with serializer');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
nameToCtor.set(name, ctor);
|
|
16
|
+
ctorToName.set(ctor, name);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function unregisterSerializableClass(name: string, ctor: Class<any>) {
|
|
20
|
+
if (nameToCtor.get(name) === ctor) {
|
|
21
|
+
nameToCtor.delete(name);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (ctorToName.get(ctor) === name) {
|
|
25
|
+
ctorToName.delete(ctor);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function shallowCopy(object: any) {
|
|
30
|
+
if (object && typeof object === 'object') {
|
|
31
|
+
if (Array.isArray(object)) {
|
|
32
|
+
return [...object];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (object instanceof Map) {
|
|
36
|
+
return new Map(object);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (object instanceof Set) {
|
|
40
|
+
return new Set(object);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return Object.create(
|
|
44
|
+
Object.getPrototypeOf(object),
|
|
45
|
+
Object.getOwnPropertyDescriptors(object),
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return object;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function isBuffer(object) {
|
|
53
|
+
return (
|
|
54
|
+
object.buffer instanceof ArrayBuffer ||
|
|
55
|
+
(typeof SharedArrayBuffer !== 'undefined' &&
|
|
56
|
+
object.buffer instanceof SharedArrayBuffer)
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function shouldContinueMapping(value) {
|
|
61
|
+
return value && typeof value === 'object' && value.$$raw !== true;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function mapObject(object: any, fn: (val: any) => any, preOrder = false): any {
|
|
65
|
+
let cache = new Map();
|
|
66
|
+
let memo = new Map();
|
|
67
|
+
|
|
68
|
+
// Memoize the passed function to ensure it always returns the exact same
|
|
69
|
+
// output by reference for the same input. This is important to maintain
|
|
70
|
+
// reference integrity when deserializing rather than cloning.
|
|
71
|
+
let memoizedFn = (val: any) => {
|
|
72
|
+
let res = memo.get(val);
|
|
73
|
+
if (res == null) {
|
|
74
|
+
res = fn(val);
|
|
75
|
+
memo.set(val, res);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return res;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
let walk = (object: any, shouldCopy = false) => {
|
|
82
|
+
// Check the cache first, both for performance and cycle detection.
|
|
83
|
+
if (cache.has(object)) {
|
|
84
|
+
return cache.get(object);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
let result = object;
|
|
88
|
+
cache.set(object, result);
|
|
89
|
+
|
|
90
|
+
let processKey = (key: any, value: any) => {
|
|
91
|
+
let newValue = value;
|
|
92
|
+
if (preOrder && value && typeof value === 'object') {
|
|
93
|
+
newValue = memoizedFn(value);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Recursively walk the children
|
|
97
|
+
if (
|
|
98
|
+
preOrder
|
|
99
|
+
? shouldContinueMapping(newValue)
|
|
100
|
+
: newValue &&
|
|
101
|
+
typeof newValue === 'object' &&
|
|
102
|
+
shouldContinueMapping(object)
|
|
103
|
+
) {
|
|
104
|
+
newValue = walk(newValue, newValue === value);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (!preOrder && newValue && typeof newValue === 'object') {
|
|
108
|
+
newValue = memoizedFn(newValue);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (newValue !== value) {
|
|
112
|
+
// Copy on write. We only need to do this when serializing, not deserializing.
|
|
113
|
+
if (object === result && preOrder && shouldCopy) {
|
|
114
|
+
result = shallowCopy(object);
|
|
115
|
+
cache.set(object, result);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Replace the key with the new value
|
|
119
|
+
if (result instanceof Map) {
|
|
120
|
+
result.set(key, newValue);
|
|
121
|
+
} else if (result instanceof Set) {
|
|
122
|
+
let _result = result; // For Flow
|
|
123
|
+
// TODO: do we care about iteration order??
|
|
124
|
+
_result.delete(value);
|
|
125
|
+
_result.add(newValue);
|
|
126
|
+
} else {
|
|
127
|
+
result[key] = newValue;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
// Iterate in various ways depending on type.
|
|
133
|
+
if (Array.isArray(object)) {
|
|
134
|
+
for (let i = 0; i < object.length; i++) {
|
|
135
|
+
processKey(i, object[i]);
|
|
136
|
+
}
|
|
137
|
+
} else if (object instanceof Map || object instanceof Set) {
|
|
138
|
+
for (let [key, val] of object.entries()) {
|
|
139
|
+
processKey(key, val);
|
|
140
|
+
}
|
|
141
|
+
} else if (!isBuffer(object)) {
|
|
142
|
+
for (let key in object) {
|
|
143
|
+
processKey(key, object[key]);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return result;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
let mapped = memoizedFn(object);
|
|
151
|
+
if (
|
|
152
|
+
preOrder
|
|
153
|
+
? shouldContinueMapping(mapped)
|
|
154
|
+
: mapped && typeof mapped === 'object' && shouldContinueMapping(object)
|
|
155
|
+
) {
|
|
156
|
+
return walk(mapped, mapped === object);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return mapped;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function prepareForSerialization(object: any): any {
|
|
163
|
+
if (object?.$$raw) {
|
|
164
|
+
return object;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return mapObject(
|
|
168
|
+
object,
|
|
169
|
+
value => {
|
|
170
|
+
// Add a $$type property with the name of this class, if any is registered.
|
|
171
|
+
if (
|
|
172
|
+
value &&
|
|
173
|
+
typeof value === 'object' &&
|
|
174
|
+
typeof value.constructor === 'function'
|
|
175
|
+
) {
|
|
176
|
+
let type = ctorToName.get(value.constructor);
|
|
177
|
+
if (type != null) {
|
|
178
|
+
let serialized = value;
|
|
179
|
+
let raw = false;
|
|
180
|
+
if (value && typeof value.serialize === 'function') {
|
|
181
|
+
// If the object has a serialize method, call it
|
|
182
|
+
serialized = value.serialize();
|
|
183
|
+
raw = (serialized && serialized.$$raw) ?? true;
|
|
184
|
+
if (serialized) {
|
|
185
|
+
delete serialized.$$raw;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return {
|
|
190
|
+
$$type: type,
|
|
191
|
+
$$raw: raw,
|
|
192
|
+
value: {...serialized},
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return value;
|
|
198
|
+
},
|
|
199
|
+
true,
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function restoreDeserializedObject(object: any): any {
|
|
204
|
+
return mapObject(object, value => {
|
|
205
|
+
// If the value has a $$type property, use it to restore the object type
|
|
206
|
+
if (value && value.$$type) {
|
|
207
|
+
let ctor = nameToCtor.get(value.$$type);
|
|
208
|
+
if (ctor == null) {
|
|
209
|
+
throw new Error(
|
|
210
|
+
`Expected constructor ${value.$$type} to be registered with serializer to deserialize`,
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (typeof ctor.deserialize === 'function') {
|
|
215
|
+
return ctor.deserialize(value.value);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
value = value.value;
|
|
219
|
+
Object.setPrototypeOf(value, ctor.prototype);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return value;
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const serializeCache = createBuildCache();
|
|
227
|
+
|
|
228
|
+
export function serialize(object: any): Buffer {
|
|
229
|
+
let cached = serializeCache.get(object);
|
|
230
|
+
if (cached) {
|
|
231
|
+
return cached;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
let mapped = prepareForSerialization(object);
|
|
235
|
+
return serializeRaw(mapped);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export function deserialize(buffer: Buffer): any {
|
|
239
|
+
let obj = deserializeRaw(buffer);
|
|
240
|
+
return restoreDeserializedObject(obj);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export function cacheSerializedObject(object: any, buffer?: Buffer): void {
|
|
244
|
+
serializeCache.set(object, buffer || serialize(object));
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export function deserializeToCache(buffer: Buffer): any {
|
|
248
|
+
let deserialized = deserialize(buffer);
|
|
249
|
+
serializeCache.set(deserialized, buffer);
|
|
250
|
+
return deserialized;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export function removeSerializedObjectFromCache(object: any) {
|
|
254
|
+
serializeCache.delete(object);
|
|
255
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
import {Buffer} from 'buffer';
|
|
3
|
+
import * as msgpackr from 'msgpackr';
|
|
4
|
+
|
|
5
|
+
let encoder = new msgpackr.Encoder({structuredClone: true});
|
|
6
|
+
|
|
7
|
+
export let serializeRaw: any => Buffer = v => Buffer.from(encoder.encode(v));
|
|
8
|
+
export let deserializeRaw: Buffer => any = v => encoder.decode(v);
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// @flow strict-local
|
|
2
|
+
import type {Blob, FilePath} from '@atlaspack/types';
|
|
3
|
+
import type {FileSystem} from '@atlaspack/fs';
|
|
4
|
+
|
|
5
|
+
import path from 'path';
|
|
6
|
+
|
|
7
|
+
const NODE_MODULES = `${path.sep}node_modules${path.sep}`;
|
|
8
|
+
|
|
9
|
+
const BUFFER_LIMIT = 5000000; // 5mb
|
|
10
|
+
|
|
11
|
+
export default async function summarizeRequest(
|
|
12
|
+
fs: FileSystem,
|
|
13
|
+
req: {|filePath: FilePath, code?: string|},
|
|
14
|
+
): Promise<{|content: Blob, size: number, isSource: boolean|}> {
|
|
15
|
+
let {content, size} = await summarizeDiskRequest(fs, req);
|
|
16
|
+
let isSource = isFilePathSource(fs, req.filePath);
|
|
17
|
+
return {content, size, isSource};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function isFilePathSource(fs: FileSystem, filePath: FilePath) {
|
|
21
|
+
return !filePath.includes(NODE_MODULES);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function summarizeDiskRequest(
|
|
25
|
+
fs: FileSystem,
|
|
26
|
+
req: {|filePath: FilePath, code?: string|},
|
|
27
|
+
): Promise<{|content: Blob, size: number|}> {
|
|
28
|
+
let code = req.code;
|
|
29
|
+
let content: Blob;
|
|
30
|
+
let size: number;
|
|
31
|
+
if (code == null) {
|
|
32
|
+
// Get the filesize. If greater than BUFFER_LIMIT, use a stream to
|
|
33
|
+
// compute the hash. In the common case, it's faster to just read the entire
|
|
34
|
+
// file first and do the hash all at once without the overhead of streams.
|
|
35
|
+
size = (await fs.stat(req.filePath)).size;
|
|
36
|
+
if (size > BUFFER_LIMIT) {
|
|
37
|
+
content = fs.createReadStream(req.filePath);
|
|
38
|
+
} else {
|
|
39
|
+
content = await fs.readFile(req.filePath);
|
|
40
|
+
}
|
|
41
|
+
} else {
|
|
42
|
+
content = code;
|
|
43
|
+
size = Buffer.byteLength(code);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return {content, size};
|
|
47
|
+
}
|