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

Feature: forgetti/runtime #3

Merged
merged 16 commits into from
Mar 22, 2023
35 changes: 35 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: CI
on:
- push
jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2.2.2
with:
version: 7
run_install: |
- recursive: true
args: [--frozen-lockfile]

- uses: actions/setup-node@v3
with:
node-version: 16
cache: 'pnpm'

- name: Clean
run: pnpm recursive run clean
env:
CI: true

- name: Build
run: pnpm recursive run build
env:
CI: true

- name: Begin Tests
run: pnpm recursive run test
env:
CI: true
3 changes: 2 additions & 1 deletion examples/preact/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
"devDependencies": {
"@preact/preset-vite": "^2.5.0",
"eslint": "^8.34.0",
"eslint-config-lxsmnsyc": "^0.4.8",
"eslint-config-lxsmnsyc": "^0.5.1",
"typescript": "^4.9.5",
"vite": "^4.1.2",
"vite-plugin-forgetti": "0.3.0"
},
"dependencies": {
"forgetti": "0.3.0",
"preact": "^10.13.0"
},
"version": "0.3.0"
Expand Down
3 changes: 2 additions & 1 deletion examples/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
"@types/react-dom": "^18.0.11",
"@vitejs/plugin-react": "^3.1.0",
"eslint": "^8.34.0",
"eslint-config-lxsmnsyc": "^0.4.8",
"eslint-config-lxsmnsyc": "^0.5.1",
"typescript": "^4.9.5",
"vite": "^4.1.2",
"vite-plugin-forgetti": "0.3.0"
},
"dependencies": {
"forgetti": "0.3.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"examples/*"
],
"devDependencies": {
"eslint": "^8.33.0",
"eslint-config-lxsmnsyc": "^0.4.8",
"lerna": "^6.4.1",
"eslint": "^8.36.0",
"eslint-config-lxsmnsyc": "^0.5.1",
"lerna": "^6.5.1",
"typescript": "^4.9.5"
}
}
33 changes: 23 additions & 10 deletions packages/forgetti/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@
"pridepack"
],
"devDependencies": {
"@babel/core": "^7.20.12",
"@babel/core": "^7.21.3",
"@types/babel__core": "^7.20.0",
"@types/babel__traverse": "^7.18.3",
"@types/node": "^18.13.0",
"eslint": "^8.32.0",
"eslint-config-lxsmnsyc": "^0.4.8",
"pridepack": "2.4.0",
"@types/node": "^18.15.3",
"eslint": "^8.36.0",
"eslint-config-lxsmnsyc": "^0.5.1",
"pridepack": "2.4.2",
"tslib": "^2.5.0",
"typescript": "^4.9.4",
"vitest": "^0.28.1"
},
"dependencies": {
"@babel/helper-module-imports": "^7.18.6",
"@babel/traverse": "^7.20.13",
"@babel/types": "^7.20.7"
"@babel/traverse": "^7.21.3",
"@babel/types": "^7.21.3"
},
"peerDependencies": {
"@babel/core": "^7"
Expand Down Expand Up @@ -59,9 +59,13 @@
"author": "Alexis Munsayac",
"private": false,
"typesVersions": {
"*": {}
"*": {
"runtime": [
"./dist/types/runtime/index.d.ts"
]
}
},
"types": "./dist/types/index.d.ts",
"types": "./dist/types/src/index.d.ts",
"main": "./dist/cjs/production/index.cjs",
"module": "./dist/esm/production/index.mjs",
"exports": {
Expand All @@ -72,7 +76,16 @@
},
"require": "./dist/cjs/production/index.cjs",
"import": "./dist/esm/production/index.mjs",
"types": "./dist/types/index.d.ts"
"types": "./dist/types/src/index.d.ts"
},
"./runtime": {
"development": {
"require": "./dist/cjs/development/runtime.cjs",
"import": "./dist/esm/development/runtime.mjs"
},
"require": "./dist/cjs/production/runtime.cjs",
"import": "./dist/esm/production/runtime.mjs",
"types": "./dist/types/runtime/index.d.ts"
}
}
}
6 changes: 5 additions & 1 deletion packages/forgetti/pridepack.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"target": "es2017"
"target": "es2017",
"entrypoints": {
".": "src/index.ts",
"./runtime": "runtime/index.ts"
}
}
10 changes: 10 additions & 0 deletions packages/forgetti/runtime/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function $$equals(a: unknown, b: unknown): boolean {
// eslint-disable-next-line no-self-compare
return a === b || (a !== a && b !== b);
}

export type MemoHook = <T>(callback: () => T, dependencies: unknown[]) => T;

export function $$cache(hook: MemoHook, size: number) {
return hook(() => new Array<unknown>(size), []);
}
70 changes: 0 additions & 70 deletions packages/forgetti/src/core/arrays.ts

This file was deleted.

17 changes: 10 additions & 7 deletions packages/forgetti/src/core/checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ import * as babel from '@babel/core';
import { ComponentNode, StateContext } from './types';

export function getImportSpecifierName(specifier: t.ImportSpecifier): string {
if (t.isIdentifier(specifier.imported)) {
if (specifier.imported.type === 'Identifier') {
return specifier.imported.name;
}
return specifier.imported.value;
}

export function isComponent(node: t.Node): node is ComponentNode {
return (
t.isArrowFunctionExpression(node)
|| t.isFunctionExpression(node)
|| t.isFunctionDeclaration(node)
);
switch (node.type) {
case 'ArrowFunctionExpression':
case 'FunctionExpression':
case 'FunctionDeclaration':
return true;
default:
return false;
}
}

export function isComponentNameValid(
Expand All @@ -23,7 +26,7 @@ export function isComponentNameValid(
checkName = false,
) {
if (checkName) {
if (t.isFunctionExpression(node) || t.isFunctionDeclaration(node)) {
if (node.type !== 'ArrowFunctionExpression') {
return (
node.id
&& (
Expand Down
7 changes: 5 additions & 2 deletions packages/forgetti/src/core/get-foreign-bindings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as babel from '@babel/core';
import * as t from '@babel/types';
import { map } from './arrays';

function isForeignBinding(
source: babel.NodePath,
Expand Down Expand Up @@ -59,5 +58,9 @@ export default function getForeignBindings(path: babel.NodePath): t.Identifier[]
},
});

return map([...identifiers], (value) => t.identifier(value));
const result: t.Identifier[] = [];
for (const identifier of identifiers) {
result.push(t.identifier(identifier));
}
return result;
}
21 changes: 21 additions & 0 deletions packages/forgetti/src/core/get-import-identifier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as babel from '@babel/core';
import { addDefault, addNamed } from '@babel/helper-module-imports';
import { ImportRegistration } from './presets';
import { StateContext } from './types';

export default function getImportIdentifier(
ctx: StateContext,
path: babel.NodePath,
definition: ImportRegistration,
) {
const target = `${definition.source}[${definition.name}]`;
const current = ctx.hooks.get(target);
if (current) {
return current;
}
const newID = (definition.kind === 'named')
? addNamed(path, definition.name, definition.source)
: addDefault(path, definition.source);
ctx.hooks.set(target, newID);
return newID;
}
13 changes: 13 additions & 0 deletions packages/forgetti/src/core/imports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ImportRegistration } from './presets';

export const RUNTIME_EQUALS: ImportRegistration = {
name: '$$equals',
source: 'forgetti/runtime',
kind: 'named',
};

export const RUNTIME_CACHE: ImportRegistration = {
name: '$$cache',
source: 'forgetti/runtime',
kind: 'named',
};
Loading