@atlaspack/package-manager 2.14.5-canary.36 → 2.14.5-canary.360

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.
Files changed (63) hide show
  1. package/CHANGELOG.md +491 -0
  2. package/dist/JSONParseStream.js +37 -0
  3. package/dist/MockPackageInstaller.js +54 -0
  4. package/dist/NodePackageManager.js +559 -0
  5. package/dist/Npm.js +73 -0
  6. package/dist/Pnpm.js +140 -0
  7. package/dist/Yarn.js +124 -0
  8. package/dist/getCurrentPackageManager.js +17 -0
  9. package/dist/index.js +24 -0
  10. package/dist/installPackage.js +197 -0
  11. package/dist/nodejsConditions.js +39 -0
  12. package/dist/promiseFromProcess.js +16 -0
  13. package/dist/utils.js +76 -0
  14. package/dist/validateModuleSpecifier.js +11 -0
  15. package/lib/JSONParseStream.js +52 -0
  16. package/lib/MockPackageInstaller.js +79 -0
  17. package/lib/NodePackageManager.js +621 -0
  18. package/lib/Npm.js +106 -0
  19. package/lib/Pnpm.js +185 -0
  20. package/lib/Yarn.js +160 -0
  21. package/lib/getCurrentPackageManager.js +20 -0
  22. package/lib/index.js +64 -5211
  23. package/lib/installPackage.js +222 -0
  24. package/lib/nodejsConditions.js +41 -0
  25. package/lib/promiseFromProcess.js +20 -0
  26. package/lib/types/JSONParseStream.d.ts +6 -0
  27. package/lib/types/MockPackageInstaller.d.ts +14 -0
  28. package/lib/types/NodePackageManager.d.ts +37 -0
  29. package/lib/types/Npm.d.ts +4 -0
  30. package/lib/types/Pnpm.d.ts +5 -0
  31. package/lib/types/Yarn.d.ts +5 -0
  32. package/lib/types/getCurrentPackageManager.d.ts +4 -0
  33. package/lib/types/index.d.ts +10 -0
  34. package/lib/types/installPackage.d.ts +5 -0
  35. package/lib/types/nodejsConditions.d.ts +3 -0
  36. package/lib/types/promiseFromProcess.d.ts +2 -0
  37. package/lib/types/utils.d.ts +15 -0
  38. package/lib/types/validateModuleSpecifier.d.ts +1 -0
  39. package/lib/utils.js +101 -0
  40. package/lib/validateModuleSpecifier.js +14 -0
  41. package/package.json +17 -20
  42. package/src/{JSONParseStream.js → JSONParseStream.ts} +8 -7
  43. package/src/{MockPackageInstaller.js → MockPackageInstaller.ts} +4 -6
  44. package/src/{NodePackageManager.js → NodePackageManager.ts} +125 -73
  45. package/src/{Npm.js → Npm.ts} +9 -9
  46. package/src/{Pnpm.js → Pnpm.ts} +68 -50
  47. package/src/{Yarn.js → Yarn.ts} +38 -25
  48. package/src/{getCurrentPackageManager.js → getCurrentPackageManager.ts} +9 -4
  49. package/src/{index.js → index.ts} +0 -2
  50. package/src/{installPackage.js → installPackage.ts} +4 -6
  51. package/src/{nodejsConditions.js → nodejsConditions.ts} +6 -3
  52. package/src/promiseFromProcess.ts +23 -0
  53. package/src/{utils.js → utils.ts} +21 -11
  54. package/src/{validateModuleSpecifier.js → validateModuleSpecifier.ts} +0 -2
  55. package/test/{NodePackageManager.test.js → NodePackageManager.test.ts} +19 -16
  56. package/test/{getCurrentPackageManager.test.js → getCurrentPackageManager.test.ts} +0 -1
  57. package/test/{validateModuleSpecifiers.test.js → validateModuleSpecifiers.test.ts} +2 -3
  58. package/tsconfig.json +33 -0
  59. package/tsconfig.tsbuildinfo +1 -0
  60. package/index.d.ts +0 -40
  61. package/lib/index.d.ts +0 -10
  62. package/lib/index.js.map +0 -1
  63. package/src/promiseFromProcess.js +0 -19
@@ -0,0 +1,621 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.NodePackageManager = void 0;
7
+ function _buildCache() {
8
+ const data = require("@atlaspack/build-cache");
9
+ _buildCache = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ function _diagnostic() {
15
+ const data = _interopRequireWildcard(require("@atlaspack/diagnostic"));
16
+ _diagnostic = function () {
17
+ return data;
18
+ };
19
+ return data;
20
+ }
21
+ function _fs() {
22
+ const data = require("@atlaspack/fs");
23
+ _fs = function () {
24
+ return data;
25
+ };
26
+ return data;
27
+ }
28
+ function _fs2() {
29
+ const data = _interopRequireDefault(require("fs"));
30
+ _fs2 = function () {
31
+ return data;
32
+ };
33
+ return data;
34
+ }
35
+ function _module() {
36
+ const data = _interopRequireDefault(require("module"));
37
+ _module = function () {
38
+ return data;
39
+ };
40
+ return data;
41
+ }
42
+ function _path() {
43
+ const data = _interopRequireDefault(require("path"));
44
+ _path = function () {
45
+ return data;
46
+ };
47
+ return data;
48
+ }
49
+ function _semver() {
50
+ const data = _interopRequireDefault(require("semver"));
51
+ _semver = function () {
52
+ return data;
53
+ };
54
+ return data;
55
+ }
56
+ function _logger() {
57
+ const data = _interopRequireDefault(require("@atlaspack/logger"));
58
+ _logger = function () {
59
+ return data;
60
+ };
61
+ return data;
62
+ }
63
+ function _nullthrows() {
64
+ const data = _interopRequireDefault(require("nullthrows"));
65
+ _nullthrows = function () {
66
+ return data;
67
+ };
68
+ return data;
69
+ }
70
+ function _utils() {
71
+ const data = require("@atlaspack/utils");
72
+ _utils = function () {
73
+ return data;
74
+ };
75
+ return data;
76
+ }
77
+ var _utils2 = require("./utils");
78
+ var _installPackage = require("./installPackage");
79
+ var _package = _interopRequireDefault(require("../package.json"));
80
+ var _nodejsConditions = require("./nodejsConditions");
81
+ function _nodeResolverCore() {
82
+ const data = require("@atlaspack/node-resolver-core");
83
+ _nodeResolverCore = function () {
84
+ return data;
85
+ };
86
+ return data;
87
+ }
88
+ function _url() {
89
+ const data = require("url");
90
+ _url = function () {
91
+ return data;
92
+ };
93
+ return data;
94
+ }
95
+ function _core() {
96
+ const data = require("@swc/core");
97
+ _core = function () {
98
+ return data;
99
+ };
100
+ return data;
101
+ }
102
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
103
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
104
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
105
+ // Package.json fields. Must match package_json.rs.
106
+ const MAIN = 1 << 0;
107
+ const SOURCE = 1 << 2;
108
+ let ENTRIES = MAIN;
109
+ if (process.env.ATLASPACK_REGISTER_USE_SRC === 'true') {
110
+ ENTRIES |= SOURCE;
111
+ }
112
+ const NODE_MODULES = `${_path().default.sep}node_modules${_path().default.sep}`;
113
+ const compileExtensions = new Set(['.ts', '.tsx', '.mts', '.cts']);
114
+
115
+ // There can be more than one instance of NodePackageManager, but node has only a single module cache.
116
+ // Therefore, the resolution cache and the map of parent to child modules should also be global.
117
+ const cache = new Map();
118
+ const children = new Map();
119
+ const invalidationsCache = new Map();
120
+
121
+ // This implements a package manager for Node by monkey patching the Node require
122
+ // algorithm so that it uses the specified FileSystem instead of the native one.
123
+ // It also handles installing packages when they are required if not already installed.
124
+ // See https://github.com/nodejs/node/blob/master/lib/internal/modules/cjs/loader.js
125
+ // for reference to Node internals.
126
+ class NodePackageManager {
127
+ // @ts-expect-error TS2749
128
+
129
+ constructor(fs, projectRoot, installer) {
130
+ this.fs = fs;
131
+ this.projectRoot = projectRoot;
132
+ this.installer = installer;
133
+
134
+ // If using src then assume we're linked and find the link root dir
135
+ this.atlaspackLinkRoot = process.env.ATLASPACK_REGISTER_USE_SRC === 'true' ? _path().default.resolve(__dirname, '../../../..') : null;
136
+
137
+ // @ts-expect-error TS2339
138
+ this.currentExtensions = Object.keys(_module().default._extensions).map(e => e.substring(1));
139
+ }
140
+
141
+ // @ts-expect-error TS2749
142
+ _createResolver() {
143
+ return new (_nodeResolverCore().ResolverBase)(this.projectRoot, {
144
+ fs: this.fs instanceof _fs().NodeFS && process.versions.pnp == null ? undefined : {
145
+ canonicalize: path => this.fs.realpathSync(path),
146
+ read: path => this.fs.readFileSync(path),
147
+ isFile: path => this.fs.statSync(path).isFile(),
148
+ isDir: path => this.fs.statSync(path).isDirectory()
149
+ },
150
+ mode: 2,
151
+ entries: ENTRIES,
152
+ packageExports: true,
153
+ moduleDirResolver: process.versions.pnp != null ? (module, from) => {
154
+ // @ts-expect-error TS2339
155
+ let pnp = _module().default.findPnpApi(_path().default.dirname(from));
156
+ return pnp.resolveToUnqualified(
157
+ // append slash to force loading builtins from npm
158
+ module + '/', from);
159
+ } : undefined,
160
+ extensions: this.currentExtensions,
161
+ typescript: true
162
+ });
163
+ }
164
+ static deserialize(opts) {
165
+ return new NodePackageManager(opts.fs, opts.projectRoot, opts.installer);
166
+ }
167
+ serialize() {
168
+ return {
169
+ $$raw: false,
170
+ fs: this.fs,
171
+ projectRoot: this.projectRoot,
172
+ installer: this.installer
173
+ };
174
+ }
175
+ async require(name, from, opts) {
176
+ let {
177
+ resolved,
178
+ type
179
+ } = await this.resolve(name, from, opts);
180
+ if (type === 2) {
181
+ _logger().default.warn({
182
+ message: 'ES module dependencies are experimental.',
183
+ origin: '@atlaspack/package-manager',
184
+ codeFrames: [{
185
+ filePath: resolved,
186
+ codeHighlights: []
187
+ }]
188
+ });
189
+
190
+ // On Windows, Node requires absolute paths to be file URLs.
191
+ if (process.platform === 'win32' && _path().default.isAbsolute(resolved)) {
192
+ // @ts-expect-error TS2322
193
+ resolved = (0, _url().pathToFileURL)(resolved);
194
+ }
195
+ return import(resolved);
196
+ }
197
+ return this.load(resolved, from);
198
+ }
199
+ requireSync(name, from) {
200
+ let {
201
+ resolved
202
+ } = this.resolveSync(name, from);
203
+ return this.load(resolved, from);
204
+ }
205
+ load(filePath, from) {
206
+ if (!_path().default.isAbsolute(filePath)) {
207
+ // Node builtin module
208
+ return require(filePath);
209
+ }
210
+
211
+ // @ts-expect-error TS2339
212
+ const cachedModule = _module().default._cache[filePath];
213
+ if (cachedModule !== undefined) {
214
+ return cachedModule.exports;
215
+ }
216
+
217
+ // @ts-expect-error TS2339
218
+ let m = new (_module().default)(filePath, _module().default._cache[from] || module.parent);
219
+
220
+ // @ts-expect-error TS2339
221
+ const extensions = Object.keys(_module().default._extensions);
222
+ // This handles supported extensions changing due to, for example, esbuild/register being used
223
+ // We assume that the extension list will change in size - as these tools usually add support for
224
+ // additional extensions.
225
+ if (extensions.length !== this.currentExtensions.length) {
226
+ this.currentExtensions = extensions.map(e => e.substring(1));
227
+ this.resolver = this._createResolver();
228
+ }
229
+
230
+ // @ts-expect-error TS2339
231
+ _module().default._cache[filePath] = m;
232
+
233
+ // Patch require within this module so it goes through our require
234
+ m.require = id => {
235
+ return this.requireSync(id, filePath);
236
+ };
237
+
238
+ // Patch `fs.readFileSync` temporarily so that it goes through our file system
239
+ let {
240
+ readFileSync,
241
+ statSync
242
+ } = _fs2().default;
243
+ // @ts-expect-error TS2322
244
+ _fs2().default.readFileSync = (filename, encoding) => {
245
+ return this.fs.readFileSync(filename, encoding);
246
+ };
247
+
248
+ // @ts-expect-error TS2540
249
+ _fs2().default.statSync = filename => {
250
+ return this.fs.statSync(filename);
251
+ };
252
+ let extname = _path().default.extname(filePath);
253
+ function shouldCompile(atlaspackLinkRoot) {
254
+ if (filePath.includes(NODE_MODULES)) {
255
+ // Don't compile node_modules
256
+ return false;
257
+ }
258
+ if (!compileExtensions.has(extname)) {
259
+ // Ignore non-TS files
260
+ return false;
261
+ }
262
+ if (atlaspackLinkRoot != null) {
263
+ // If we're linked, only compile files outside the linked atlaspack
264
+ // as those are handled by @atlaspack/babel-register
265
+ return !filePath.startsWith(atlaspackLinkRoot);
266
+ }
267
+
268
+ // @ts-expect-error TS2339
269
+ // Lastly make sure there's no existing loader for this extension
270
+ return !_module().default._extensions[extname];
271
+ }
272
+ if (shouldCompile(this.atlaspackLinkRoot)) {
273
+ // @ts-expect-error TS2339
274
+ let compile = m._compile;
275
+ // @ts-expect-error TS2339
276
+ m._compile = (code, filename) => {
277
+ let out = (0, _core().transformSync)(code, {
278
+ filename,
279
+ module: {
280
+ type: 'commonjs'
281
+ },
282
+ env: {
283
+ targets: {
284
+ node: '18'
285
+ }
286
+ }
287
+ });
288
+ compile.call(m, out.code, filename);
289
+ };
290
+
291
+ // @ts-expect-error TS2339
292
+ _module().default._extensions[extname] = (m, filename) => {
293
+ // @ts-expect-error TS2339
294
+ delete _module().default._extensions[extname];
295
+ // @ts-expect-error TS2339
296
+ _module().default._extensions['.js'](m, filename);
297
+ };
298
+ }
299
+ try {
300
+ // @ts-expect-error TS2339
301
+ m.load(filePath);
302
+ } catch (err) {
303
+ // @ts-expect-error TS2339
304
+ delete _module().default._cache[filePath];
305
+ throw err;
306
+ } finally {
307
+ _fs2().default.readFileSync = readFileSync;
308
+ // @ts-expect-error TS2540
309
+ _fs2().default.statSync = statSync;
310
+ }
311
+ return m.exports;
312
+ }
313
+ async resolve(id, from, options) {
314
+ let basedir = _path().default.dirname(from);
315
+ let key = basedir + ':' + id;
316
+ let resolved = cache.get(key);
317
+ if (!resolved) {
318
+ let [name] = (0, _utils().getModuleParts)(id);
319
+ try {
320
+ resolved = this.resolveInternal(id, from);
321
+ } catch (e) {
322
+ if (e.code !== 'MODULE_NOT_FOUND' || (options === null || options === void 0 ? void 0 : options.shouldAutoInstall) !== true || id.startsWith('.') // a local file, don't autoinstall
323
+ ) {
324
+ if (e.code === 'MODULE_NOT_FOUND' && (options === null || options === void 0 ? void 0 : options.shouldAutoInstall) !== true) {
325
+ let err = new (_diagnostic().default)({
326
+ diagnostic: {
327
+ message: (0, _diagnostic().escapeMarkdown)(e.message),
328
+ hints: ['Autoinstall is disabled, please install this package manually and restart Parcel.']
329
+ }
330
+ });
331
+ // @ts-expect-error TS2339
332
+ err.code = 'MODULE_NOT_FOUND';
333
+ throw err;
334
+ } else {
335
+ throw e;
336
+ }
337
+ }
338
+ let conflicts = await (0, _utils2.getConflictingLocalDependencies)(this.fs, name, from, this.projectRoot);
339
+ if (conflicts == null) {
340
+ this.invalidate(id, from);
341
+ await this.install([{
342
+ name,
343
+ range: options === null || options === void 0 ? void 0 : options.range
344
+ }], from, {
345
+ saveDev: (options === null || options === void 0 ? void 0 : options.saveDev) ?? true
346
+ });
347
+ return this.resolve(id, from, {
348
+ ...options,
349
+ shouldAutoInstall: false
350
+ });
351
+ }
352
+ throw new (_diagnostic().default)({
353
+ diagnostic: conflicts.fields.map(field => ({
354
+ message: (0, _diagnostic().md)`Could not find module "${name}", but it was listed in package.json. Run your package manager first.`,
355
+ origin: '@atlaspack/package-manager',
356
+ codeFrames: [{
357
+ filePath: conflicts.filePath,
358
+ language: 'json',
359
+ code: conflicts.json,
360
+ codeHighlights: (0, _diagnostic().generateJSONCodeHighlights)(conflicts.json, [{
361
+ key: `/${field}/${(0, _diagnostic().encodeJSONKeyComponent)(name)}`,
362
+ type: 'key',
363
+ message: 'Defined here, but not installed'
364
+ }])
365
+ }]
366
+ }))
367
+ });
368
+ }
369
+ let range = options === null || options === void 0 ? void 0 : options.range;
370
+ if (range != null) {
371
+ let pkg = resolved.pkg;
372
+ if (pkg == null || !_semver().default.satisfies(pkg.version, range)) {
373
+ let conflicts = await (0, _utils2.getConflictingLocalDependencies)(this.fs, name, from, this.projectRoot);
374
+ if (conflicts == null && (options === null || options === void 0 ? void 0 : options.shouldAutoInstall) === true) {
375
+ this.invalidate(id, from);
376
+ await this.install([{
377
+ name,
378
+ range
379
+ }], from);
380
+ return this.resolve(id, from, {
381
+ ...options,
382
+ shouldAutoInstall: false
383
+ });
384
+ } else if (conflicts != null) {
385
+ throw new (_diagnostic().default)({
386
+ diagnostic: {
387
+ message: (0, _diagnostic().md)`Could not find module "${name}" satisfying ${range}.`,
388
+ origin: '@atlaspack/package-manager',
389
+ codeFrames: [{
390
+ filePath: conflicts.filePath,
391
+ language: 'json',
392
+ code: conflicts.json,
393
+ codeHighlights: (0, _diagnostic().generateJSONCodeHighlights)(conflicts.json, conflicts.fields.map(field => ({
394
+ key: `/${field}/${(0, _diagnostic().encodeJSONKeyComponent)(name)}`,
395
+ type: 'key',
396
+ message: 'Found this conflicting local requirement.'
397
+ })))
398
+ }]
399
+ }
400
+ });
401
+ }
402
+ let version = pkg === null || pkg === void 0 ? void 0 : pkg.version;
403
+ let message = (0, _diagnostic().md)`Could not resolve package "${name}" that satisfies ${range}.`;
404
+ if (version != null) {
405
+ message += (0, _diagnostic().md)` Found ${version}.`;
406
+ }
407
+ throw new (_diagnostic().default)({
408
+ diagnostic: {
409
+ message,
410
+ hints: ['Looks like the incompatible version was installed transitively. Add this package as a direct dependency with a compatible version range.']
411
+ }
412
+ });
413
+ }
414
+ }
415
+ cache.set(key, resolved);
416
+ invalidationsCache.clear();
417
+
418
+ // Add the specifier as a child to the parent module.
419
+ // Don't do this if the specifier was an absolute path, as this was likely a dynamically resolved path
420
+ // (e.g. babel uses require() to load .babelrc.js configs and we don't want them to be added as children of babel itself).
421
+ if (!_path().default.isAbsolute(name)) {
422
+ let moduleChildren = children.get(from);
423
+ if (!moduleChildren) {
424
+ moduleChildren = new Set();
425
+ children.set(from, moduleChildren);
426
+ }
427
+ moduleChildren.add(name);
428
+ }
429
+ }
430
+ return resolved;
431
+ }
432
+ resolveSync(name, from) {
433
+ let basedir = _path().default.dirname(from);
434
+ let key = basedir + ':' + name;
435
+ let resolved = cache.get(key);
436
+ if (!resolved) {
437
+ resolved = this.resolveInternal(name, from);
438
+ cache.set(key, resolved);
439
+ invalidationsCache.clear();
440
+ if (!_path().default.isAbsolute(name)) {
441
+ let moduleChildren = children.get(from);
442
+ if (!moduleChildren) {
443
+ moduleChildren = new Set();
444
+ children.set(from, moduleChildren);
445
+ }
446
+ moduleChildren.add(name);
447
+ }
448
+ }
449
+ return resolved;
450
+ }
451
+ async install(modules, from, opts) {
452
+ await (0, _installPackage.installPackage)(this.fs, this, modules, from, this.projectRoot, {
453
+ packageInstaller: this.installer,
454
+ ...opts
455
+ });
456
+ }
457
+ getInvalidations(name, from) {
458
+ let basedir = _path().default.dirname(from);
459
+ let cacheKey = basedir + ':' + name;
460
+ let resolved = cache.get(cacheKey);
461
+ if (resolved && _path().default.isAbsolute(resolved.resolved)) {
462
+ let cached = invalidationsCache.get(resolved.resolved);
463
+ if (cached != null) {
464
+ return cached;
465
+ }
466
+ let res = {
467
+ invalidateOnFileCreate: [],
468
+ invalidateOnFileChange: new Set(),
469
+ invalidateOnStartup: false
470
+ };
471
+ let seen = new Set();
472
+ let addKey = (name, from) => {
473
+ let basedir = _path().default.dirname(from);
474
+ let key = basedir + ':' + name;
475
+ if (seen.has(key)) {
476
+ return;
477
+ }
478
+ seen.add(key);
479
+ let resolved = cache.get(key);
480
+ if (!resolved || !_path().default.isAbsolute(resolved.resolved)) {
481
+ return;
482
+ }
483
+
484
+ // @ts-expect-error TS2345
485
+ res.invalidateOnFileCreate.push(...resolved.invalidateOnFileCreate);
486
+ res.invalidateOnFileChange.add(resolved.resolved);
487
+ for (let file of resolved.invalidateOnFileChange) {
488
+ res.invalidateOnFileChange.add(file);
489
+ }
490
+ let moduleChildren = children.get(resolved.resolved);
491
+ if (moduleChildren) {
492
+ for (let specifier of moduleChildren) {
493
+ addKey(specifier, resolved.resolved);
494
+ }
495
+ }
496
+ };
497
+ addKey(name, from);
498
+
499
+ // If this is an ES module, we won't have any of the dependencies because import statements
500
+ // cannot be intercepted. Instead, ask the resolver to parse the file and recursively analyze the deps.
501
+ if (resolved.type === 2) {
502
+ let invalidations = this.resolver.getInvalidations(resolved.resolved);
503
+ // @ts-expect-error TS7006
504
+ invalidations.invalidateOnFileChange.forEach(i => res.invalidateOnFileChange.add(i));
505
+ // @ts-expect-error TS7006
506
+ invalidations.invalidateOnFileCreate.forEach(i =>
507
+ // @ts-expect-error TS2345
508
+ res.invalidateOnFileCreate.push(i));
509
+ res.invalidateOnStartup ||= invalidations.invalidateOnStartup;
510
+ if (res.invalidateOnStartup) {
511
+ _logger().default.warn({
512
+ message: (0, _diagnostic().md)`${_path().default.relative(this.projectRoot, resolved.resolved)} contains non-statically analyzable dependencies in its module graph. This causes Parcel to invalidate the cache on startup.`,
513
+ origin: '@atlaspack/package-manager'
514
+ });
515
+ }
516
+ }
517
+
518
+ // @ts-expect-error TS2345
519
+ invalidationsCache.set(resolved.resolved, res);
520
+ // @ts-expect-error TS2322
521
+ return res;
522
+ }
523
+ return {
524
+ invalidateOnFileCreate: [],
525
+ invalidateOnFileChange: new Set(),
526
+ invalidateOnStartup: false
527
+ };
528
+ }
529
+ invalidate(name, from) {
530
+ let seen = new Set();
531
+ let invalidate = (name, from) => {
532
+ let basedir = _path().default.dirname(from);
533
+ let key = basedir + ':' + name;
534
+ if (seen.has(key)) {
535
+ return;
536
+ }
537
+ seen.add(key);
538
+ let resolved = cache.get(key);
539
+ if (!resolved || !_path().default.isAbsolute(resolved.resolved)) {
540
+ return;
541
+ }
542
+
543
+ // During testing don't invalidate Atlaspack modules because
544
+ // this causes failures due to multiple instances of the same module
545
+ // existing simultaniously. This is fine when using babe;-register because
546
+ // it has an internal module cache that NodePacakageManager does not invalidate
547
+ // but fails when using compiled Atlaspack packages in integration tests
548
+ if (process.env.ATLASPACK_BUILD_ENV === 'test' && name.startsWith('@atlaspack/')) {
549
+ return;
550
+ }
551
+ invalidationsCache.delete(resolved.resolved);
552
+
553
+ // @ts-expect-error TS2339
554
+ let module = _module().default._cache[resolved.resolved];
555
+ if (module) {
556
+ // @ts-expect-error TS2339
557
+ delete _module().default._cache[resolved.resolved];
558
+ }
559
+ let moduleChildren = children.get(resolved.resolved);
560
+ if (moduleChildren) {
561
+ for (let specifier of moduleChildren) {
562
+ invalidate(specifier, resolved.resolved);
563
+ }
564
+ }
565
+ children.delete(resolved.resolved);
566
+ cache.delete(key);
567
+ };
568
+ invalidate(name, from);
569
+ this.resolver = this._createResolver();
570
+ }
571
+ resolveInternal(name, from) {
572
+ if (this.resolver == null) {
573
+ this.resolver = this._createResolver();
574
+ }
575
+ let res = this.resolver.resolve({
576
+ filename: name,
577
+ specifierType: 'commonjs',
578
+ parent: from,
579
+ packageConditions: (0, _nodejsConditions.getConditionsFromEnv)()
580
+ });
581
+
582
+ // Invalidate whenever the .pnp.js file changes.
583
+ // TODO: only when we actually resolve a node_modules package?
584
+ if (process.versions.pnp != null && res.invalidateOnFileChange) {
585
+ // @ts-expect-error TS2339
586
+ let pnp = _module().default.findPnpApi(_path().default.dirname(from));
587
+ res.invalidateOnFileChange.push(pnp.resolveToUnqualified('pnpapi', null));
588
+ }
589
+ if (res.error) {
590
+ let e = new Error(`Could not resolve module "${name}" from "${from}"`);
591
+ // @ts-expect-error TS2339
592
+ e.code = 'MODULE_NOT_FOUND';
593
+ throw e;
594
+ }
595
+ // @ts-expect-error TS7034
596
+ let getPkg;
597
+ switch (res.resolution.type) {
598
+ case 'Path':
599
+ getPkg = () => {
600
+ let pkgPath = this.fs.findAncestorFile(['package.json'], (0, _nullthrows().default)(res.resolution.value), this.projectRoot);
601
+ return pkgPath ? JSON.parse(this.fs.readFileSync(pkgPath, 'utf8')) : null;
602
+ };
603
+ // fallthrough
604
+ case 'Builtin':
605
+ return {
606
+ resolved: res.resolution.value,
607
+ invalidateOnFileChange: new Set(res.invalidateOnFileChange),
608
+ invalidateOnFileCreate: res.invalidateOnFileCreate,
609
+ type: res.moduleType,
610
+ get pkg() {
611
+ // @ts-expect-error TS7005
612
+ return getPkg();
613
+ }
614
+ };
615
+ default:
616
+ throw new Error('Unknown resolution type');
617
+ }
618
+ }
619
+ }
620
+ exports.NodePackageManager = NodePackageManager;
621
+ (0, _buildCache().registerSerializableClass)(`${_package.default.version}:NodePackageManager`, NodePackageManager);