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

feat(transform): allow non-usage of WeakRef in Module conditionally #71

Merged
merged 2 commits into from
Apr 16, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/weak-yaks-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@wyw-in-js/transform': patch
---

Allow conditional usage of WeakRef in Module evalutation through a new feature flag `useWeakRefInEval`
1 change: 1 addition & 0 deletions packages/shared/src/options/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type AllFeatureFlags = {
happyDOM: FeatureFlag;
softErrors: FeatureFlag;
useBabelConfigs: FeatureFlag;
useWeakRefInEval: FeatureFlag;
};

export type FeatureFlags<
Expand Down
1 change: 1 addition & 0 deletions packages/transform/src/__tests__/module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const options: StrictOptions = {
happyDOM: true,
softErrors: false,
useBabelConfigs: true,
useWeakRefInEval: true,
},
highPriorityPlugins: [],
overrideContext: (context) => ({
Expand Down
1 change: 1 addition & 0 deletions packages/transform/src/__tests__/shaker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const run = (only: string[]) => (code: TemplateStringsArray) => {
happyDOM: false,
softErrors: false,
useBabelConfigs: false,
useWeakRefInEval: true,
},
highPriorityPlugins: [],
onlyExports: only,
Expand Down
17 changes: 13 additions & 4 deletions packages/transform/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import vm from 'vm';

import { invariant } from 'ts-invariant';

import type { Debugger } from '@wyw-in-js/shared';
import { isFeatureEnabled, type Debugger } from '@wyw-in-js/shared';

import './utils/dispose-polyfill';
import type { TransformCacheCollection } from './cache';
Expand Down Expand Up @@ -192,7 +192,7 @@ export class Module {

private cache: TransformCacheCollection;

#entrypointRef: WeakRef<Entrypoint>;
#entrypointRef: WeakRef<Entrypoint> | Entrypoint;

constructor(
private services: Services,
Expand All @@ -201,7 +201,13 @@ export class Module {
private moduleImpl: HiddenModuleMembers = DefaultModuleImplementation
) {
this.cache = services.cache;
this.#entrypointRef = new WeakRef(entrypoint);
this.#entrypointRef = isFeatureEnabled(
services.options.pluginOptions.features,
'useWeakRefInEval',
entrypoint.name
)
? new WeakRef(entrypoint)
: entrypoint;
this.idx = entrypoint.idx;
this.id = entrypoint.name;
this.filename = entrypoint.name;
Expand Down Expand Up @@ -232,7 +238,10 @@ export class Module {
}

protected get entrypoint(): Entrypoint {
const entrypoint = this.#entrypointRef.deref();
const entrypoint =
this.#entrypointRef instanceof WeakRef
? this.#entrypointRef.deref()
: this.#entrypointRef;
invariant(entrypoint, `Module ${this.idx} is disposed`);
return entrypoint;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export function loadWywOptions(
happyDOM: true,
softErrors: false,
useBabelConfigs: true,
useWeakRefInEval: true,
};

const options: StrictOptions = {
Expand Down
Loading