Skip to content

Commit

Permalink
feat: jsx-runtime.js for automatic babel mode
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenybai committed Aug 6, 2021
1 parent e577762 commit ac35da5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
!dist/**/*
!LICENSE
!/README.md
!package.json
!package.json
!jsx-runtime.js
57 changes: 57 additions & 0 deletions jsx-runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { m, VFlags, className, style, svg } from './dist/million.esm';

const h = (tag, props, children, delta) => {
let type = VFlags.ANY_CHILDREN;
if (!children) type = VFlags.NO_CHILDREN;
if (children && children.some((child) => typeof child === 'string')) {
type = VFlags.ONLY_TEXT_CHILDREN;
}
if (props) {
if (props.className && typeof props.className === 'object') {
props.className = className(props.className);
}
if (props.style && typeof props.style === 'object') {
props.style = style(props.style);
}
}

const vnode = m(tag, props, children, type, delta);
return !props?.ns && tag === 'svg' ? svg(vnode) : vnode;
};

const normalize = (children, normalizedChildren) => {
for (const child of children) {
if (child !== undefined && child !== null && child !== false && child !== '') {
if (Array.isArray(child)) {
normalize(child, normalizedChildren);
} else if (
typeof child === 'string' ||
typeof child === 'number' ||
typeof child === 'boolean'
) {
normalizedChildren.push(String(child));
} else {
normalizedChildren.push(child);
}
}
}
return normalizedChildren;
};

const jsx = (tag, props, ...children) => {
const normalizedChildren = normalize(children, []);
let delta;
if (props.delta && props.delta.length > 0) {
delta = props.delta;
delete props.delta;
}
if (typeof tag === 'function') {
return tag(props || {}, normalizedChildren, delta);
} else {
return h(tag, props || {}, normalizedChildren, delta);
}
};

const Fragment = (props) => props.children;

export { jsx, jsx as jsxs, Fragment };
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@
"url": "git+https://github.com/aidenybai/million.git"
},
"exports": {
"require": "./dist/million.cjs.js",
"import": "./dist/million.esm.js"
".": {
"import": "./dist/million.esm.js",
"require": "./dist/million.cjs.js"
},
"./jsx-runtime": {
"import": "./jsx-runtime.js",
"require": "./jsx-runtime.js"
}
},
"sideEffects": false,
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
"declaration": true,
"strict": true
},
"include": ["./src/**/*.ts", "jest.setup.ts"],
"include": ["./src/**/*.ts"],
"exclude": ["node_modules", "dist", "./src/**/*.js", "./src/**/*.spec.ts"]
}

0 comments on commit ac35da5

Please sign in to comment.