Skip to content

Commit

Permalink
feat(transform): allow non-usage of WeakRef in Module conditionally (#71
Browse files Browse the repository at this point in the history
)

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

* Add changeset
  • Loading branch information
brijeshb42 authored Apr 16, 2024
1 parent 7cc243b commit cd7b7f0
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 4 deletions.
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
1 change: 1 addition & 0 deletions packages/transform/src/transform/helpers/loadWywOptions.ts
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

0 comments on commit cd7b7f0

Please sign in to comment.