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

New Transpiler (Limited AST) #123

Merged
merged 47 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
25e21d5
core package draft
taishinaritomi Feb 19, 2023
8d0c354
add swc-plan
taishinaritomi Feb 19, 2023
a5e7044
rename swc-idea
taishinaritomi Feb 19, 2023
092eda7
update swc idea
taishinaritomi Feb 20, 2023
f368ba0
fix code format
taishinaritomi Feb 21, 2023
5159b00
remove swc-idea
taishinaritomi Feb 21, 2023
9a63e08
Initial new-transpiler
taishinaritomi Feb 21, 2023
b02f7c4
update core index export
taishinaritomi Feb 22, 2023
d92a47b
fix swc plugin options
taishinaritomi Feb 22, 2023
e2d545c
move code core
taishinaritomi Feb 22, 2023
01436bd
fix code format
taishinaritomi Feb 22, 2023
877487f
remove dead code
taishinaritomi Feb 22, 2023
1ec5e9e
remove dead code
taishinaritomi Feb 22, 2023
3ac0bce
fix build package
taishinaritomi Feb 22, 2023
611684c
fix core export
taishinaritomi Feb 22, 2023
bc7f0f6
swc-plugin usize to u8
taishinaritomi Feb 22, 2023
b1d66d0
add user options
taishinaritomi Feb 22, 2023
a108825
fix code format
taishinaritomi Feb 22, 2023
05e2fae
fix swc-plugin small fix
taishinaritomi Feb 22, 2023
276db46
fix rename BuildArgument to BuildArg and more
taishinaritomi Feb 23, 2023
cf8d2a0
NOT TreeShake Webpack Node ChildComplier
taishinaritomi Feb 23, 2023
dee290c
update swc-plugin dependency & profile.release
taishinaritomi Feb 24, 2023
71c0c43
fix playground
taishinaritomi Feb 25, 2023
1954861
fix webpack-plugin
taishinaritomi Feb 25, 2023
9bbc490
fix code format
taishinaritomi Feb 25, 2023
110c271
fix playground
taishinaritomi Feb 25, 2023
a8858bf
fix webpack-plugin
taishinaritomi Feb 25, 2023
127897b
fix swc-plugin pre-transform
taishinaritomi Feb 25, 2023
1f625b2
fix swc-plugin pre-transform
taishinaritomi Feb 25, 2023
a71e210
swc-plugin u8 to usize
taishinaritomi Feb 27, 2023
215dc08
refactor rename
taishinaritomi Feb 28, 2023
71c581f
add babel-plugin preTransform
taishinaritomi Feb 28, 2023
a407c89
add babel-plugin transform
taishinaritomi Mar 2, 2023
79dea24
refactor remove dead code
taishinaritomi Mar 2, 2023
f7e256c
add build package babel transpiler
taishinaritomi Mar 2, 2023
26c26b3
clean up collector
taishinaritomi Mar 2, 2023
bbd53ac
refactor astNode & astNode array types
taishinaritomi Mar 3, 2023
b6cb0d7
swc-plugin wasm size optimize
taishinaritomi Mar 4, 2023
ec91e1f
Merge branch 'main' into new-transpiler
taishinaritomi Mar 5, 2023
064a656
comeback createStyle & createGlobalStyle
taishinaritomi Mar 13, 2023
e20c36f
update playground
taishinaritomi Mar 13, 2023
aa590e9
(not completion) create library docs
taishinaritomi Mar 17, 2023
95fbc32
fix code format
taishinaritomi Mar 17, 2023
363d148
remove dead code for swc-plugin
taishinaritomi Mar 18, 2023
9c14e21
update docs
taishinaritomi Mar 23, 2023
98c3f9c
update playground
taishinaritomi Mar 23, 2023
a8a6679
refactor rename
taishinaritomi Mar 23, 2023
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
95 changes: 95 additions & 0 deletions docs/HOW_TO_CREATE_LIBRARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# How to create Library

Library authors can use kaze-style to create pre-generated CSS in JS.

In kaze-style, there are two ways to create a Library.

## 1. Static assets

type reference [style](./1.STYLE.md) | [globalStyle](./2.GLOBAL_STYLE.md)

```ts
// Example
// @kaze-style/themes theme & globalTheme uses this method.

import { style, globalStyle } from '@kaze-style/core';
import { theme, globalTheme } from '@kaze-style/themes';

globalStyle(globalTheme.reset());

export const classes = style({
container: {
...theme.animation('spin'),
},
});
```

### how to create

```ts
// Example
export const animation = {
spin: {
animationDuration: '1s',
animationIterationCount: 'infinite',
animationTimingFunction: 'linear',
animationName: {
from: {
transform: 'rotate(0deg)',
},
to: {
transform: 'rotate(360deg)',
},
},
},
} as const;
```

## 2. Function API

This one can do more than static assets, but is a bit more complicated.

```ts
// Example
// @kaze-style/core style & globalStyle uses this method.

import { cssVar } from 'your-lib';

export const vars = cssVar({
darkColor: 'black',
lightColor: 'white',
});
```

### how to create

```ts
// Example
// @kaze-style/core style api
import { isBuildTime } from './isBuildTime';
import { resolveStyle } from './resolveStyle';
import { setCssRules } from './setCssRules';
import type { BuildArg, Classes } from './types/common';
import type { KazeStyle } from './types/style';
import { classesSerialize } from './utils/classesSerialize';

export function style<K extends string>(styles: KazeStyle<K>): Classes<K>;
export function style<K extends string>(
styles: KazeStyle<K>,
buildArg: BuildArg,
index: number,
): Classes<K>;
export function style<K extends string>(
styles: KazeStyle<K>,
buildArg?: BuildArg,
index?: number,
): Classes<K> {
const [cssRules, classes, staticClasses] = resolveStyle(styles);
if (isBuildTime(buildArg) && typeof index !== 'undefined') {
const classesNode = classesSerialize(staticClasses);
buildArg.injector.cssRules.push(...cssRules);
buildArg.injector.args.push({ value: [classesNode], index });
} else if (typeof document !== 'undefined') setCssRules(cssRules);
return classes;
}
```
47 changes: 47 additions & 0 deletions packages/babel-plugin/src/astNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { types as t } from '@babel/core';
import type { AstNode } from '@kaze-style/core';

export const nodeToExpr = (node: AstNode): t.Expression => {
if (node.type === 'String') {
return { type: 'StringLiteral', value: node.value };
} else if (node.type === 'Number') {
return { type: 'NumericLiteral', value: node.value };
} else if (node.type === 'Boolean') {
return { type: 'BooleanLiteral', value: node.value };
} else if (node.type === 'Null') {
return { type: 'NullLiteral' };
} else if (node.type === 'Identifier') {
return {
type: 'Identifier',
name: node.name,
};
} else if (node.type === 'Call') {
return {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: node.name,
},
arguments: node.arguments.map((value) => nodeToExpr(value)),
};
} else if (node.type === 'Array') {
return {
type: 'ArrayExpression',
elements: node.elements.map((value) => nodeToExpr(value)),
};
} else {
return {
type: 'ObjectExpression',
properties: node.properties.map(({ key, value }) => ({
type: 'ObjectProperty',
key: {
type: 'StringLiteral',
value: key,
},
value: nodeToExpr(value),
computed: false,
shorthand: false,
})),
};
}
};
5 changes: 5 additions & 0 deletions packages/babel-plugin/src/commonConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type InputTransform = {
source: string;
from: string;
to: string;
};
2 changes: 0 additions & 2 deletions packages/babel-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ export * from './preTransform';
export * from './transform';
export * from './preTransformPlugin';
export * from './transformPlugin';
export type { PreTransformOptions } from './preTransformPlugin';
export type { TransformOptions } from './transformPlugin';
export type { TransformOptions as BabelOptions } from '@babel/core';
13 changes: 7 additions & 6 deletions packages/babel-plugin/src/preTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,32 @@ import { transformAsync as babelTransform } from '@babel/core';
import type { TransformOptions as BabelOptions } from '@babel/core';
// @ts-expect-error type
import typescriptSyntax from '@babel/plugin-syntax-typescript';
import type { PreTransformOptions } from './preTransformPlugin';
import { preTransformPlugin } from './preTransformPlugin';

type Options = {
filename: string;
preTransformOptions: PreTransformOptions;
babelOptions?: BabelOptions;
transform: Record<string, unknown>;
babel?: BabelOptions;
};

type Metadata = { isTransformed: boolean };
type Result = [string, Metadata];

export const preTransform = async (
code: string,
{ filename, preTransformOptions, babelOptions = {} }: Options,
options: Options,
): Promise<Result> => {
const babelOptions = options.babel || {};
const transformOptions = options.transform || {};
const result = await babelTransform(code, {
filename,
filename: options.filename,
caller: { name: 'kaze' },
babelrc: false,
configFile: false,
compact: false,
...babelOptions,
plugins: [
[preTransformPlugin, preTransformOptions],
[preTransformPlugin, transformOptions],
[typescriptSyntax, { isTSX: true }],
...(babelOptions.plugins || []),
],
Expand Down
Loading