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 2 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
5 changes: 5 additions & 0 deletions packages/core/core/src/public/PluginOptions.js
Expand Up @@ -13,6 +13,7 @@ import type {FileSystem} from '@parcel/fs';
import type {PackageManager} from '@parcel/package-manager';
import type {ParcelOptions} from '../types';
import {type FeatureFlags} from '@parcel/feature-flags';
import {PARCEL_VERSION} from '../constants';

let parcelOptionsToPluginOptions: WeakMap<ParcelOptions, PluginOptions> =
new WeakMap();
Expand Down Expand Up @@ -43,6 +44,10 @@ export default class PluginOptions implements IPluginOptions {
return this.#options.env;
}

get parcelVersion(): string {
return PARCEL_VERSION;
}

get hmrOptions(): ?HMROptions {
return this.#options.hmrOptions;
}
Expand Down
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');
const versionFileLocation = path.join(dir, 'parcel-version.txt');

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';

export default new Reporter({
async report({event, options}) {
if (event.type === 'buildSuccess') {
await options.outputFS.writeFile("${versionFileLocation}", options.parcelVersion);
}
}
})
`;

await bundle(path.join(dir, 'index.js'), {
inputFS: overlayFS,
outputFS: overlayFS,
});

assert.equal(
await overlayFS.readFile(versionFileLocation),
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