diff --git a/packages/core/core/src/public/PluginOptions.js b/packages/core/core/src/public/PluginOptions.js index ca9d6c42e7e..aa7bea6562d 100644 --- a/packages/core/core/src/public/PluginOptions.js +++ b/packages/core/core/src/public/PluginOptions.js @@ -43,6 +43,10 @@ export default class PluginOptions implements IPluginOptions { return this.#options.env; } + get parcelVersion(): string { + return this.#options.parcelVersion; + } + get hmrOptions(): ?HMROptions { return this.#options.hmrOptions; } diff --git a/packages/core/core/src/resolveOptions.js b/packages/core/core/src/resolveOptions.js index 16325538a82..8bbe30cc114 100644 --- a/packages/core/core/src/resolveOptions.js +++ b/packages/core/core/src/resolveOptions.js @@ -26,6 +26,7 @@ import {toProjectPath} from './projectPath'; import {getResolveFrom} from './requests/ParcelConfigRequest'; import {DEFAULT_FEATURE_FLAGS} from '@parcel/feature-flags'; +import {PARCEL_VERSION} from './constants'; // Default cache directory name const DEFAULT_CACHE_DIRNAME = '.parcel-cache'; @@ -223,6 +224,7 @@ export default async function resolveOptions( isLibrary: initialOptions?.defaultTargetOptions?.isLibrary, }, featureFlags: {...DEFAULT_FEATURE_FLAGS, ...initialOptions?.featureFlags}, + parcelVersion: PARCEL_VERSION, }; } diff --git a/packages/core/core/src/types.js b/packages/core/core/src/types.js index b355f968b09..e165e24c57f 100644 --- a/packages/core/core/src/types.js +++ b/packages/core/core/src/types.js @@ -274,6 +274,7 @@ export type ParcelOptions = {| config?: DependencySpecifier, defaultConfig?: DependencySpecifier, env: EnvMap, + parcelVersion: string, targets: ?(Array | {+[string]: TargetDescriptor, ...}), shouldDisableCache: boolean, cacheDir: FilePath, diff --git a/packages/core/core/test/test-utils.js b/packages/core/core/test/test-utils.js index 6a430bb4f3d..37784a57837 100644 --- a/packages/core/core/test/test-utils.js +++ b/packages/core/core/test/test-utils.js @@ -17,6 +17,7 @@ cache.ensure(); export const DEFAULT_OPTIONS: ParcelOptions = { cacheDir: path.join(__dirname, '.parcel-cache'), + parcelVersion: '', watchDir: __dirname, watchIgnore: undefined, watchBackend: undefined, diff --git a/packages/core/integration-tests/.mocharc.json b/packages/core/integration-tests/.mocharc.json index 76ff008a637..d4e99c150ab 100644 --- a/packages/core/integration-tests/.mocharc.json +++ b/packages/core/integration-tests/.mocharc.json @@ -2,5 +2,7 @@ "require": ["@parcel/babel-register", "@parcel/test-utils/src/mochaSetup.js"], "timeout": 50000, // TODO: Remove this when https://github.com/nodejs/node/pull/28788 is resolved - "exit": true + "exit": true, + // This helps reduce CI flakiness of the Mac OS test run + "retries": 2 } diff --git a/packages/core/integration-tests/test/api.js b/packages/core/integration-tests/test/api.js index b2327e9d708..00d37bcacfd 100644 --- a/packages/core/integration-tests/test/api.js +++ b/packages/core/integration-tests/test/api.js @@ -1,7 +1,16 @@ // @flow strict-local import path from 'path'; import assert from 'assert'; -import {distDir, bundle, assertBundles, outputFS} from '@parcel/test-utils'; +import { + distDir, + bundle, + assertBundles, + outputFS, + overlayFS, + fsFixture, +} from '@parcel/test-utils'; + +import {PARCEL_VERSION} from '../../core/src/constants'; describe('JS API', function () { it('should respect distEntry', async function () { @@ -49,4 +58,52 @@ describe('JS API', function () { assert(await outputFS.exists(path.join(distDir, 'bundle-buddy.json'))); }); + + describe('Reporter API', () => { + it('should pass the parcel version to plugins', async () => { + const dir = path.join(__dirname, 'plugin-parcel-version'); + + overlayFS.mkdirp(dir); + + await fsFixture(overlayFS, dir)` + index.js: + export default 'Hi'; + + .parcelrc: + { + extends: "@parcel/config-default", + reporters: ["./reporter-plugin.js", "..."], + } + + package.json: + { + "version": "1234" + } + + yarn.lock: + + reporter-plugin.js: + import {Reporter} from '@parcel/plugin'; + import path from 'node:path'; + + export default new Reporter({ + async report({event, options}) { + if (event.type === 'buildSuccess') { + await options.outputFS.writeFile(path.join(options.projectRoot, 'parcel-version.txt'), options.parcelVersion); + } + } + }) + `; + + await bundle(path.join(dir, 'index.js'), { + inputFS: overlayFS, + outputFS: overlayFS, + }); + + assert.equal( + await overlayFS.readFile(path.join(dir, 'parcel-version.txt')), + PARCEL_VERSION, + ); + }); + }); }); diff --git a/packages/core/types-internal/src/index.js b/packages/core/types-internal/src/index.js index 1fae14576d8..cb5c8f29fd8 100644 --- a/packages/core/types-internal/src/index.js +++ b/packages/core/types-internal/src/index.js @@ -400,6 +400,7 @@ export type InitialServerOptions = {| export interface PluginOptions { +mode: BuildMode; + +parcelVersion: string; +env: EnvMap; +hmrOptions: ?HMROptions; +serveOptions: ServerOptions | false; diff --git a/packages/reporters/cli/test/CLIReporter.test.js b/packages/reporters/cli/test/CLIReporter.test.js index 70d43eefe9b..0e9e99d246b 100644 --- a/packages/reporters/cli/test/CLIReporter.test.js +++ b/packages/reporters/cli/test/CLIReporter.test.js @@ -14,6 +14,7 @@ import {DEFAULT_FEATURE_FLAGS} from '@parcel/feature-flags'; const EMPTY_OPTIONS = { cacheDir: '.parcel-cache', + parcelVersion: '', entries: [], logLevel: 'info', targets: [],