Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parcel version to PluginOptions #9671

Merged
merged 6 commits into from Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/core/core/src/public/PluginOptions.js
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/core/src/resolveOptions.js
Expand Up @@ -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';
Expand Down Expand Up @@ -223,6 +224,7 @@ export default async function resolveOptions(
isLibrary: initialOptions?.defaultTargetOptions?.isLibrary,
},
featureFlags: {...DEFAULT_FEATURE_FLAGS, ...initialOptions?.featureFlags},
parcelVersion: PARCEL_VERSION,
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/core/src/types.js
Expand Up @@ -274,6 +274,7 @@ export type ParcelOptions = {|
config?: DependencySpecifier,
defaultConfig?: DependencySpecifier,
env: EnvMap,
parcelVersion: string,
targets: ?(Array<string> | {+[string]: TargetDescriptor, ...}),
shouldDisableCache: boolean,
cacheDir: FilePath,
Expand Down
1 change: 1 addition & 0 deletions packages/core/core/test/test-utils.js
Expand Up @@ -17,6 +17,7 @@ cache.ensure();

export const DEFAULT_OPTIONS: ParcelOptions = {
cacheDir: path.join(__dirname, '.parcel-cache'),
parcelVersion: '',
watchDir: __dirname,
watchIgnore: undefined,
watchBackend: undefined,
Expand Down
4 changes: 3 additions & 1 deletion packages/core/integration-tests/.mocharc.json
Expand Up @@ -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
}
59 changes: 58 additions & 1 deletion 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 () {
Expand Down Expand Up @@ -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,
);
});
});
});
1 change: 1 addition & 0 deletions packages/core/types-internal/src/index.js
Expand Up @@ -400,6 +400,7 @@ export type InitialServerOptions = {|

export interface PluginOptions {
+mode: BuildMode;
+parcelVersion: string;
+env: EnvMap;
+hmrOptions: ?HMROptions;
+serveOptions: ServerOptions | false;
Expand Down
1 change: 1 addition & 0 deletions packages/reporters/cli/test/CLIReporter.test.js
Expand Up @@ -14,6 +14,7 @@ import {DEFAULT_FEATURE_FLAGS} from '@parcel/feature-flags';

const EMPTY_OPTIONS = {
cacheDir: '.parcel-cache',
parcelVersion: '',
entries: [],
logLevel: 'info',
targets: [],
Expand Down