Skip to content

Commit

Permalink
use modules for moon MVL
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed May 5, 2019
1 parent 72c9bdc commit 66dadb6
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 21 deletions.
1 change: 0 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"no-ex-assign": 2,
"no-extra-boolean-cast": 2,
"no-extra-semi": 2,
"no-fallthrough": 2,
"no-func-assign": 2,
"no-global-assign": 2,
"no-inner-declarations": 2,
Expand Down
98 changes: 90 additions & 8 deletions packages/moon-mvl/dist/moon-mvl.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,98 @@
* Released under the MIT License
* https://kbrsh.github.io/moon
*/
var Moon = require("moon");
import Moon from 'moon';

var slash = require("./slash");
/**
* Slash
* Fast, efficient hash
* Copyright 2017-2018 Kabir Shah
* Released under the MIT License
*/

/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "xhl|xhh" }] */
var box = [158, 163, 200, 235, 223, 47, 250, 35, 101, 191, 5, 132, 161, 71, 142, 18, 7, 133, 176, 253, 246, 207, 117, 233, 138, 92, 74, 16, 187, 10, 91, 249, 94, 210, 80, 167, 13, 136, 202, 150, 62, 126, 160, 162, 186, 22, 108, 124, 112, 21, 232, 14, 148, 127, 218, 143, 214, 220, 39, 57, 190, 144, 159, 130, 88, 2, 154, 242, 81, 24, 111, 147, 66, 54, 238, 28, 37, 63, 45, 153, 169, 102, 208, 247, 226, 48, 79, 27, 183, 145, 184, 228, 180, 89, 56, 20, 11, 170, 40, 84, 152, 198, 55, 103, 237, 221, 178, 174, 8, 29, 90, 122, 205, 155, 12, 67, 30, 96, 243, 43, 254, 26, 44, 15, 193, 251, 59, 4, 78, 129, 137, 31, 203, 1, 219, 70, 181, 236, 157, 72, 225, 104, 194, 222, 53, 0, 241, 85, 131, 192, 6, 9, 114, 46, 248, 95, 240, 121, 128, 244, 239, 107, 19, 201, 25, 149, 116, 105, 34, 175, 76, 224, 146, 51, 139, 41, 75, 204, 52, 23, 171, 68, 196, 164, 50, 77, 245, 229, 134, 106, 252, 217, 115, 33, 188, 97, 61, 227, 86, 87, 82, 32, 166, 17, 98, 93, 60, 231, 140, 172, 125, 195, 212, 206, 73, 177, 3, 69, 58, 199, 118, 141, 209, 197, 109, 173, 123, 211, 135, 189, 151, 110, 213, 83, 38, 113, 216, 119, 179, 99, 36, 42, 120, 255, 185, 64, 168, 230, 49, 234, 165, 65, 156, 215, 182, 100];

function multiply(x, y) {
var x1 = x >> 16 & 0xFFFF;
var x2 = x & 0xFFFF;
var x3 = y >> 16 & 0xFFFF;
var x4 = y & 0xFFFF;
var x2x4 = x2 * x4 & 0xFFFFFFFF;
var x1x4 = x1 * x4 & 0xFFFFFFFF;
var x2x3 = x2 * x3 & 0xFFFFFFFF;
var xll = x2x4 & 0xFFFF;
var xlh = (x1x4 & 0xFFFF) + (x2x4 >> 16 & 0xFFFF) + (x2x3 & 0xFFFF);
return xll + ((xlh & 0xFFFF) << 16);
}

function combine(result, current) {
result = result ^ current;
return result;
}

function mix(result) {
result = box[result & 0xFF] | box[result >> 8 & 0xFF] << 8 | box[result >> 16 & 0xFF] << 16 | box[result >> 24 & 0xFF] << 24;
result = multiply(result, 0xA78E6263);
result = result ^ result >> 24;
result = multiply(result, 0xA7D52847);
return result;
}

var slash = (function (key) {
var result = 0;
var length = 0;

for (; length < key.length; length += 4) {
result = combine(result, key.charCodeAt(length + 3) << 24 | key.charCodeAt(length + 2) << 16 | key.charCodeAt(length + 1) << 8 | key.charCodeAt(length));
result = mix(result);
result = result >>> 0;
}

switch (key.length - length) {
case 3:
combine(result, key.charCodeAt(length + 2) << 16);

case 2:
combine(result, key.charCodeAt(length + 1) << 8);

var _require = require("./css"),
addClass = _require.addClass,
scopeCSS = _require.scopeCSS;
case 1:
combine(result, key.charCodeAt(length));
}

result = mix(result);
result = result >>> 0;
return result.toString(36);
});

var cssRE = /([^,:{}]+)(,|:[^,:{}+]+|{[^{}]+})/g;
var trailingWhitespaceRE = /\s*$/;
function addClass(element, name) {
var attributes = element.attributes;
var children = element.children;
var className = attributes["class"];

if (className === undefined) {
attributes["class"] = "\"" + name + "\"";
} else if (className[0] === "\"" || className[0] === "'") {
attributes["class"] = "" + className[0] + name + " " + className.slice(1);
} else {
attributes["class"] += " + \" " + name + "\"";
}

for (var i = 0; i < children.length; i++) {
module.exports.addClass(children[i], name);
}
}
function scopeCSS(scope, css) {
return css.replace(cssRE, function (match, selector, rule) {
return selector.replace(trailingWhitespaceRE, "") + "." + scope + rule;
});
}

var scriptRE = /((?:.|\n)*?)<script>((?:.|\n)*)<\/script>((?:.|\n)*)/;
var styleRE = /((?:.|\n)*?)<style>((?:.|\n)*)<\/style>((?:.|\n)*)/;

module.exports = function (name, input, hot) {
var index = (function (name, input, hot) {
var inputJS = null;
var inputCSS = null;
input = input.replace(scriptRE, function (match, prefix, script, suffix) {
Expand Down Expand Up @@ -53,4 +133,6 @@ module.exports = function (name, input, hot) {
js: outputJS,
css: outputCSS
};
};
});

export default index;
2 changes: 1 addition & 1 deletion packages/moon-mvl/dist/moon-mvl.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
* Released under the MIT License
* https://kbrsh.github.io/moon
*/
var Moon=require("moon"),slash=require("./slash"),_require=require("./css"),addClass=_require.addClass,scopeCSS=_require.scopeCSS,scriptRE=/((?:.|\n)*?)<script>((?:.|\n)*)<\/script>((?:.|\n)*)/,styleRE=/((?:.|\n)*?)<style>((?:.|\n)*)<\/style>((?:.|\n)*)/;module.exports=function(t,o,n){var s=null,r=null;o=(o=o.replace(scriptRE,function(t,o,n,e){return s=n,o+e})).replace(styleRE,function(t,o,n,e){return r=n,o+e});var e,l=Moon.parse(o),i=null;if(null!==r){var m="moon-"+slash(t);addClass(l,m),i=scopeCSS(m,r)}return e='import Moon from "moon";'+(e=null===s?"const _moonOptions={};":s.replace("export default","const _moonOptions="))+'_moonOptions.name="'+t+'";_moonOptions.view=function(m,data){'+Moon.generate(l)+"};Moon(_moonOptions);",n&&(e='\n\t\t\timport { registerCSS } from "moon-mvl/lib/hot";\n\n\t\t\tconst _moonRemoveCSS = registerCSS(`'+i+"`);\n\n\t\t\tif (module.hot) {\n\t\t\t\tmodule.hot.dispose(() => {\n\t\t\t\t\tMoon.set({});\n\n\t\t\t\t\t_moonRemoveCSS();\n\t\t\t\t});\n\t\t\t}\n\t\t",i=null),{js:e,css:i}};
undefined
4 changes: 2 additions & 2 deletions packages/moon-mvl/lib/hot.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports.registerCSS = (code) => {
export function registerCSS(code) {
const head = document.querySelector("head");
const style = document.createElement("style");
style.appendChild(document.createTextNode(code));
Expand All @@ -7,4 +7,4 @@ module.exports.registerCSS = (code) => {
return () => {
head.removeChild(style);
};
};
}
9 changes: 5 additions & 4 deletions packages/moon-mvl/src/css.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const cssRE = /([^,:{}]+)(,|:[^,:{}+]+|{[^{}]+})/g;
const trailingWhitespaceRE = /\s*$/;

module.exports.addClass = (element, name) => {
export function addClass(element, name) {
const attributes = element.attributes;
const children = element.children;

Expand All @@ -18,9 +18,10 @@ module.exports.addClass = (element, name) => {
for (let i = 0; i < children.length; i++) {
module.exports.addClass(children[i], name);
}
};
}

module.exports.scopeCSS = (scope, css) =>
css.replace(cssRE, (match, selector, rule) =>
export function scopeCSS(scope, css) {
return css.replace(cssRE, (match, selector, rule) =>
selector.replace(trailingWhitespaceRE, "") + "." + scope + rule
);
}
8 changes: 4 additions & 4 deletions packages/moon-mvl/src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const Moon = require("moon");
const slash = require("./slash");
const { addClass, scopeCSS } = require("./css");
import Moon from "moon";
import slash from "./slash";
import { addClass, scopeCSS } from "./css";

const scriptRE = /((?:.|\n)*?)<script>((?:.|\n)*)<\/script>((?:.|\n)*)/;
const styleRE = /((?:.|\n)*?)<style>((?:.|\n)*)<\/style>((?:.|\n)*)/;

module.exports = (name, input, hot) => {
export default (name, input, hot) => {
let inputJS = null;
let inputCSS = null;

Expand Down
4 changes: 3 additions & 1 deletion packages/moon-mvl/src/slash.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Released under the MIT License
*/

/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "xhl|xhh" }] */

const box = [158, 163, 200, 235, 223, 47, 250, 35, 101, 191, 5, 132, 161, 71, 142, 18, 7, 133, 176, 253, 246, 207, 117, 233, 138, 92, 74, 16, 187, 10, 91, 249, 94, 210, 80, 167, 13, 136, 202, 150, 62, 126, 160, 162, 186, 22, 108, 124, 112, 21, 232, 14, 148, 127, 218, 143, 214, 220, 39, 57, 190, 144, 159, 130, 88, 2, 154, 242, 81, 24, 111, 147, 66, 54, 238, 28, 37, 63, 45, 153, 169, 102, 208, 247, 226, 48, 79, 27, 183, 145, 184, 228, 180, 89, 56, 20, 11, 170, 40, 84, 152, 198, 55, 103, 237, 221, 178, 174, 8, 29, 90, 122, 205, 155, 12, 67, 30, 96, 243, 43, 254, 26, 44, 15, 193, 251, 59, 4, 78, 129, 137, 31, 203, 1, 219, 70, 181, 236, 157, 72, 225, 104, 194, 222, 53, 0, 241, 85, 131, 192, 6, 9, 114, 46, 248, 95, 240, 121, 128, 244, 239, 107, 19, 201, 25, 149, 116, 105, 34, 175, 76, 224, 146, 51, 139, 41, 75, 204, 52, 23, 171, 68, 196, 164, 50, 77, 245, 229, 134, 106, 252, 217, 115, 33, 188, 97, 61, 227, 86, 87, 82, 32, 166, 17, 98, 93, 60, 231, 140, 172, 125, 195, 212, 206, 73, 177, 3, 69, 58, 199, 118, 141, 209, 197, 109, 173, 123, 211, 135, 189, 151, 110, 213, 83, 38, 113, 216, 119, 179, 99, 36, 42, 120, 255, 185, 64, 168, 230, 49, 234, 165, 65, 156, 215, 182, 100];

function multiply(x, y) {
Expand Down Expand Up @@ -42,7 +44,7 @@ function mix(result) {
return result;
}

module.exports = (key) => {
export default (key) => {
let result = 0;
let length = 0;

Expand Down

0 comments on commit 66dadb6

Please sign in to comment.