Skip to content

Commit

Permalink
fix(babel): lost plugin options (#1361)
Browse files Browse the repository at this point in the history
* fix(babel): lost pluginOptions

* fix(babel): do not pass pluginOptions to Entrypoint since services already have it

* chore: changeset

* chore: linter

* chore: tests
  • Loading branch information
Anber committed Sep 28, 2023
1 parent 15fa87a commit 5f216d3
Show file tree
Hide file tree
Showing 17 changed files with 144 additions and 139 deletions.
6 changes: 6 additions & 0 deletions .changeset/afraid-starfishes-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@linaria/babel-preset': patch
'@linaria/testkit': patch
---

Fix for lost `pluginOptions` in some entrypoints.
6 changes: 6 additions & 0 deletions .changeset/tall-clouds-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@linaria/babel-preset': patch
'@linaria/testkit': patch
---

`pluginOptions` could be lost during processing.
6 changes: 3 additions & 3 deletions packages/babel/src/evaluators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
* This file is an entry point for module evaluation for getting lazy dependencies.
*/

import type { TransformCacheCollection } from '../cache';
import Module from '../module';
import type { Entrypoint } from '../transform/Entrypoint';
import type { Services } from '../transform/types';

export interface IEvaluateResult {
dependencies: string[];
value: Record<string | symbol, unknown>;
}

export default function evaluate(
cache: TransformCacheCollection,
services: Services,
entrypoint: Entrypoint
): IEvaluateResult {
const m = new Module(entrypoint, cache);
const m = new Module(services, entrypoint);

m.evaluate();

Expand Down
30 changes: 12 additions & 18 deletions packages/babel/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ import { invariant } from 'ts-invariant';
import type { Debugger } from '@linaria/logger';

import './utils/dispose-polyfill';
import { TransformCacheCollection } from './cache';
import type { TransformCacheCollection } from './cache';
import { Entrypoint } from './transform/Entrypoint';
import { getStack, isSuperSet } from './transform/Entrypoint.helpers';
import type { IEntrypointDependency } from './transform/Entrypoint.types';
import type { IEvaluatedEntrypoint } from './transform/EvaluatedEntrypoint';
import { isUnprocessedEntrypointError } from './transform/actions/UnprocessedEntrypointError';
import loadLinariaOptions from './transform/helpers/loadLinariaOptions';
import { withDefaultServices } from './transform/helpers/withDefaultServices';
import type { Services } from './transform/types';
import { createVmContext } from './vm/createVmContext';

type HiddenModuleMembers = {
Expand Down Expand Up @@ -191,14 +190,17 @@ class Module {

public resolve = resolve.bind(this);

private cache: TransformCacheCollection;

#entrypointRef: WeakRef<Entrypoint>;

constructor(
private services: Services,
entrypoint: Entrypoint,
private cache = new TransformCacheCollection(),
parentModule?: Module,
private moduleImpl: HiddenModuleMembers = DefaultModuleImplementation
) {
this.cache = services.cache;
this.#entrypointRef = new WeakRef(entrypoint);
this.idx = entrypoint.idx;
this.id = entrypoint.name;
Expand All @@ -214,7 +216,7 @@ class Module {
this.callstack = [entrypoint.name];
}

this.extensions = entrypoint.pluginOptions.extensions;
this.extensions = services.options.pluginOptions.extensions;

this.debug('init', entrypoint.name);
}
Expand Down Expand Up @@ -250,7 +252,8 @@ class Module {
evaluatedCreated = true;
}

const { transformedCode: source, pluginOptions } = entrypoint;
const { transformedCode: source } = entrypoint;
const { pluginOptions } = this.services.options;

if (!source) {
this.debug(`evaluate`, 'there is nothing to evaluate');
Expand Down Expand Up @@ -386,21 +389,12 @@ class Module {

// If code wasn't extracted from cache, it indicates that we were unable
// to process some of the imports on stage1. Let's try to reprocess.
const services = withDefaultServices({
cache: this.cache,
options: {
filename,
},
});

const pluginOptions = loadLinariaOptions({});
const code = fs.readFileSync(filename, 'utf-8');
const newEntrypoint = Entrypoint.createRoot(
services,
this.services,
filename,
only,
code,
pluginOptions
code
);

if (newEntrypoint.evaluated) {
Expand Down Expand Up @@ -469,7 +463,7 @@ class Module {
};

protected createChild(entrypoint: Entrypoint): Module {
return new Module(entrypoint, this.cache, this, this.moduleImpl);
return new Module(this.services, entrypoint, this, this.moduleImpl);
}
}

Expand Down
8 changes: 6 additions & 2 deletions packages/babel/src/plugins/babel-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { syncResolve } from '@linaria/utils';
import type { Core } from '../babel';
import { TransformCacheCollection } from '../cache';
import { transformSync } from '../transform';
import loadLinariaOptions from '../transform/helpers/loadLinariaOptions';
import type { ICollectAction, SyncScenarioForAction } from '../transform/types';
import type { IPluginState, PluginOptions } from '../types';

Expand All @@ -25,7 +26,8 @@ export default function babelTransform(
this: ICollectAction
): SyncScenarioForAction<ICollectAction> {
const { valueCache } = this.data;
const { loadedAndParsed, pluginOptions } = this.entrypoint;
const { loadedAndParsed } = this.entrypoint;
const { pluginOptions } = this.services.options;
if (loadedAndParsed.evaluator === 'ignored') {
throw new Error('entrypoint was ignored');
}
Expand All @@ -40,6 +42,8 @@ export default function babelTransform(

debug('babel-transform:start', file.opts.filename);

const pluginOptions = loadLinariaOptions(options);

transformSync(
{
babel,
Expand All @@ -48,7 +52,7 @@ export default function babelTransform(
filename: file.opts.filename!,
root: file.opts.root ?? undefined,
inputSourceMap: file.opts.inputSourceMap ?? undefined,
pluginOptions: options,
pluginOptions,
},
},
file.code,
Expand Down
33 changes: 23 additions & 10 deletions packages/babel/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
asyncResolveImports,
syncResolveImports,
} from './transform/generators/resolveImports';
import type { PartialOptions } from './transform/helpers/loadLinariaOptions';
import loadLinariaOptions from './transform/helpers/loadLinariaOptions';
import { withDefaultServices } from './transform/helpers/withDefaultServices';
import type {
Expand All @@ -29,9 +30,11 @@ import type {
} from './transform/types';
import type { Result } from './types';

type RequiredServices = 'options';
type PartialServices = Partial<Omit<Services, RequiredServices>> &
Pick<Services, RequiredServices>;
type PartialServices = Partial<Omit<Services, 'options'>> & {
options: Omit<Services['options'], 'pluginOptions'> & {
pluginOptions?: PartialOptions;
};
};

type AllHandlers<TMode extends 'async' | 'sync'> = Handlers<TMode>;

Expand All @@ -41,9 +44,15 @@ export function transformSync(
syncResolve: (what: string, importer: string, stack: string[]) => string,
customHandlers: Partial<AllHandlers<'sync'>> = {}
): Result {
const services = withDefaultServices(partialServices);
const { options } = services;
const { options } = partialServices;
const pluginOptions = loadLinariaOptions(options.pluginOptions);
const services = withDefaultServices({
...partialServices,
options: {
...options,
pluginOptions,
},
});

if (
!isFeatureEnabled(pluginOptions.features, 'globalCache', options.filename)
Expand All @@ -56,8 +65,7 @@ export function transformSync(
services,
options.filename,
['__linariaPreval'],
originalCode,
pluginOptions
originalCode
);

if (entrypoint.ignored) {
Expand Down Expand Up @@ -111,8 +119,14 @@ export default async function transform(
customHandlers: Partial<AllHandlers<'sync'>> = {}
): Promise<Result> {
const { options } = partialServices;
const services = withDefaultServices(partialServices);
const pluginOptions = loadLinariaOptions(options.pluginOptions);
const services = withDefaultServices({
...partialServices,
options: {
...options,
pluginOptions,
},
});

if (
!isFeatureEnabled(pluginOptions.features, 'globalCache', options.filename)
Expand All @@ -133,8 +147,7 @@ export default async function transform(
services,
options.filename,
['__linariaPreval'],
originalCode,
pluginOptions
originalCode
);

if (entrypoint.ignored) {
Expand Down
9 changes: 6 additions & 3 deletions packages/babel/src/transform/Entrypoint.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,13 @@ export function loadAndParse(
services: Services,
name: string,
loadedCode: string | undefined,
log: Debugger,
pluginOptions: StrictOptions
log: Debugger
): IEntrypointCode | IIgnoredEntrypoint {
const { babel, eventEmitter } = services;
const {
babel,
eventEmitter,
options: { pluginOptions },
} = services;

const extension = extname(name);

Expand Down
38 changes: 7 additions & 31 deletions packages/babel/src/transform/Entrypoint.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { invariant } from 'ts-invariant';

import type { StrictOptions } from '@linaria/utils';

import type { ParentEntrypoint, ITransformFileResult } from '../types';

import { BaseEntrypoint } from './BaseEntrypoint';
Expand Down Expand Up @@ -64,7 +62,6 @@ export class Entrypoint extends BaseEntrypoint {
public readonly initialCode: string | undefined,
name: string,
only: string[],
public readonly pluginOptions: StrictOptions,
exports: Record<string | symbol, unknown> | undefined,
evaluatedOnly: string[],
loadedAndParsed?: IEntrypointCode | IIgnoredEntrypoint,
Expand All @@ -83,8 +80,7 @@ export class Entrypoint extends BaseEntrypoint {
services,
name,
initialCode,
parents[0]?.log ?? services.log,
pluginOptions
parents[0]?.log ?? services.log
);

if (this.loadedAndParsed.code !== undefined) {
Expand Down Expand Up @@ -121,17 +117,9 @@ export class Entrypoint extends BaseEntrypoint {
services: Services,
name: string,
only: string[],
loadedCode: string | undefined,
pluginOptions: StrictOptions
loadedCode: string | undefined
): Entrypoint {
const created = Entrypoint.create(
services,
null,
name,
only,
loadedCode,
pluginOptions
);
const created = Entrypoint.create(services, null, name, only, loadedCode);
invariant(created !== 'loop', 'loop detected');

return created;
Expand All @@ -154,8 +142,7 @@ export class Entrypoint extends BaseEntrypoint {
parent: ParentEntrypoint | null,
name: string,
only: string[],
loadedCode: string | undefined,
pluginOptions: StrictOptions
loadedCode: string | undefined
): Entrypoint | 'loop' {
const { cache, eventEmitter } = services;
return eventEmitter.perf('createEntrypoint', () => {
Expand All @@ -172,8 +159,7 @@ export class Entrypoint extends BaseEntrypoint {
: null,
name,
only,
loadedCode,
pluginOptions
loadedCode
);

if (status !== 'cached') {
Expand All @@ -189,8 +175,7 @@ export class Entrypoint extends BaseEntrypoint {
parent: ParentEntrypoint | null,
name: string,
only: string[],
loadedCode: string | undefined,
pluginOptions: StrictOptions
loadedCode: string | undefined
): ['loop' | 'created' | 'cached', Entrypoint] {
const { cache } = services;

Expand Down Expand Up @@ -245,7 +230,6 @@ export class Entrypoint extends BaseEntrypoint {
loadedCode,
name,
mergedOnly,
pluginOptions,
exports,
evaluatedOnly,
undefined,
Expand Down Expand Up @@ -330,14 +314,7 @@ export class Entrypoint extends BaseEntrypoint {
only: string[],
loadedCode?: string
): Entrypoint | 'loop' {
return Entrypoint.create(
this.services,
this,
name,
only,
loadedCode,
this.pluginOptions
);
return Entrypoint.create(this.services, this, name, only, loadedCode);
}

public createEvaluated() {
Expand Down Expand Up @@ -405,7 +382,6 @@ export class Entrypoint extends BaseEntrypoint {
this.initialCode,
this.name,
newOnlyOrEntrypoint,
this.pluginOptions,
this.exports,
this.evaluatedOnly,
this.loadedAndParsed,
Expand Down
5 changes: 2 additions & 3 deletions packages/babel/src/transform/Entrypoint.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { TransformOptions } from '@babel/core';
import type { File } from '@babel/types';

import type { Debugger } from '@linaria/logger';
import type { Evaluator, StrictOptions } from '@linaria/utils';
import type { Evaluator } from '@linaria/utils';

import type { Services } from './types';

Expand Down Expand Up @@ -30,6 +30,5 @@ export type LoadAndParseFn = (
services: Services,
name: string,
loadedCode: string | undefined,
log: Debugger,
pluginOptions: StrictOptions
log: Debugger
) => IEntrypointCode | IIgnoredEntrypoint;

0 comments on commit 5f216d3

Please sign in to comment.