Skip to content

Commit

Permalink
chore(utils): move some utils and types from babel to utils package (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Anber committed Jul 21, 2023
1 parent e59bf80 commit 7955724
Show file tree
Hide file tree
Showing 39 changed files with 441 additions and 381 deletions.
12 changes: 12 additions & 0 deletions .changeset/honest-otters-guess.md
@@ -0,0 +1,12 @@
---
'@linaria/atomic': patch
'@linaria/babel-preset': patch
'@linaria/core': patch
'@linaria/react': patch
'@linaria/stylelint': patch
'@linaria/tags': patch
'@linaria/testkit': patch
'@linaria/utils': patch
---

Nothing has changed. Just moved some utils and types from babel to utils package.
2 changes: 1 addition & 1 deletion packages/atomic/src/processors/styled.ts
Expand Up @@ -3,8 +3,8 @@ import type { SourceLocation } from '@babel/types';
import { debug } from '@linaria/logger';
import type { IProps } from '@linaria/react/processors/styled';
import StyledProcessor from '@linaria/react/processors/styled';
import { hasMeta } from '@linaria/tags';
import type { Rules, ValueCache } from '@linaria/tags';
import { hasMeta } from '@linaria/utils';

import atomize from './helpers/atomize';

Expand Down
4 changes: 1 addition & 3 deletions packages/babel/src/index.ts
@@ -1,5 +1,5 @@
/**
* File defines babel prest for Linaria.
* File defines babel preset for Linaria.
* It uses ./extract function that is an entry point for styles extraction.
* It also bypass babel options defined in Linaria config file with it's defaults (see ./utils/loadOptions).
*/
Expand All @@ -14,8 +14,6 @@ import loadLinariaOptions from './transform-stages/helpers/loadLinariaOptions';
export { slugify } from '@linaria/utils';

export { default as preeval } from './plugins/preeval';
export * from './utils/collectTemplateDependencies';
export { default as collectTemplateDependencies } from './utils/collectTemplateDependencies';
export { default as withLinariaMetadata } from './utils/withLinariaMetadata';
export { default as Module, DefaultModuleImplementation } from './module';
export { default as transform } from './transform';
Expand Down
10 changes: 7 additions & 3 deletions packages/babel/src/transform-stages/4-extract.ts
Expand Up @@ -4,9 +4,11 @@ import type { Mapping } from 'source-map';
import { SourceMapGenerator } from 'source-map';
import stylis from 'stylis';

import type { BaseProcessor, Replacements } from '@linaria/tags';
import type { Rules } from '@linaria/tags';
import type { Replacements } from '@linaria/utils';
import type { Artifact } from '@linaria/utils/types/types';

import type { Rules, Options, PreprocessorFn } from '../types';
import type { Options, PreprocessorFn } from '../types';

const STYLIS_DECLARATION = 1;
const posixSep = path.posix.sep;
Expand Down Expand Up @@ -118,7 +120,9 @@ function extractCssFromAst(
* Extract artifacts (e.g. CSS) from processors
*/
export default function extractStage(
processors: BaseProcessor[],
processors: {
artifacts: Artifact[];
}[],
originalCode: string,
options: Options
) {
Expand Down
31 changes: 19 additions & 12 deletions packages/babel/src/transform-stages/helpers/loadLinariaOptions.ts
Expand Up @@ -9,7 +9,24 @@ export type PluginOptions = StrictOptions & {
stage?: Stage;
};

const explorerSync = cosmiconfigSync('linaria');
const searchPlaces = [
`.linariarc`,
`.linariarc.json`,
`.linariarc.yaml`,
`.linariarc.yml`,
`.linariarc.js`,
`.linariarc.cjs`,
`.config/linariarc`,
`.config/linariarc.json`,
`.config/linariarc.yaml`,
`.config/linariarc.yml`,
`.config/linariarc.js`,
`.config/linariarc.cjs`,
`linaria.config.js`,
`linaria.config.cjs`,
];

const explorerSync = cosmiconfigSync('linaria', { searchPlaces });

const cache = new WeakMap<Partial<PluginOptions>, StrictOptions>();
const defaultOverrides = {};
Expand All @@ -32,17 +49,7 @@ export default function loadLinariaOptions(
const options = {
displayName: false,
evaluate: true,
extensions: [
'.cjs',
'.cts',
'.json',
'.js',
'.jsx',
'.mjs',
'.mts',
'.ts',
'.tsx',
],
extensions: ['.cjs', '.cts', '.js', '.jsx', '.mjs', '.mts', '.ts', '.tsx'],
rules: rules ?? [
{
action: require.resolve('@linaria/shaker'),
Expand Down
37 changes: 5 additions & 32 deletions packages/babel/src/types.ts
Expand Up @@ -4,9 +4,12 @@ import type { File } from '@babel/types';
import type { RawSourceMap } from 'source-map';

import type { BaseProcessor } from '@linaria/tags';
import type { LinariaMetadata, Replacement, Rules } from '@linaria/utils';

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

export type { Value, ValueCache } from '@linaria/tags';

export type {
ExpressionValue,
FunctionValue,
Expand All @@ -15,32 +18,12 @@ export type {
JSONValue,
LazyValue,
Serializable,
Value,
ValueCache,
} from '@linaria/tags';

export { ValueType } from '@linaria/tags';

export interface ICSSRule {
className: string;
displayName: string;
cssText: string;
start: Location | null | undefined;
atom?: boolean;
}
} from '@linaria/utils';

export type Rules = Record<string, ICSSRule>;
export { ValueType } from '@linaria/utils';

export type Dependencies = string[];

export type LinariaMetadata = {
processors: BaseProcessor[];

rules: Rules;
replacements: Replacement[];
dependencies: string[];
};

export interface IPluginState extends PluginPass {
processors: BaseProcessor[];
dependencies: Dependencies;
Expand All @@ -58,16 +41,6 @@ export interface ITransformFileResult {

export type Stage = 'preeval' | 'collect';

export type Location = {
line: number;
column: number;
};

export type Replacement = {
original: { start: Location; end: Location };
length: number;
};

export type Result = {
code: string;
sourceMap?: RawSourceMap | null;
Expand Down
Expand Up @@ -4,8 +4,9 @@ import generate from '@babel/generator';
import dedent from 'dedent';
import stripAnsi from 'strip-ansi';

import { extractExpression } from '@linaria/utils';

import type { MissedBabelCoreTypes } from '../../types';
import { extractExpression } from '../collectTemplateDependencies';

const { File } = babel as typeof babel & MissedBabelCoreTypes;

Expand Down
30 changes: 11 additions & 19 deletions packages/babel/src/utils/getTagProcessor.ts
Expand Up @@ -9,30 +9,23 @@ import type {
SourceLocation,
Identifier,
MemberExpression,
Program,
} from '@babel/types';

import { BaseProcessor } from '@linaria/tags';
import type {
Param,
Params,
IFileContext,
ExpressionValue,
TagSource,
} from '@linaria/tags';
import type { IImport, StrictOptions } from '@linaria/utils';
import type { Param, Params, IFileContext, TagSource } from '@linaria/tags';
import type { ExpressionValue, IImport, StrictOptions } from '@linaria/utils';
import {
collectExportsAndImports,
collectTemplateDependencies,
explicitImport,
extractExpression,
findPackageJSON,
getSource,
isNotNull,
mutate,
} from '@linaria/utils';

import collectTemplateDependencies, {
extractExpression,
} from './collectTemplateDependencies';
import getSource from './getSource';

type BuilderArgs = ConstructorParameters<typeof BaseProcessor> extends [
Params,
TagSource,
Expand Down Expand Up @@ -337,7 +330,7 @@ function getBuilderForIdentifier(
function getDisplayName(
path: NodePath<Identifier>,
idx: number,
fileContext: IFileContext
filename?: string | null
): string {
let displayName: string | undefined;

Expand Down Expand Up @@ -372,11 +365,10 @@ function getDisplayName(
}

if (!displayName) {
const filename = fileContext.filename ?? 'unknown';
// Try to derive the path from the filename
displayName = basename(filename);
displayName = basename(filename ?? 'unknown');

if (/^index\.[a-z\d]+$/.test(displayName)) {
if (filename && /^index\.[a-z\d]+$/.test(displayName)) {
// If the file name is 'index', better to get name from parent folder
displayName = basename(dirname(filename));
}
Expand Down Expand Up @@ -446,7 +438,7 @@ export default function getTagProcessor(
>
): BaseProcessor | null {
if (!cache.has(path.node)) {
const root = path.scope.getProgramParent().path;
const root = path.scope.getProgramParent().path as NodePath<Program>;
const { imports } = collectExportsAndImports(root);
try {
const builder = getBuilderForIdentifier(
Expand All @@ -461,7 +453,7 @@ export default function getTagProcessor(
// Also used for display name if it couldn't be determined
const idx = getNextIndex(fileContext);

const displayName = getDisplayName(path, idx, fileContext);
const displayName = getDisplayName(path, idx, fileContext.filename);

const processor = builder(
displayName,
Expand Down
2 changes: 1 addition & 1 deletion packages/babel/src/utils/withLinariaMetadata.ts
@@ -1,4 +1,4 @@
import type { LinariaMetadata } from '../types';
import type { LinariaMetadata } from '@linaria/utils';

const withLinariaMetadata = (
value: unknown
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/css.ts
@@ -1,4 +1,4 @@
import type { StyledMeta } from '@linaria/tags';
import type { StyledMeta } from '@linaria/utils';

import type { CSSProperties } from './CSSProperties';
import type { LinariaClassName } from './cx';
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/index.ts
Expand Up @@ -6,4 +6,4 @@ export type {
Styled,
} from './styled';
export type { CSSProperties } from '@linaria/core';
export type { StyledMeta } from '@linaria/tags';
export type { StyledMeta } from '@linaria/utils';
4 changes: 1 addition & 3 deletions packages/react/src/processors/styled.ts
Expand Up @@ -20,14 +20,12 @@ import type {
} from '@linaria/tags';
import {
buildSlug,
hasMeta,
TaggedTemplateProcessor,
validateParams,
ValueType,
toValidCSSIdentifier,
} from '@linaria/tags';
import type { IVariableContext } from '@linaria/utils';
import { findPackageJSON, slugify } from '@linaria/utils';
import { findPackageJSON, hasMeta, slugify, ValueType } from '@linaria/utils';

const isNotNull = <T>(x: T | null): x is T => x !== null;

Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/styled.ts
Expand Up @@ -10,7 +10,7 @@ import React from 'react';

import { cx } from '@linaria/core';
import type { CSSProperties } from '@linaria/core';
import type { StyledMeta } from '@linaria/tags';
import type { StyledMeta } from '@linaria/utils';

export type NoInfer<A> = [A][A extends any ? 0 : never];

Expand Down
4 changes: 2 additions & 2 deletions packages/stylelint/src/preprocessor.ts
@@ -1,7 +1,7 @@
import stripAnsi from 'strip-ansi';

import { transform } from '@linaria/babel-preset';
import type { Replacement } from '@linaria/babel-preset';
import type { Replacements } from '@linaria/utils';
import { asyncResolveFallback } from '@linaria/utils';

type Errors = {
Expand All @@ -21,7 +21,7 @@ type Errors = {
};

type Cache = {
[key: string]: Replacement[] | null | undefined;
[key: string]: Replacements | null | undefined;
};

type Warning = {
Expand Down
15 changes: 5 additions & 10 deletions packages/tags/src/BaseProcessor.ts
Expand Up @@ -8,16 +8,11 @@ import type {
MemberExpression,
} from '@babel/types';

import type {
ExpressionValue,
IInterpolation,
Params,
Value,
ValueCache,
Artifact,
} from './types';
import type { Artifact, ExpressionValue } from '@linaria/utils';
import { hasMeta } from '@linaria/utils';

import type { IInterpolation, Params, Value, ValueCache } from './types';
import getClassNameAndSlug from './utils/getClassNameAndSlug';
import hasMeta from './utils/hasMeta';
import { isCSSable } from './utils/toCSS';
import type { IFileContext, IOptions } from './utils/types';
import { validateParams } from './utils/validateParams';
Expand All @@ -34,7 +29,7 @@ export type TagSource = {
source: string;
};

export default abstract class BaseProcessor {
export abstract class BaseProcessor {
public static SKIP = Symbol('skip');

public readonly artifacts: Artifact[] = [];
Expand Down
8 changes: 5 additions & 3 deletions packages/tags/src/TaggedTemplateProcessor.ts
@@ -1,9 +1,11 @@
import type { TemplateElement, Expression, SourceLocation } from '@babel/types';

import type { ExpressionValue } from '@linaria/utils';
import { ValueType } from '@linaria/utils';

import type { TailProcessorParams } from './BaseProcessor';
import BaseProcessor from './BaseProcessor';
import type { ExpressionValue, ValueCache, Rules, Params } from './types';
import { ValueType } from './types';
import { BaseProcessor } from './BaseProcessor';
import type { ValueCache, Rules, Params } from './types';
import templateProcessor from './utils/templateProcessor';
import { validateParams } from './utils/validateParams';

Expand Down
16 changes: 10 additions & 6 deletions packages/tags/src/index.ts
@@ -1,10 +1,14 @@
export * from './BaseProcessor';
export { BaseProcessor } from './BaseProcessor';
export type {
Expression,
TagSource,
ProcessorParams,
TailProcessorParams,
} from './BaseProcessor';
export * from './types';
export { buildSlug } from './utils/buildSlug';
export { default as isSerializable } from './utils/isSerializable';
export * from './utils/types';
export * from './utils/validateParams';
export { default as BaseProcessor } from './BaseProcessor';
export type { IOptions, IFileContext } from './utils/types';
export { isValidParams, validateParams } from './utils/validateParams';
export type { MapParams, ParamConstraints } from './utils/validateParams';
export { default as TaggedTemplateProcessor } from './TaggedTemplateProcessor';
export { default as hasMeta } from './utils/hasMeta';
export { default as toValidCSSIdentifier } from './utils/toValidCSSIdentifier';

0 comments on commit 7955724

Please sign in to comment.