Skip to content

Commit 119f186

Browse files
Fix circular dependencies on @parcel/types (#9610)
* Fix all circular dependencies * Fix circular dependencies between packages and `@parcel/types` * Use generic WorkerFarm type parameter to avoid having to move/duplicate the class * Don't do deep imports to types * Fix accidental changes to package.json * Revert turbo related changes to package.json * Fix typescript errors * Fix more typescript errors * Move packages into a types-internal pkg This allows for there to be no API changes from the circular dependencies fix. There is now a `@parcel/types` package with the same API as before and a new `@parcel/types-internal` package, which is consumed by `@parcel/workers` while avoiding it. * Fix typescript issues * Fix eslint errors * Fix typescript errors * Add back fs dependency on parcel/types * Add missing exports * Fix dependency cycle * Fix package-manager breaking changes * Fix import duplication in ts declaration file --------- Co-authored-by: Pedro Yamada <[email protected]>
1 parent 2a75be0 commit 119f186

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+310
-143
lines changed

gulpfile.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const IGNORED_PACKAGES = [
1111
'!packages/core/workers/test/integration/**',
1212
'!packages/core/test-utils/**',
1313
'!packages/core/types/**',
14+
'!packages/core/types-internal/**',
1415

1516
// These packages are bundled.
1617
'!packages/core/codeframe/**',

packages/core/cache/index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import type {Cache} from './lib/types';
33

44
export type {Cache} from './lib/types';
55
export const FSCache: {
6-
new (cacheDir: FilePath): Cache
6+
new (cacheDir: FilePath): Cache;
77
};
88

99
export const LMDBCache: {
10-
new (cacheDir: FilePath): Cache
10+
new (cacheDir: FilePath): Cache;
1111
};

packages/core/cache/src/types.js

+2-25
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,4 @@
11
// @flow
2-
import type {Readable} from 'stream';
2+
import type {Cache} from '@parcel/types';
33

4-
export interface Cache {
5-
ensure(): Promise<void>;
6-
has(key: string): Promise<boolean>;
7-
get<T>(key: string): Promise<?T>;
8-
set(key: string, value: mixed): Promise<void>;
9-
getStream(key: string): Readable;
10-
setStream(key: string, stream: Readable): Promise<void>;
11-
getBlob(key: string): Promise<Buffer>;
12-
setBlob(key: string, contents: Buffer | string): Promise<void>;
13-
hasLargeBlob(key: string): Promise<boolean>;
14-
getLargeBlob(key: string): Promise<Buffer>;
15-
setLargeBlob(
16-
key: string,
17-
contents: Buffer | string,
18-
options?: {|signal?: AbortSignal|},
19-
): Promise<void>;
20-
getBuffer(key: string): Promise<?Buffer>;
21-
/**
22-
* In a multi-threaded environment, where there are potentially multiple Cache
23-
* instances writing to the cache, ensure that this instance has the latest view
24-
* of the changes that may have been written to the cache in other threads.
25-
*/
26-
refresh(): void;
27-
}
4+
export type {Cache};

packages/core/core/index.d.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import type {InitialParcelOptions, BuildEvent, BuildSuccessEvent, AsyncSubscription} from '@parcel/types';
1+
import type {
2+
InitialParcelOptions,
3+
BuildEvent,
4+
BuildSuccessEvent,
5+
AsyncSubscription,
6+
} from '@parcel/types';
27
import type {FarmOptions} from '@parcel/workers';
38
import type WorkerFarm from '@parcel/workers';
49

@@ -7,9 +12,11 @@ export class Parcel {
712
run(): Promise<BuildSuccessEvent>;
813
watch(
914
cb?: (err: Error | null | undefined, buildEvent?: BuildEvent) => unknown,
10-
): Promise<AsyncSubscription>
15+
): Promise<AsyncSubscription>;
1116
}
1217

13-
export declare function createWorkerFarm(options?: Partial<FarmOptions>): WorkerFarm;
18+
export declare function createWorkerFarm(
19+
options?: Partial<FarmOptions>,
20+
): WorkerFarm;
1421

1522
export default Parcel;

packages/core/core/src/Parcel.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export default class Parcel {
6666
#farm /*: WorkerFarm*/;
6767
#initialized /*: boolean*/ = false;
6868
#disposable /*: Disposable */;
69-
#initialOptions /*: InitialParcelOptions*/;
69+
#initialOptions /*: InitialParcelOptions */;
7070
#reporterRunner /*: ReporterRunner*/;
7171
#resolvedOptions /*: ?ParcelOptions*/ = null;
7272
#optionsRef /*: SharedReference */;

packages/core/fs/index.d.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
import type {FileSystem} from './lib/types';
1+
import type {FileSystem} from '@parcel/types-internal';
22
import type WorkerFarm from '@parcel/workers';
33

4-
export * from './lib/types';
4+
export type {
5+
FileSystem,
6+
FileOptions,
7+
ReaddirOptions,
8+
Stats,
9+
Encoding,
10+
Dirent,
11+
} from '@parcel/types-internal';
512

613
export const NodeFS: {
714
new (): FileSystem;

packages/core/fs/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"includeNodeModules": {
2727
"@parcel/core": false,
2828
"@parcel/rust": false,
29-
"@parcel/types": false,
29+
"@parcel/types-internal": false,
3030
"@parcel/utils": false,
3131
"@parcel/watcher": false,
3232
"@parcel/workers": false
@@ -36,7 +36,7 @@
3636
"includeNodeModules": {
3737
"@parcel/core": false,
3838
"@parcel/rust": false,
39-
"@parcel/types": false,
39+
"@parcel/types-internal": false,
4040
"@parcel/utils": false,
4141
"@parcel/watcher": false,
4242
"@parcel/workers": false
@@ -49,7 +49,7 @@
4949
},
5050
"dependencies": {
5151
"@parcel/rust": "2.12.0",
52-
"@parcel/types": "2.12.0",
52+
"@parcel/types-internal": "2.12.0",
5353
"@parcel/utils": "2.12.0",
5454
"@parcel/watcher": "^2.0.7",
5555
"@parcel/workers": "2.12.0"

packages/core/fs/src/MemoryFS.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
// @flow
22

3-
import type {FileSystem, FileOptions, ReaddirOptions, Encoding} from './types';
4-
import type {FilePath} from '@parcel/types';
3+
import type {
4+
FilePath,
5+
FileSystem,
6+
FileOptions,
7+
ReaddirOptions,
8+
Encoding,
9+
} from '@parcel/types-internal';
510
import type {
611
Event,
712
Options as WatcherOptions,

packages/core/fs/src/NodeFS.browser.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @flow
2-
import type {FileSystem} from './types';
2+
import type {FileSystem} from '@parcel/types-internal';
33

44
// $FlowFixMe[prop-missing] handled by the throwing constructor
55
export class NodeFS implements FileSystem {

packages/core/fs/src/NodeFS.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
// @flow
22
import type {ReadStream, Stats} from 'fs';
33
import type {Writable} from 'stream';
4-
import type {FileOptions, FileSystem, Encoding} from './types';
5-
import type {FilePath} from '@parcel/types';
4+
import type {
5+
FilePath,
6+
Encoding,
7+
FileOptions,
8+
FileSystem,
9+
} from '@parcel/types-internal';
610
import type {
711
Event,
812
Options as WatcherOptions,

packages/core/fs/src/OverlayFS.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import type {Readable, Writable} from 'stream';
44
import type {
5+
FilePath,
56
Encoding,
67
FileOptions,
78
FileSystem,
89
ReaddirOptions,
9-
Stats,
10-
} from './types';
11-
import type {FilePath} from '@parcel/types';
10+
FileStats,
11+
} from '@parcel/types-internal';
1212
import type {
1313
Event,
1414
Options as WatcherOptions,
@@ -168,7 +168,7 @@ export class OverlayFS implements FileSystem {
168168
}
169169

170170
// eslint-disable-next-line require-await
171-
async stat(filePath: FilePath): Promise<Stats> {
171+
async stat(filePath: FilePath): Promise<FileStats> {
172172
return this.statSync(filePath);
173173
}
174174

@@ -289,7 +289,7 @@ export class OverlayFS implements FileSystem {
289289
}
290290
}
291291

292-
statSync(filePath: FilePath): Stats {
292+
statSync(filePath: FilePath): FileStats {
293293
filePath = this._normalizePath(filePath);
294294
try {
295295
return this.writable.statSync(filePath);

packages/core/fs/src/find.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// @flow
2-
import type {FilePath} from '@parcel/types';
3-
import type {FileSystem} from './types';
2+
import type {FilePath, FileSystem} from '@parcel/types-internal';
43
import path from 'path';
54

65
export function findNodeModule(

packages/core/fs/src/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// @flow strict-local
2-
import type {FileSystem} from './types';
3-
import type {FilePath} from '@parcel/types';
2+
import type {FilePath, FileSystem, FileOptions} from '@parcel/types-internal';
43
import type {Readable, Writable} from 'stream';
54

65
import path from 'path';
76
import stream from 'stream';
87
import {promisify} from 'util';
98

10-
export type * from './types';
119
export * from './NodeFS';
1210
export * from './MemoryFS';
1311
export * from './OverlayFS';
1412

13+
export type {FileSystem, FileOptions};
14+
1515
const pipeline: (Readable, Writable) => Promise<void> = promisify(
1616
stream.pipeline,
1717
);

packages/core/package-manager/index.d.ts

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1-
import type {FilePath} from '@parcel/types';
1+
import type {
2+
FilePath,
3+
PackageInstaller,
4+
PackageManager,
5+
PackageManagerResolveResult,
6+
} from '@parcel/types';
27
import type {FileSystem} from '@parcel/fs';
3-
import type {PackageInstaller, PackageManager} from './lib/types';
48

5-
export * from './lib/types';
9+
export type {PackageManagerResolveResult};
10+
export type {PackageManagerResolveResult as ResolveResult};
11+
12+
export type {
13+
PackageManager,
14+
InstallOptions,
15+
InstallerOptions,
16+
PackageInstaller,
17+
Invalidations,
18+
ModuleRequest,
19+
} from '@parcel/types';
620

721
export const Npm: {
822
new (): PackageInstaller;
@@ -18,5 +32,9 @@ export const MockPackageInstaller: {
1832
new (): PackageInstaller;
1933
};
2034
export const NodePackageManager: {
21-
new (fs: FileSystem, projectRoot: FilePath, installer?: PackageInstaller): PackageManager;
35+
new (
36+
fs: FileSystem,
37+
projectRoot: FilePath,
38+
installer?: PackageInstaller,
39+
): PackageManager;
2240
};

packages/core/package-manager/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"node": ">= 12.0.0"
2222
},
2323
"scripts": {
24-
"build-ts": "mkdir -p lib && flow-to-ts src/types.js > lib/types.d.ts",
24+
"build-ts": "mkdir -p lib && flow-to-ts src/index.js > lib/index.d.ts",
2525
"check-ts": "tsc --noEmit index.d.ts",
2626
"test": "mocha test"
2727
},

packages/core/package-manager/src/MockPackageInstaller.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// @flow
22

3-
import type {ModuleRequest, PackageInstaller, InstallerOptions} from './types';
3+
import type {
4+
ModuleRequest,
5+
PackageInstaller,
6+
InstallerOptions,
7+
} from '@parcel/types';
48
import type {FileSystem} from '@parcel/fs';
59
import type {FilePath} from '@parcel/types';
610

packages/core/package-manager/src/NodePackageManager.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import type {
77
PackageInstaller,
88
InstallOptions,
99
Invalidations,
10-
} from './types';
11-
import type {ResolveResult} from './types';
10+
PackageManagerResolveResult,
11+
} from '@parcel/types';
1212

1313
import {registerSerializableClass} from '@parcel/core';
1414
import ThrowableDiagnostic, {
@@ -47,7 +47,7 @@ const NODE_MODULES = `${path.sep}node_modules${path.sep}`;
4747

4848
// There can be more than one instance of NodePackageManager, but node has only a single module cache.
4949
// Therefore, the resolution cache and the map of parent to child modules should also be global.
50-
const cache = new Map<DependencySpecifier, ResolveResult>();
50+
const cache = new Map<DependencySpecifier, PackageManagerResolveResult>();
5151
const children = new Map<FilePath, Set<DependencySpecifier>>();
5252
const invalidationsCache = new Map<string, Invalidations>();
5353

@@ -259,7 +259,7 @@ export class NodePackageManager implements PackageManager {
259259
shouldAutoInstall?: boolean,
260260
saveDev?: boolean,
261261
|},
262-
): Promise<ResolveResult> {
262+
): Promise<PackageManagerResolveResult> {
263263
let basedir = path.dirname(from);
264264
let key = basedir + ':' + id;
265265
let resolved = cache.get(key);
@@ -413,7 +413,10 @@ export class NodePackageManager implements PackageManager {
413413
return resolved;
414414
}
415415
416-
resolveSync(name: DependencySpecifier, from: FilePath): ResolveResult {
416+
resolveSync(
417+
name: DependencySpecifier,
418+
from: FilePath,
419+
): PackageManagerResolveResult {
417420
let basedir = path.dirname(from);
418421
let key = basedir + ':' + name;
419422
let resolved = cache.get(key);
@@ -568,7 +571,7 @@ export class NodePackageManager implements PackageManager {
568571
this.resolver = this._createResolver();
569572
}
570573
571-
resolveInternal(name: string, from: string): ResolveResult {
574+
resolveInternal(name: string, from: string): PackageManagerResolveResult {
572575
if (this.resolver == null) {
573576
this.resolver = this._createResolver();
574577
}

packages/core/package-manager/src/Npm.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @flow strict-local
22

3-
import type {PackageInstaller, InstallerOptions} from './types';
3+
import type {PackageInstaller, InstallerOptions} from '@parcel/types';
44

55
import path from 'path';
66
import spawn from 'cross-spawn';

packages/core/package-manager/src/Pnpm.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @flow strict-local
22

3-
import type {PackageInstaller, InstallerOptions} from './types';
3+
import type {PackageInstaller, InstallerOptions} from '@parcel/types';
44

55
import path from 'path';
66
import fs from 'fs';

packages/core/package-manager/src/Yarn.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @flow strict-local
22

3-
import type {PackageInstaller, InstallerOptions} from './types';
3+
import type {PackageInstaller, InstallerOptions} from '@parcel/types';
44

55
import commandExists from 'command-exists';
66
import spawn from 'cross-spawn';
+12-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
// @flow
2-
export type * from './types';
2+
3+
import type {PackageManagerResolveResult} from '@parcel/types';
4+
5+
export type {
6+
PackageManager,
7+
Invalidations,
8+
PackageInstaller,
9+
ModuleRequest,
10+
} from '@parcel/types';
311
export * from './Npm';
412
export * from './Pnpm';
513
export * from './Yarn';
614
export * from './MockPackageInstaller';
715
export * from './NodePackageManager';
816
export {_addToInstallQueue} from './installPackage';
17+
18+
export type {PackageManagerResolveResult};
19+
export type {PackageManagerResolveResult as ResolveResult};

packages/core/package-manager/src/installPackage.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
PackageManager,
77
PackageInstaller,
88
InstallOptions,
9-
} from './types';
9+
} from '@parcel/types';
1010
import type {FileSystem} from '@parcel/fs';
1111

1212
import invariant from 'assert';

packages/core/package-manager/src/utils.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// @flow strict-local
22

3-
import type {ModuleRequest} from './types';
4-
import type {FilePath} from '@parcel/types';
3+
import type {FilePath, ModuleRequest} from '@parcel/types';
54
import type {FileSystem} from '@parcel/fs';
65

76
import invariant from 'assert';

0 commit comments

Comments
 (0)