Skip to content

Commit

Permalink
fix(babel): resolve babel config for every processed file (#1297)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anber committed Jul 22, 2023
1 parent dca076e commit ae3727f
Show file tree
Hide file tree
Showing 25 changed files with 654 additions and 493 deletions.
8 changes: 8 additions & 0 deletions .changeset/stale-swans-raise.md
@@ -0,0 +1,8 @@
---
'@linaria/babel-preset': patch
'@linaria/shaker': patch
'@linaria/testkit': patch
'@linaria/utils': patch
---

Fix the issues with processing files that are supposed to be parsed with their respective Babel config.
2 changes: 1 addition & 1 deletion packages/babel/src/cache.ts
Expand Up @@ -23,7 +23,7 @@ export class TransformCacheCollection {
}
> = new Map(),
public readonly evalCache: Map<string, Module> = new Map(),
public readonly originalASTCache: Map<string, File | 'ignored'> = new Map()
public readonly originalASTCache: Map<string, File> = new Map()
) {}

public invalidateForFile(filename: string) {
Expand Down
10 changes: 4 additions & 6 deletions packages/babel/src/evaluators/index.ts
Expand Up @@ -2,19 +2,17 @@
* This file is an entry point for module evaluation for getting lazy dependencies.
*/

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

import type { TransformCacheCollection } from '../cache';
import Module from '../module';
import loadLinariaOptions from '../transform-stages/helpers/loadLinariaOptions';
import type { Options } from '../types';

export default function evaluate(
cache: TransformCacheCollection,
code: string,
options: Pick<Options, 'filename' | 'pluginOptions'>
pluginOptions: StrictOptions,
filename: string
) {
const filename = options?.filename ?? 'unknown';
const pluginOptions = loadLinariaOptions(options.pluginOptions);

const m = new Module(filename ?? 'unknown', pluginOptions, cache);

m.dependencies = [];
Expand Down
10 changes: 6 additions & 4 deletions packages/babel/src/index.ts
Expand Up @@ -9,7 +9,6 @@ import { debug } from '@linaria/logger';

import transform from './plugins/babel-transform';
import type { PluginOptions } from './transform-stages/helpers/loadLinariaOptions';
import loadLinariaOptions from './transform-stages/helpers/loadLinariaOptions';

export { slugify } from '@linaria/utils';

Expand All @@ -21,14 +20,17 @@ export * from './types';
export { parseFile } from './transform-stages/helpers/parseFile';
export { default as loadLinariaOptions } from './transform-stages/helpers/loadLinariaOptions';
export type { PluginOptions } from './transform-stages/helpers/loadLinariaOptions';
export { prepareCode } from './transform-stages/1-prepare-for-eval';
export {
createEntrypoint,
prepareCode,
} from './transform-stages/1-prepare-for-eval';
export { transformUrl } from './transform-stages/4-extract';
export { default as isNode } from './utils/isNode';
export { default as getTagProcessor } from './utils/getTagProcessor';
export { default as getVisitorKeys } from './utils/getVisitorKeys';
export type { VisitorKeys } from './utils/getVisitorKeys';
export { default as peek } from './utils/peek';
export { default as processTemplateExpression } from './utils/processTemplateExpression';
export { processTemplateExpression } from './utils/processTemplateExpression';
export { TransformCacheCollection } from './cache';

function isEnabled(caller?: TransformCaller & { evaluate?: true }) {
Expand All @@ -41,6 +43,6 @@ export default function linaria(babel: ConfigAPI, options: PluginOptions) {
}
debug('options', JSON.stringify(options));
return {
plugins: [[transform, loadLinariaOptions(options)]],
plugins: [[transform, options]],
};
}
37 changes: 23 additions & 14 deletions packages/babel/src/plugins/babel-transform.ts
@@ -1,21 +1,23 @@
import type { BabelFile, PluginObj } from '@babel/core';
import type { NodePath } from '@babel/traverse';
import type { Identifier } from '@babel/types';

import { debug } from '@linaria/logger';
import type { StrictOptions } from '@linaria/utils';
import { removeWithRelated, syncResolve } from '@linaria/utils';

import type { Core } from '../babel';
import { TransformCacheCollection } from '../cache';
import { prepareForEvalSync } from '../transform-stages/1-prepare-for-eval';
import evalStage from '../transform-stages/2-eval';
import type { PluginOptions } from '../transform-stages/helpers/loadLinariaOptions';
import loadLinariaOptions from '../transform-stages/helpers/loadLinariaOptions';
import type { IPluginState } from '../types';
import processTemplateExpression from '../utils/processTemplateExpression';
import { processTemplateExpression } from '../utils/processTemplateExpression';
import withLinariaMetadata from '../utils/withLinariaMetadata';

export default function collector(
babel: Core,
options: StrictOptions
options: Partial<PluginOptions>
): PluginObj<IPluginState> {
const cache = new TransformCacheCollection();

Expand All @@ -30,14 +32,17 @@ export default function collector(
only: ['__linariaPreval'],
};

const pluginOptions = loadLinariaOptions(options);

const prepareStageResult = prepareForEvalSync(
babel,
cache,
syncResolve,
entrypoint,
pluginOptions,
{
root: file.opts.root ?? undefined,
pluginOptions: options,
inputSourceMap: file.opts.inputSourceMap ?? undefined,
}
);

Expand All @@ -48,27 +53,31 @@ export default function collector(
return;
}

const evalStageResult = evalStage(cache, prepareStageResult.code, {
filename: file.opts.filename!,
pluginOptions: options,
});
const evalStageResult = evalStage(
cache,
prepareStageResult.code,
pluginOptions,
file.opts.filename!
);

if (evalStageResult === null) {
return;
}

const [valueCache] = evalStageResult;

const identifiers: NodePath<Identifier>[] = [];
file.path.traverse({
// TODO: process transformed literals
Identifier: (p) => {
processTemplateExpression(p, file.opts, options, (processor) => {
processor.build(valueCache);

processor.doRuntimeReplacement();
});
identifiers.push(p);
},
});
identifiers.forEach((p) => {
processTemplateExpression(p, file.opts, pluginOptions, (processor) => {
processor.build(valueCache);
processor.doRuntimeReplacement();
});
});

// We can remove __linariaPreval export and all related code
const prevalExport = (
Expand Down
34 changes: 19 additions & 15 deletions packages/babel/src/plugins/collector.ts
Expand Up @@ -5,14 +5,15 @@

import type { BabelFile, PluginObj } from '@babel/core';
import type { NodePath } from '@babel/traverse';
import type { Identifier } from '@babel/types';

import { debug } from '@linaria/logger';
import type { StrictOptions } from '@linaria/utils';
import type { StrictOptions, LinariaMetadata } from '@linaria/utils';
import { removeWithRelated } from '@linaria/utils';

import type { Core } from '../babel';
import type { IPluginState, ValueCache } from '../types';
import processTemplateExpression from '../utils/processTemplateExpression';
import { processTemplateExpression } from '../utils/processTemplateExpression';

export default function collector(
babel: Core,
Expand All @@ -24,29 +25,31 @@ export default function collector(
pre(file: BabelFile) {
debug('collect:start', file.opts.filename);

this.processors = [];
const processors: LinariaMetadata['processors'] = [];

const identifiers: NodePath<Identifier>[] = [];
file.path.traverse({
// TODO: process transformed literals
Identifier: (p) => {
processTemplateExpression(p, file.opts, options, (processor) => {
processor.build(values);

processor.doRuntimeReplacement();
this.processors.push(processor);
});
identifiers.push(p);
},
});
},
visitor: {},
post(file: BabelFile) {
if (this.processors.length === 0) {

// TODO: process transformed literals
identifiers.forEach((p) => {
processTemplateExpression(p, file.opts, options, (processor) => {
processor.build(values);
processor.doRuntimeReplacement();
processors.push(processor);
});
});

if (processors.length === 0) {
// We didn't find any Linaria template literals.
return;
}

this.file.metadata.linaria = {
processors: this.processors,
processors,
replacements: [],
rules: {},
dependencies: [],
Expand All @@ -62,5 +65,6 @@ export default function collector(

debug('collect:end', file.opts.filename);
},
visitor: {},
};
}
2 changes: 1 addition & 1 deletion packages/babel/src/plugins/preeval.ts
Expand Up @@ -15,7 +15,7 @@ import {

import type { Core } from '../babel';
import type { IPluginState } from '../types';
import processTemplateExpression from '../utils/processTemplateExpression';
import { processTemplateExpression } from '../utils/processTemplateExpression';

export type PreevalOptions = Pick<
StrictOptions,
Expand Down

0 comments on commit ae3727f

Please sign in to comment.