From 119f18671fb315ca246de8a5d2d2c4d50563e200 Mon Sep 17 00:00:00 2001 From: Pedro Tacla Yamada Date: Thu, 11 Apr 2024 09:50:28 +1000 Subject: [PATCH] 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 --- gulpfile.js | 1 + packages/core/cache/index.d.ts | 4 +- packages/core/cache/src/types.js | 27 +---- packages/core/core/index.d.ts | 13 ++- packages/core/core/src/Parcel.js | 2 +- packages/core/fs/index.d.ts | 11 +- packages/core/fs/package.json | 6 +- packages/core/fs/src/MemoryFS.js | 9 +- packages/core/fs/src/NodeFS.browser.js | 2 +- packages/core/fs/src/NodeFS.js | 8 +- packages/core/fs/src/OverlayFS.js | 10 +- packages/core/fs/src/find.js | 3 +- packages/core/fs/src/index.js | 6 +- packages/core/package-manager/index.d.ts | 26 ++++- packages/core/package-manager/package.json | 2 +- .../src/MockPackageInstaller.js | 6 +- .../package-manager/src/NodePackageManager.js | 15 ++- packages/core/package-manager/src/Npm.js | 2 +- packages/core/package-manager/src/Pnpm.js | 2 +- packages/core/package-manager/src/Yarn.js | 2 +- packages/core/package-manager/src/index.js | 13 ++- .../package-manager/src/installPackage.js | 2 +- packages/core/package-manager/src/utils.js | 3 +- packages/core/profiler/package.json | 3 +- packages/core/profiler/src/types.js | 4 +- packages/core/types-internal/package.json | 22 ++++ .../scripts}/build-ts.js | 6 +- .../core/types-internal/scripts/build-ts.sh | 11 ++ packages/core/types-internal/src/Cache.js | 28 +++++ .../types-internal/src/DependencySpecifier.js | 4 + .../src/FileCreateInvalidation.js | 22 ++++ packages/core/types-internal/src/FilePath.js | 3 + .../src/FileSystem.js} | 2 +- packages/core/types-internal/src/Glob.js | 3 + .../src/PackageManager.js} | 19 ++-- .../core/types-internal/src/PackageName.js | 3 + .../core/types-internal/src/SemverRange.js | 3 + .../{types => types-internal/src}/index.js | 105 +++++++++++------- .../{types => types-internal/src}/unsafe.js | 0 packages/core/types/package.json | 12 +- packages/core/types/src/index.js | 8 ++ packages/core/utils/src/alternatives.js | 2 +- packages/core/workers/package.json | 2 +- packages/core/workers/src/Worker.js | 2 +- packages/core/workers/src/WorkerFarm.js | 2 +- packages/core/workers/src/child.js | 2 +- packages/core/workers/src/index.js | 2 +- packages/core/workers/src/types.js | 2 +- packages/dev/esm-fuzzer/package.json | 2 + packages/dev/repl/src/parcel/ParcelWorker.js | 3 +- packages/validators/eslint/package.json | 1 + 51 files changed, 310 insertions(+), 143 deletions(-) create mode 100644 packages/core/types-internal/package.json rename packages/core/{types => types-internal/scripts}/build-ts.js (61%) create mode 100755 packages/core/types-internal/scripts/build-ts.sh create mode 100644 packages/core/types-internal/src/Cache.js create mode 100644 packages/core/types-internal/src/DependencySpecifier.js create mode 100644 packages/core/types-internal/src/FileCreateInvalidation.js create mode 100644 packages/core/types-internal/src/FilePath.js rename packages/core/{fs/src/types.js => types-internal/src/FileSystem.js} (98%) create mode 100644 packages/core/types-internal/src/Glob.js rename packages/core/{package-manager/src/types.js => types-internal/src/PackageManager.js} (78%) create mode 100644 packages/core/types-internal/src/PackageName.js create mode 100644 packages/core/types-internal/src/SemverRange.js rename packages/core/{types => types-internal/src}/index.js (97%) rename packages/core/{types => types-internal/src}/unsafe.js (100%) create mode 100644 packages/core/types/src/index.js diff --git a/gulpfile.js b/gulpfile.js index 76390719711..dd6ba06c6ef 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -11,6 +11,7 @@ const IGNORED_PACKAGES = [ '!packages/core/workers/test/integration/**', '!packages/core/test-utils/**', '!packages/core/types/**', + '!packages/core/types-internal/**', // These packages are bundled. '!packages/core/codeframe/**', diff --git a/packages/core/cache/index.d.ts b/packages/core/cache/index.d.ts index eb06878b4fd..bd738460e46 100644 --- a/packages/core/cache/index.d.ts +++ b/packages/core/cache/index.d.ts @@ -3,9 +3,9 @@ import type {Cache} from './lib/types'; export type {Cache} from './lib/types'; export const FSCache: { - new (cacheDir: FilePath): Cache + new (cacheDir: FilePath): Cache; }; export const LMDBCache: { - new (cacheDir: FilePath): Cache + new (cacheDir: FilePath): Cache; }; diff --git a/packages/core/cache/src/types.js b/packages/core/cache/src/types.js index c9dab9535b6..472be4c88f7 100644 --- a/packages/core/cache/src/types.js +++ b/packages/core/cache/src/types.js @@ -1,27 +1,4 @@ // @flow -import type {Readable} from 'stream'; +import type {Cache} from '@parcel/types'; -export interface Cache { - ensure(): Promise; - has(key: string): Promise; - get(key: string): Promise; - set(key: string, value: mixed): Promise; - getStream(key: string): Readable; - setStream(key: string, stream: Readable): Promise; - getBlob(key: string): Promise; - setBlob(key: string, contents: Buffer | string): Promise; - hasLargeBlob(key: string): Promise; - getLargeBlob(key: string): Promise; - setLargeBlob( - key: string, - contents: Buffer | string, - options?: {|signal?: AbortSignal|}, - ): Promise; - getBuffer(key: string): Promise; - /** - * In a multi-threaded environment, where there are potentially multiple Cache - * instances writing to the cache, ensure that this instance has the latest view - * of the changes that may have been written to the cache in other threads. - */ - refresh(): void; -} +export type {Cache}; diff --git a/packages/core/core/index.d.ts b/packages/core/core/index.d.ts index ae736883951..a1d56432acc 100644 --- a/packages/core/core/index.d.ts +++ b/packages/core/core/index.d.ts @@ -1,4 +1,9 @@ -import type {InitialParcelOptions, BuildEvent, BuildSuccessEvent, AsyncSubscription} from '@parcel/types'; +import type { + InitialParcelOptions, + BuildEvent, + BuildSuccessEvent, + AsyncSubscription, +} from '@parcel/types'; import type {FarmOptions} from '@parcel/workers'; import type WorkerFarm from '@parcel/workers'; @@ -7,9 +12,11 @@ export class Parcel { run(): Promise; watch( cb?: (err: Error | null | undefined, buildEvent?: BuildEvent) => unknown, - ): Promise + ): Promise; } -export declare function createWorkerFarm(options?: Partial): WorkerFarm; +export declare function createWorkerFarm( + options?: Partial, +): WorkerFarm; export default Parcel; diff --git a/packages/core/core/src/Parcel.js b/packages/core/core/src/Parcel.js index df14f4fc8ce..55325000a5b 100644 --- a/packages/core/core/src/Parcel.js +++ b/packages/core/core/src/Parcel.js @@ -66,7 +66,7 @@ export default class Parcel { #farm /*: WorkerFarm*/; #initialized /*: boolean*/ = false; #disposable /*: Disposable */; - #initialOptions /*: InitialParcelOptions*/; + #initialOptions /*: InitialParcelOptions */; #reporterRunner /*: ReporterRunner*/; #resolvedOptions /*: ?ParcelOptions*/ = null; #optionsRef /*: SharedReference */; diff --git a/packages/core/fs/index.d.ts b/packages/core/fs/index.d.ts index afed1f43db7..ea5c725cb75 100644 --- a/packages/core/fs/index.d.ts +++ b/packages/core/fs/index.d.ts @@ -1,7 +1,14 @@ -import type {FileSystem} from './lib/types'; +import type {FileSystem} from '@parcel/types-internal'; import type WorkerFarm from '@parcel/workers'; -export * from './lib/types'; +export type { + FileSystem, + FileOptions, + ReaddirOptions, + Stats, + Encoding, + Dirent, +} from '@parcel/types-internal'; export const NodeFS: { new (): FileSystem; diff --git a/packages/core/fs/package.json b/packages/core/fs/package.json index f3ee5976a9d..5e0b665e0ce 100644 --- a/packages/core/fs/package.json +++ b/packages/core/fs/package.json @@ -26,7 +26,7 @@ "includeNodeModules": { "@parcel/core": false, "@parcel/rust": false, - "@parcel/types": false, + "@parcel/types-internal": false, "@parcel/utils": false, "@parcel/watcher": false, "@parcel/workers": false @@ -36,7 +36,7 @@ "includeNodeModules": { "@parcel/core": false, "@parcel/rust": false, - "@parcel/types": false, + "@parcel/types-internal": false, "@parcel/utils": false, "@parcel/watcher": false, "@parcel/workers": false @@ -49,7 +49,7 @@ }, "dependencies": { "@parcel/rust": "2.12.0", - "@parcel/types": "2.12.0", + "@parcel/types-internal": "2.12.0", "@parcel/utils": "2.12.0", "@parcel/watcher": "^2.0.7", "@parcel/workers": "2.12.0" diff --git a/packages/core/fs/src/MemoryFS.js b/packages/core/fs/src/MemoryFS.js index 554b5b6afa9..7a77bdc4c90 100644 --- a/packages/core/fs/src/MemoryFS.js +++ b/packages/core/fs/src/MemoryFS.js @@ -1,7 +1,12 @@ // @flow -import type {FileSystem, FileOptions, ReaddirOptions, Encoding} from './types'; -import type {FilePath} from '@parcel/types'; +import type { + FilePath, + FileSystem, + FileOptions, + ReaddirOptions, + Encoding, +} from '@parcel/types-internal'; import type { Event, Options as WatcherOptions, diff --git a/packages/core/fs/src/NodeFS.browser.js b/packages/core/fs/src/NodeFS.browser.js index 43454dc61f8..850eb8548aa 100644 --- a/packages/core/fs/src/NodeFS.browser.js +++ b/packages/core/fs/src/NodeFS.browser.js @@ -1,5 +1,5 @@ // @flow -import type {FileSystem} from './types'; +import type {FileSystem} from '@parcel/types-internal'; // $FlowFixMe[prop-missing] handled by the throwing constructor export class NodeFS implements FileSystem { diff --git a/packages/core/fs/src/NodeFS.js b/packages/core/fs/src/NodeFS.js index 6554fc1ce54..55e776ae668 100644 --- a/packages/core/fs/src/NodeFS.js +++ b/packages/core/fs/src/NodeFS.js @@ -1,8 +1,12 @@ // @flow import type {ReadStream, Stats} from 'fs'; import type {Writable} from 'stream'; -import type {FileOptions, FileSystem, Encoding} from './types'; -import type {FilePath} from '@parcel/types'; +import type { + FilePath, + Encoding, + FileOptions, + FileSystem, +} from '@parcel/types-internal'; import type { Event, Options as WatcherOptions, diff --git a/packages/core/fs/src/OverlayFS.js b/packages/core/fs/src/OverlayFS.js index 47b8d06aea3..073d02a9d55 100644 --- a/packages/core/fs/src/OverlayFS.js +++ b/packages/core/fs/src/OverlayFS.js @@ -2,13 +2,13 @@ import type {Readable, Writable} from 'stream'; import type { + FilePath, Encoding, FileOptions, FileSystem, ReaddirOptions, - Stats, -} from './types'; -import type {FilePath} from '@parcel/types'; + FileStats, +} from '@parcel/types-internal'; import type { Event, Options as WatcherOptions, @@ -168,7 +168,7 @@ export class OverlayFS implements FileSystem { } // eslint-disable-next-line require-await - async stat(filePath: FilePath): Promise { + async stat(filePath: FilePath): Promise { return this.statSync(filePath); } @@ -289,7 +289,7 @@ export class OverlayFS implements FileSystem { } } - statSync(filePath: FilePath): Stats { + statSync(filePath: FilePath): FileStats { filePath = this._normalizePath(filePath); try { return this.writable.statSync(filePath); diff --git a/packages/core/fs/src/find.js b/packages/core/fs/src/find.js index 60a1abfff3d..f086eb413ec 100644 --- a/packages/core/fs/src/find.js +++ b/packages/core/fs/src/find.js @@ -1,6 +1,5 @@ // @flow -import type {FilePath} from '@parcel/types'; -import type {FileSystem} from './types'; +import type {FilePath, FileSystem} from '@parcel/types-internal'; import path from 'path'; export function findNodeModule( diff --git a/packages/core/fs/src/index.js b/packages/core/fs/src/index.js index fc5c83c8e0d..0279b9c3f03 100644 --- a/packages/core/fs/src/index.js +++ b/packages/core/fs/src/index.js @@ -1,17 +1,17 @@ // @flow strict-local -import type {FileSystem} from './types'; -import type {FilePath} from '@parcel/types'; +import type {FilePath, FileSystem, FileOptions} from '@parcel/types-internal'; import type {Readable, Writable} from 'stream'; import path from 'path'; import stream from 'stream'; import {promisify} from 'util'; -export type * from './types'; export * from './NodeFS'; export * from './MemoryFS'; export * from './OverlayFS'; +export type {FileSystem, FileOptions}; + const pipeline: (Readable, Writable) => Promise = promisify( stream.pipeline, ); diff --git a/packages/core/package-manager/index.d.ts b/packages/core/package-manager/index.d.ts index 940a66e68ea..7f860a0f391 100644 --- a/packages/core/package-manager/index.d.ts +++ b/packages/core/package-manager/index.d.ts @@ -1,8 +1,22 @@ -import type {FilePath} from '@parcel/types'; +import type { + FilePath, + PackageInstaller, + PackageManager, + PackageManagerResolveResult, +} from '@parcel/types'; import type {FileSystem} from '@parcel/fs'; -import type {PackageInstaller, PackageManager} from './lib/types'; -export * from './lib/types'; +export type {PackageManagerResolveResult}; +export type {PackageManagerResolveResult as ResolveResult}; + +export type { + PackageManager, + InstallOptions, + InstallerOptions, + PackageInstaller, + Invalidations, + ModuleRequest, +} from '@parcel/types'; export const Npm: { new (): PackageInstaller; @@ -18,5 +32,9 @@ export const MockPackageInstaller: { new (): PackageInstaller; }; export const NodePackageManager: { - new (fs: FileSystem, projectRoot: FilePath, installer?: PackageInstaller): PackageManager; + new ( + fs: FileSystem, + projectRoot: FilePath, + installer?: PackageInstaller, + ): PackageManager; }; diff --git a/packages/core/package-manager/package.json b/packages/core/package-manager/package.json index 954174e69bb..2c77526dde0 100644 --- a/packages/core/package-manager/package.json +++ b/packages/core/package-manager/package.json @@ -21,7 +21,7 @@ "node": ">= 12.0.0" }, "scripts": { - "build-ts": "mkdir -p lib && flow-to-ts src/types.js > lib/types.d.ts", + "build-ts": "mkdir -p lib && flow-to-ts src/index.js > lib/index.d.ts", "check-ts": "tsc --noEmit index.d.ts", "test": "mocha test" }, diff --git a/packages/core/package-manager/src/MockPackageInstaller.js b/packages/core/package-manager/src/MockPackageInstaller.js index 50dd6e8899b..520e2679a13 100644 --- a/packages/core/package-manager/src/MockPackageInstaller.js +++ b/packages/core/package-manager/src/MockPackageInstaller.js @@ -1,6 +1,10 @@ // @flow -import type {ModuleRequest, PackageInstaller, InstallerOptions} from './types'; +import type { + ModuleRequest, + PackageInstaller, + InstallerOptions, +} from '@parcel/types'; import type {FileSystem} from '@parcel/fs'; import type {FilePath} from '@parcel/types'; diff --git a/packages/core/package-manager/src/NodePackageManager.js b/packages/core/package-manager/src/NodePackageManager.js index 0b0f67fa5ca..98f10b7b8e2 100644 --- a/packages/core/package-manager/src/NodePackageManager.js +++ b/packages/core/package-manager/src/NodePackageManager.js @@ -7,8 +7,8 @@ import type { PackageInstaller, InstallOptions, Invalidations, -} from './types'; -import type {ResolveResult} from './types'; + PackageManagerResolveResult, +} from '@parcel/types'; import {registerSerializableClass} from '@parcel/core'; import ThrowableDiagnostic, { @@ -47,7 +47,7 @@ const NODE_MODULES = `${path.sep}node_modules${path.sep}`; // There can be more than one instance of NodePackageManager, but node has only a single module cache. // Therefore, the resolution cache and the map of parent to child modules should also be global. -const cache = new Map(); +const cache = new Map(); const children = new Map>(); const invalidationsCache = new Map(); @@ -259,7 +259,7 @@ export class NodePackageManager implements PackageManager { shouldAutoInstall?: boolean, saveDev?: boolean, |}, - ): Promise { + ): Promise { let basedir = path.dirname(from); let key = basedir + ':' + id; let resolved = cache.get(key); @@ -413,7 +413,10 @@ export class NodePackageManager implements PackageManager { return resolved; } - resolveSync(name: DependencySpecifier, from: FilePath): ResolveResult { + resolveSync( + name: DependencySpecifier, + from: FilePath, + ): PackageManagerResolveResult { let basedir = path.dirname(from); let key = basedir + ':' + name; let resolved = cache.get(key); @@ -568,7 +571,7 @@ export class NodePackageManager implements PackageManager { this.resolver = this._createResolver(); } - resolveInternal(name: string, from: string): ResolveResult { + resolveInternal(name: string, from: string): PackageManagerResolveResult { if (this.resolver == null) { this.resolver = this._createResolver(); } diff --git a/packages/core/package-manager/src/Npm.js b/packages/core/package-manager/src/Npm.js index 6391ce5f82c..13a8640b764 100644 --- a/packages/core/package-manager/src/Npm.js +++ b/packages/core/package-manager/src/Npm.js @@ -1,6 +1,6 @@ // @flow strict-local -import type {PackageInstaller, InstallerOptions} from './types'; +import type {PackageInstaller, InstallerOptions} from '@parcel/types'; import path from 'path'; import spawn from 'cross-spawn'; diff --git a/packages/core/package-manager/src/Pnpm.js b/packages/core/package-manager/src/Pnpm.js index 932983feaee..62ac042629b 100644 --- a/packages/core/package-manager/src/Pnpm.js +++ b/packages/core/package-manager/src/Pnpm.js @@ -1,6 +1,6 @@ // @flow strict-local -import type {PackageInstaller, InstallerOptions} from './types'; +import type {PackageInstaller, InstallerOptions} from '@parcel/types'; import path from 'path'; import fs from 'fs'; diff --git a/packages/core/package-manager/src/Yarn.js b/packages/core/package-manager/src/Yarn.js index b541ecb3ca1..578da0bf71c 100644 --- a/packages/core/package-manager/src/Yarn.js +++ b/packages/core/package-manager/src/Yarn.js @@ -1,6 +1,6 @@ // @flow strict-local -import type {PackageInstaller, InstallerOptions} from './types'; +import type {PackageInstaller, InstallerOptions} from '@parcel/types'; import commandExists from 'command-exists'; import spawn from 'cross-spawn'; diff --git a/packages/core/package-manager/src/index.js b/packages/core/package-manager/src/index.js index c7c0f96d062..dc916d499d2 100644 --- a/packages/core/package-manager/src/index.js +++ b/packages/core/package-manager/src/index.js @@ -1,8 +1,19 @@ // @flow -export type * from './types'; + +import type {PackageManagerResolveResult} from '@parcel/types'; + +export type { + PackageManager, + Invalidations, + PackageInstaller, + ModuleRequest, +} from '@parcel/types'; export * from './Npm'; export * from './Pnpm'; export * from './Yarn'; export * from './MockPackageInstaller'; export * from './NodePackageManager'; export {_addToInstallQueue} from './installPackage'; + +export type {PackageManagerResolveResult}; +export type {PackageManagerResolveResult as ResolveResult}; diff --git a/packages/core/package-manager/src/installPackage.js b/packages/core/package-manager/src/installPackage.js index 1c1f28317dd..deac4c38a88 100644 --- a/packages/core/package-manager/src/installPackage.js +++ b/packages/core/package-manager/src/installPackage.js @@ -6,7 +6,7 @@ import type { PackageManager, PackageInstaller, InstallOptions, -} from './types'; +} from '@parcel/types'; import type {FileSystem} from '@parcel/fs'; import invariant from 'assert'; diff --git a/packages/core/package-manager/src/utils.js b/packages/core/package-manager/src/utils.js index acdecd07c4d..ae51142adcb 100644 --- a/packages/core/package-manager/src/utils.js +++ b/packages/core/package-manager/src/utils.js @@ -1,7 +1,6 @@ // @flow strict-local -import type {ModuleRequest} from './types'; -import type {FilePath} from '@parcel/types'; +import type {FilePath, ModuleRequest} from '@parcel/types'; import type {FileSystem} from '@parcel/fs'; import invariant from 'assert'; diff --git a/packages/core/profiler/package.json b/packages/core/profiler/package.json index bda1146e2b1..a4eea622e4d 100644 --- a/packages/core/profiler/package.json +++ b/packages/core/profiler/package.json @@ -20,12 +20,13 @@ "node": ">= 12.0.0" }, "scripts": { - "build-ts": "flow-to-ts src/*.js --write && tsc --emitDeclarationOnly --declaration --esModuleInterop --target es2015 --moduleResolution node16 --module node16 src/*.ts && mkdir -p lib && mv src/*.d.ts lib/. && rm src/*.ts && node build-ts.js", + "build-ts": "flow-to-ts src/*.js --write && rm -f ./src/*.d.ts && tsc --emitDeclarationOnly --declaration --esModuleInterop --target es2015 --moduleResolution node16 --module node16 src/*.ts && mkdir -p lib && mv src/*.d.ts lib/. && rm src/*.ts && node build-ts.js", "check-ts": "tsc --noEmit lib/index.d.ts" }, "dependencies": { "@parcel/diagnostic": "2.12.0", "@parcel/events": "2.12.0", + "@parcel/types-internal": "2.12.0", "chrome-trace-event": "^1.0.2" } } diff --git a/packages/core/profiler/src/types.js b/packages/core/profiler/src/types.js index 031fdbeda84..e595ca7ed36 100644 --- a/packages/core/profiler/src/types.js +++ b/packages/core/profiler/src/types.js @@ -1,8 +1,6 @@ // @flow -export interface TraceMeasurement { - end(): void; -} +export type {TraceMeasurement} from '@parcel/types-internal'; export type TraceMeasurementData = {| +categories: string[], diff --git a/packages/core/types-internal/package.json b/packages/core/types-internal/package.json new file mode 100644 index 00000000000..601f8ae7f4f --- /dev/null +++ b/packages/core/types-internal/package.json @@ -0,0 +1,22 @@ +{ + "name": "@parcel/types-internal", + "version": "2.12.0", + "license": "MIT", + "main": "src/index.js", + "types": "lib/index.d.ts", + "repository": { + "type": "git", + "url": "https://github.com/parcel-bundler/parcel.git" + }, + "publishConfig": {}, + "scripts": { + "build-ts": "./scripts/build-ts.sh", + "check-ts": "tsc --noEmit lib/index.d.ts" + }, + "dependencies": { + "@parcel/diagnostic": "2.12.0", + "@parcel/feature-flags": "2.12.0", + "@parcel/source-map": "^2.1.1", + "utility-types": "^3.10.0" + } +} diff --git a/packages/core/types/build-ts.js b/packages/core/types-internal/scripts/build-ts.js similarity index 61% rename from packages/core/types/build-ts.js rename to packages/core/types-internal/scripts/build-ts.js index 5ba30a67cbf..0244384cf47 100644 --- a/packages/core/types/build-ts.js +++ b/packages/core/types-internal/scripts/build-ts.js @@ -1,7 +1,9 @@ const fs = require('fs'); +const path = require('path'); -let contents = fs.readFileSync(__dirname + '/lib/index.d.ts', 'utf8'); +const typesPath = path.join(__dirname, '../lib/index.d.ts'); +let contents = fs.readFileSync(typesPath, 'utf8'); // Some fixups of flow-to-ts output contents = contents.replace( 'Record', @@ -10,4 +12,4 @@ contents = contents.replace( contents = contents.replace(/\$ReadOnlyMap/g, 'ReadonlyMap'); contents = contents.replace(/\$ReadOnlySet/g, 'ReadonlySet'); -fs.writeFileSync(__dirname + '/lib/index.d.ts', contents); +fs.writeFileSync(typesPath, contents); diff --git a/packages/core/types-internal/scripts/build-ts.sh b/packages/core/types-internal/scripts/build-ts.sh new file mode 100755 index 00000000000..9d58001a676 --- /dev/null +++ b/packages/core/types-internal/scripts/build-ts.sh @@ -0,0 +1,11 @@ +#!/bin/bash +set -x +set -e + +mkdir -p lib + +for file in src/*.js; do + npx flow-to-ts $file > lib/$(basename $file .js).d.ts +done + +node ./scripts/build-ts.js diff --git a/packages/core/types-internal/src/Cache.js b/packages/core/types-internal/src/Cache.js new file mode 100644 index 00000000000..cb4eb862fba --- /dev/null +++ b/packages/core/types-internal/src/Cache.js @@ -0,0 +1,28 @@ +// @flow strict-local + +import type {Readable} from 'stream'; + +export interface Cache { + ensure(): Promise; + has(key: string): Promise; + get(key: string): Promise; + set(key: string, value: mixed): Promise; + getStream(key: string): Readable; + setStream(key: string, stream: Readable): Promise; + getBlob(key: string): Promise; + setBlob(key: string, contents: Buffer | string): Promise; + hasLargeBlob(key: string): Promise; + getLargeBlob(key: string): Promise; + setLargeBlob( + key: string, + contents: Buffer | string, + options?: {|signal?: AbortSignal|}, + ): Promise; + getBuffer(key: string): Promise; + /** + * In a multi-threaded environment, where there are potentially multiple Cache + * instances writing to the cache, ensure that this instance has the latest view + * of the changes that may have been written to the cache in other threads. + */ + refresh(): void; +} diff --git a/packages/core/types-internal/src/DependencySpecifier.js b/packages/core/types-internal/src/DependencySpecifier.js new file mode 100644 index 00000000000..7422162b306 --- /dev/null +++ b/packages/core/types-internal/src/DependencySpecifier.js @@ -0,0 +1,4 @@ +// @flow strict-local + +/** See Dependency */ +export type DependencySpecifier = string; diff --git a/packages/core/types-internal/src/FileCreateInvalidation.js b/packages/core/types-internal/src/FileCreateInvalidation.js new file mode 100644 index 00000000000..8e8bea906e6 --- /dev/null +++ b/packages/core/types-internal/src/FileCreateInvalidation.js @@ -0,0 +1,22 @@ +// @flow + +import type {Glob} from './Glob'; +import type {FilePath} from './FilePath'; + +export type GlobInvalidation = {| + glob: Glob, +|}; + +export type FileInvalidation = {| + filePath: FilePath, +|}; + +export type FileAboveInvalidation = {| + fileName: string, + aboveFilePath: FilePath, +|}; + +export type FileCreateInvalidation = + | FileInvalidation + | GlobInvalidation + | FileAboveInvalidation; diff --git a/packages/core/types-internal/src/FilePath.js b/packages/core/types-internal/src/FilePath.js new file mode 100644 index 00000000000..11356400892 --- /dev/null +++ b/packages/core/types-internal/src/FilePath.js @@ -0,0 +1,3 @@ +// @flow strict-local + +export type FilePath = string; diff --git a/packages/core/fs/src/types.js b/packages/core/types-internal/src/FileSystem.js similarity index 98% rename from packages/core/fs/src/types.js rename to packages/core/types-internal/src/FileSystem.js index 8e46182903f..08fe950ef8a 100644 --- a/packages/core/fs/src/types.js +++ b/packages/core/types-internal/src/FileSystem.js @@ -1,5 +1,5 @@ // @flow -import type {FilePath} from '@parcel/types'; +import type {FilePath} from './FilePath'; import type {Readable, Writable} from 'stream'; import type { Event, diff --git a/packages/core/types-internal/src/Glob.js b/packages/core/types-internal/src/Glob.js new file mode 100644 index 00000000000..c2822c3cd4a --- /dev/null +++ b/packages/core/types-internal/src/Glob.js @@ -0,0 +1,3 @@ +// @flow strict-local + +export type Glob = string; diff --git a/packages/core/package-manager/src/types.js b/packages/core/types-internal/src/PackageManager.js similarity index 78% rename from packages/core/package-manager/src/types.js rename to packages/core/types-internal/src/PackageManager.js index 7025beb32c5..87bc0656546 100644 --- a/packages/core/package-manager/src/types.js +++ b/packages/core/types-internal/src/PackageManager.js @@ -1,15 +1,12 @@ // @flow -import type { - FilePath, - FileCreateInvalidation, - SemverRange, - DependencySpecifier, - PackageJSON, -} from '@parcel/types'; -import type {FileSystem} from '@parcel/fs'; - -export type ResolveResult = {| +import type {FileCreateInvalidation, PackageJSON} from './index'; +import type {SemverRange} from './SemverRange'; +import type {DependencySpecifier} from './DependencySpecifier'; +import type {FileSystem} from './FileSystem'; +import type {FilePath} from './FilePath'; + +export type PackageManagerResolveResult = {| resolved: FilePath | DependencySpecifier, pkg?: ?PackageJSON, invalidateOnFileCreate: Array, @@ -52,7 +49,7 @@ export interface PackageManager { id: DependencySpecifier, from: FilePath, ?{|range?: ?SemverRange, shouldAutoInstall?: boolean, saveDev?: boolean|}, - ): Promise; + ): Promise; getInvalidations(id: DependencySpecifier, from: FilePath): Invalidations; invalidate(id: DependencySpecifier, from: FilePath): void; } diff --git a/packages/core/types-internal/src/PackageName.js b/packages/core/types-internal/src/PackageName.js new file mode 100644 index 00000000000..30ee3c0de9a --- /dev/null +++ b/packages/core/types-internal/src/PackageName.js @@ -0,0 +1,3 @@ +// @flow strict-local + +export type PackageName = string; diff --git a/packages/core/types-internal/src/SemverRange.js b/packages/core/types-internal/src/SemverRange.js new file mode 100644 index 00000000000..97c27eba7bc --- /dev/null +++ b/packages/core/types-internal/src/SemverRange.js @@ -0,0 +1,3 @@ +// @flow strict-local + +export type SemverRange = string; diff --git a/packages/core/types/index.js b/packages/core/types-internal/src/index.js similarity index 97% rename from packages/core/types/index.js rename to packages/core/types-internal/src/index.js index f547e88a100..756e1357117 100644 --- a/packages/core/types/index.js +++ b/packages/core/types-internal/src/index.js @@ -2,21 +2,75 @@ import type {Readable} from 'stream'; import type SourceMap from '@parcel/source-map'; -import type {FileSystem} from '@parcel/fs'; -import type WorkerFarm from '@parcel/workers'; -import type {PackageManager} from '@parcel/package-manager'; import type { Diagnostic, Diagnostifiable, DiagnosticWithoutOrigin, } from '@parcel/diagnostic'; -import type {Cache} from '@parcel/cache'; - -import type {AST as _AST, ConfigResult as _ConfigResult} from './unsafe'; -import type {TraceMeasurement} from '@parcel/profiler'; import type {FeatureFlags} from '@parcel/feature-flags'; import type {Event, BackendType} from '@parcel/watcher'; +import type {Cache} from './Cache'; +import type { + FileSystem, + FileOptions, + Stats as FileStats, + ReaddirOptions, + Encoding, + Dirent, +} from './FileSystem'; +import type {AST as _AST, ConfigResult as _ConfigResult} from './unsafe'; +import type {FilePath} from './FilePath'; +import type {Glob} from './Glob'; +import type {PackageName} from './PackageName'; +import type { + PackageManager, + PackageManagerResolveResult, + Invalidations, + PackageInstaller, + ModuleRequest, + InstallOptions, + InstallerOptions, +} from './PackageManager'; +import type {SemverRange} from './SemverRange'; +import type {DependencySpecifier} from './DependencySpecifier'; +import type { + FileCreateInvalidation, + GlobInvalidation, + FileInvalidation, + FileAboveInvalidation, +} from './FileCreateInvalidation'; + +export interface TraceMeasurement { + end(): void; +} + +export type { + FilePath, + FileSystem, + FileOptions, + FileStats, + ReaddirOptions, + Encoding, + Dirent, + PackageName, + Glob, + DependencySpecifier, + SemverRange, + FileCreateInvalidation, + GlobInvalidation, + FileInvalidation, + FileAboveInvalidation, + PackageManager, + PackageManagerResolveResult, + Invalidations, + PackageInstaller, + ModuleRequest, + InstallOptions, + InstallerOptions, + Cache, +}; + /** Plugin-specific AST, any */ export type AST = _AST; export type ConfigResult = _ConfigResult; @@ -40,14 +94,7 @@ export type JSONValue = /** A JSON object (as in "map") */ export type JSONObject = {[key: string]: JSONValue, ...}; -export type PackageName = string; -export type FilePath = string; -export type Glob = string; export type Semver = string; -export type SemverRange = string; -/** See Dependency */ -export type DependencySpecifier = string; - /** A pipeline as specified in the config mapping to T */ export type GlobMap = {[Glob]: T, ...}; @@ -288,7 +335,7 @@ export type DetailedReportOptions = {| declare type GlobPattern = string; -export type InitialParcelOptions = {| +export type InitialParcelOptionsInternal = {| +entries?: FilePath | Array, +config?: DependencySpecifier, +defaultConfig?: DependencySpecifier, @@ -400,7 +447,7 @@ export type SourceLocation = {| |}; /** - * An object that plugins can write arbitatry data to. + * An object that plugins can write arbitrary data to. */ export type Meta = JSONObject; @@ -433,7 +480,7 @@ export interface AssetSymbols // eslint-disable-next-line no-undef export interface MutableAssetSymbols extends AssetSymbols { /** - * Initilizes the map, sets isCleared to false. + * Initializes the map, sets isCleared to false. */ ensure(): void; set( @@ -456,11 +503,11 @@ export interface MutableDependencySymbols // eslint-disable-next-line no-undef ], > { /** - * Initilizes the map, sets isCleared to false. + * Initializes the map, sets isCleared to false. */ ensure(): void; /** - * The symbols taht are imports are unknown, rather than just empty. + * The symbols that are imports are unknown, rather than just empty. * This is the default state. */ +isCleared: boolean; @@ -486,7 +533,7 @@ export type DependencyPriority = 'sync' | 'parallel' | 'lazy'; export type SpecifierType = 'commonjs' | 'esm' | 'url' | 'custom'; /** - * Usen when creating a Dependency, see that. + * Used when creating a Dependency, see that. * @section transformer */ export type DependencyOptions = {| @@ -1576,24 +1623,6 @@ export type BundleResult = {| +type?: string, |}; -export type GlobInvalidation = {| - glob: Glob, -|}; - -export type FileInvalidation = {| - filePath: FilePath, -|}; - -export type FileAboveInvalidation = {| - fileName: string, - aboveFilePath: FilePath, -|}; - -export type FileCreateInvalidation = - | FileInvalidation - | GlobInvalidation - | FileAboveInvalidation; - /** * @section resolver */ diff --git a/packages/core/types/unsafe.js b/packages/core/types-internal/src/unsafe.js similarity index 100% rename from packages/core/types/unsafe.js rename to packages/core/types-internal/src/unsafe.js diff --git a/packages/core/types/package.json b/packages/core/types/package.json index 76edc3a274e..f9025fd1b08 100644 --- a/packages/core/types/package.json +++ b/packages/core/types/package.json @@ -12,17 +12,11 @@ "access": "public" }, "scripts": { - "build-ts": "mkdir -p lib && flow-to-ts index.js > lib/index.d.ts && flow-to-ts unsafe.js > lib/unsafe.d.ts && node build-ts.js", + "build-ts": "mkdir -p lib ; flow-to-ts src/index.js > lib/index.d.ts", "check-ts": "tsc --noEmit lib/index.d.ts" }, "dependencies": { - "@parcel/cache": "2.12.0", - "@parcel/diagnostic": "2.12.0", - "@parcel/feature-flags": "2.12.0", - "@parcel/fs": "2.12.0", - "@parcel/package-manager": "2.12.0", - "@parcel/source-map": "^2.1.1", - "@parcel/workers": "2.12.0", - "utility-types": "^3.10.0" + "@parcel/types-internal": "2.12.0", + "@parcel/workers": "2.12.0" } } diff --git a/packages/core/types/src/index.js b/packages/core/types/src/index.js new file mode 100644 index 00000000000..3a3ffc1f40a --- /dev/null +++ b/packages/core/types/src/index.js @@ -0,0 +1,8 @@ +// @flow strict-local + +import type WorkerFarm from '@parcel/workers'; +import type {InitialParcelOptionsInternal} from '@parcel/types-internal'; + +export type * from '@parcel/types-internal'; + +export type InitialParcelOptions = InitialParcelOptionsInternal; diff --git a/packages/core/utils/src/alternatives.js b/packages/core/utils/src/alternatives.js index 5c216281abe..0037ad7c14f 100644 --- a/packages/core/utils/src/alternatives.js +++ b/packages/core/utils/src/alternatives.js @@ -1,6 +1,6 @@ // @flow import path from 'path'; -import type {FileSystem} from '@parcel/fs'; +import type {FileSystem} from '@parcel/types'; import {fuzzySearch} from './schema'; import {relativePath} from './path'; import {resolveConfig} from './config'; diff --git a/packages/core/workers/package.json b/packages/core/workers/package.json index 3a96414974c..a20e4c3b275 100644 --- a/packages/core/workers/package.json +++ b/packages/core/workers/package.json @@ -24,7 +24,7 @@ "@parcel/diagnostic": "2.12.0", "@parcel/logger": "2.12.0", "@parcel/profiler": "2.12.0", - "@parcel/types": "2.12.0", + "@parcel/types-internal": "2.12.0", "@parcel/utils": "2.12.0", "nullthrows": "^1.1.1" }, diff --git a/packages/core/workers/src/Worker.js b/packages/core/workers/src/Worker.js index 42d2afa604f..3b6a1204fff 100644 --- a/packages/core/workers/src/Worker.js +++ b/packages/core/workers/src/Worker.js @@ -1,6 +1,6 @@ // @flow -import type {FilePath} from '@parcel/types'; +import type {FilePath} from '@parcel/types-internal'; import type {BackendType, WorkerImpl, WorkerMessage} from './types'; import type {SharedReference} from './WorkerFarm'; diff --git a/packages/core/workers/src/WorkerFarm.js b/packages/core/workers/src/WorkerFarm.js index 9ca1e928080..7360828fe0a 100644 --- a/packages/core/workers/src/WorkerFarm.js +++ b/packages/core/workers/src/WorkerFarm.js @@ -1,6 +1,6 @@ // @flow -import type {ErrorWithCode, FilePath} from '@parcel/types'; +import type {ErrorWithCode, FilePath} from '@parcel/types-internal'; import type { CallRequest, HandleCallRequest, diff --git a/packages/core/workers/src/child.js b/packages/core/workers/src/child.js index f300c03b9a1..27cd1c1e451 100644 --- a/packages/core/workers/src/child.js +++ b/packages/core/workers/src/child.js @@ -9,7 +9,7 @@ import type { WorkerResponse, ChildImpl, } from './types'; -import type {Async, IDisposable} from '@parcel/types'; +import type {Async, IDisposable} from '@parcel/types-internal'; import type {SharedReference} from './WorkerFarm'; import * as coreWorker from './core-worker'; diff --git a/packages/core/workers/src/index.js b/packages/core/workers/src/index.js index f737ba2df4a..89e690a3204 100644 --- a/packages/core/workers/src/index.js +++ b/packages/core/workers/src/index.js @@ -1,5 +1,5 @@ // @flow -import type {TraceEvent, LogEvent} from '@parcel/types'; +import type {TraceEvent, LogEvent} from '@parcel/types-internal'; import invariant from 'assert'; import WorkerFarm from './WorkerFarm'; import Logger from '@parcel/logger'; diff --git a/packages/core/workers/src/types.js b/packages/core/workers/src/types.js index 0f3fd242670..8b0eb81eca3 100644 --- a/packages/core/workers/src/types.js +++ b/packages/core/workers/src/types.js @@ -1,6 +1,6 @@ // @flow import type {Diagnostic} from '@parcel/diagnostic'; -import type {FilePath} from '@parcel/types'; +import type {FilePath} from '@parcel/types-internal'; export type LocationCallRequest = {| args: $ReadOnlyArray, diff --git a/packages/dev/esm-fuzzer/package.json b/packages/dev/esm-fuzzer/package.json index ccfdeca79e4..e3edf7f934e 100644 --- a/packages/dev/esm-fuzzer/package.json +++ b/packages/dev/esm-fuzzer/package.json @@ -10,6 +10,8 @@ "@babel/generator": "^7.22.10", "@babel/template": "^7.22.5", "@babel/types": "^7.22.11", + "@parcel/core": "^2.12.0", + "@parcel/fs": "^2.12.0", "nanoid": "^3.1.12", "nullthrows": "^1.1.1" } diff --git a/packages/dev/repl/src/parcel/ParcelWorker.js b/packages/dev/repl/src/parcel/ParcelWorker.js index 1b7ea8da0b6..6b7af8c847e 100644 --- a/packages/dev/repl/src/parcel/ParcelWorker.js +++ b/packages/dev/repl/src/parcel/ParcelWorker.js @@ -3,6 +3,7 @@ import type {Diagnostic} from '@parcel/diagnostic'; import type {FSList, CodeMirrorDiagnostic, REPLOptions} from '../utils'; import type {MemoryFS} from '@parcel/fs'; import type {BuildSuccessEvent} from '@parcel/types'; +import type WorkerFarm from '@parcel/workers'; import {expose, proxy} from 'comlink'; import Parcel, {createWorkerFarm} from '@parcel/core'; @@ -42,7 +43,7 @@ export type BundleOutput = |} | BundleOutputError; -let workerFarm; +let workerFarm: WorkerFarm; let fs: MemoryFS; function startWorkerFarm(numWorkers: ?number) { // $FlowFixMe diff --git a/packages/validators/eslint/package.json b/packages/validators/eslint/package.json index efc884cf80e..88ec9b78b86 100644 --- a/packages/validators/eslint/package.json +++ b/packages/validators/eslint/package.json @@ -20,6 +20,7 @@ "parcel": "^2.12.0" }, "dependencies": { + "@parcel/diagnostic": "2.12.0", "@parcel/plugin": "2.12.0", "@parcel/utils": "2.12.0", "chalk": "^4.1.0"