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