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