Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
ae0f622
Implement HTM without eval
jviide Dec 21, 2018
dbdc12e
Optimize build.js size by avoiding comparisons
jviide Dec 26, 2018
6752939
Implement multiple root elements
jviide Dec 27, 2018
d060bb8
Optimize size with String#indexOf
jviide Dec 27, 2018
7199d5c
Use property access instead of String#charAt
jviide Dec 27, 2018
80e602c
Remove a String#indexOf call
jviide Dec 27, 2018
0bb9227
Merge branch 'master' into no-eval
developit Dec 27, 2018
077a065
Disable caching, evaluate directly
jviide Jan 9, 2019
bd4572a
Merge branch 'master' into no-eval
jviide Jan 16, 2019
bcd229a
Backport optimizations from the htmini branch
jviide Jan 16, 2019
1f277a2
Optimize build.mjs size
jviide Jan 16, 2019
1bddae7
Clean up unnecessary code
jviide Jan 16, 2019
3ad2f8c
Reorganize code to optimize size
jviide Jan 16, 2019
35e2821
Reorganize code for smaller size
jviide Jan 16, 2019
0608217
Optimize instruction arrays
jviide Jan 16, 2019
dcb7b95
Fix if -> else if
jviide Jan 20, 2019
73b183e
Merge branch 'master' into htmini
developit Jan 21, 2019
2f38ad1
[mini] inline build to save a few more bytes
developit Jan 21, 2019
1f992e8
Use Map for caching when available
jviide Jan 21, 2019
314d351
Merge pull request #1 from developit/htmmini-size-reduc
jviide Jan 21, 2019
5b4947f
Combine no-eval and htmini branches
jviide Jan 30, 2019
48cfaf6
Merge branch 'master' into megamix
jviide Feb 21, 2019
fac9149
Update to microbundle v0.10.0, enable mini builds
jviide Feb 21, 2019
a9154f0
Update to microbundle v0.10.1, use the alias option to set MINI
jviide Feb 22, 2019
917fc90
Merge branch 'master' into megamix
jviide Feb 22, 2019
d8cdc21
Merge branch 'master' into megamix
developit Feb 25, 2019
10edc74
Add an extra hint for dead-code elimination
jviide Feb 25, 2019
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules
package-lock.json
/preact
dist
mini
yarn.lock
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"umd:main": "dist/htm.umd.js",
"module": "dist/htm.mjs",
"scripts": {
"build": "npm run -s build:main && npm run -s build:preact && npm run -s build:babel && npm run -s build:babel-transform-jsx",
"build": "npm run -s build:main && npm run -s build:mini && npm run -s build:preact && npm run -s build:babel && npm run -s build:babel-transform-jsx",
"build:main": "microbundle src/index.mjs -f es,umd --no-sourcemap --target web && microbundle src/cjs.mjs -f iife --no-sourcemap --target web",
"build:mini": "microbundle src/index.mjs -o ./mini/index.js -f es,umd --no-sourcemap --target web --alias ./constants=./constants-mini && microbundle src/cjs.mjs -o ./mini/index.js -f iife --no-sourcemap --target web --alias ./constants=./constants-mini",
"build:preact": "cd src/integrations/preact && npm run build",
"build:babel": "cd packages/babel-plugin-htm && npm run build",
"build:babel-transform-jsx": "cd packages/babel-plugin-transform-jsx-to-htm && npm run build",
Expand All @@ -16,14 +17,17 @@
},
"files": [
"dist",
"mini",
"preact",
"src"
],
"eslintConfig": {
"extends": "developit",
"rules": {
"prefer-const": 0,
"no-fallthrough": 0
"prefer-spread": 0,
"prefer-rest-params": 0,
"func-style": 0
}
},
"jest": {
Expand Down
32 changes: 27 additions & 5 deletions packages/babel-plugin-htm/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export default function htmBabelPlugin({ types: t }, options = {}) {
const useBuiltIns = options.useBuiltIns;
const inlineVNodes = options.monomorphic || pragma===false;

const symbol = Symbol();

function dottedIdentifier(keypath) {
const path = keypath.split('.');
let out;
Expand Down Expand Up @@ -73,7 +75,26 @@ export default function htmBabelPlugin({ types: t }, options = {}) {
return t.callExpression(pragma, [tag, props].concat(children));
}

function flatten(props, result = []) {
const { [symbol]: head, ...tail } = props;
if (head) head.forEach(obj => {
flatten(obj, result);
});
if (Object.keys(tail).length > 0) {
result.push(tail);
}
return result;
}

function spreadNode(args, state) {
if (args.length > 0 && t.isNode(args[0])) {
args.unshift({});
}

// 'Object.assign(x)', can be collapsed to 'x'.
if (args.length === 1) {
return propsNode(args[0]);
}
// 'Object.assign({}, x)', can be collapsed to 'x'.
if (args.length === 2 && !t.isNode(args[0]) && Object.keys(args[0]).length === 0) {
return propsNode(args[1]);
Expand All @@ -83,8 +104,6 @@ export default function htmBabelPlugin({ types: t }, options = {}) {
}

function propsNode(props) {
if (props == null) return t.nullLiteral();

return t.isNode(props) ? props : t.objectExpression(
Object.keys(props).map(key => {
let value = props[key];
Expand All @@ -111,7 +130,7 @@ export default function htmBabelPlugin({ types: t }, options = {}) {
return t.isNode(child) ? child : transform(child, state);
}
const newTag = typeof tag === 'string' ? t.stringLiteral(tag) : tag;
const newProps = !Array.isArray(props) ? propsNode(props) : spreadNode(props, state);
const newProps = props ? spreadNode(flatten(props), state) : t.nullLiteral();
const newChildren = t.arrayExpression(children.map(childMapper));
return createVNode(newTag, newProps, newChildren);
}
Expand All @@ -125,7 +144,7 @@ export default function htmBabelPlugin({ types: t }, options = {}) {
function treeify(statics, expr) {
const assign = Object.assign;
try {
Object.assign = function(...objs) { return objs; };
Object.assign = function(...objs) { return { [symbol]: objs }; };
return html(statics, ...expr);
}
finally {
Expand All @@ -147,7 +166,10 @@ export default function htmBabelPlugin({ types: t }, options = {}) {
const expr = path.node.quasi.expressions;

const tree = treeify(statics, expr);
path.replaceWith(transform(tree, state));
const node = !Array.isArray(tree)
? transform(tree, state)
: t.arrayExpression(tree.map(root => transform(root, state)));
path.replaceWith(node);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/constants-mini.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const MINI = true;
1 change: 1 addition & 0 deletions src/constants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const MINI = false;
Loading