Skip to content

Commit

Permalink
webcomponents examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mbasso committed Dec 28, 2017
1 parent 7e81c8f commit cd53649
Show file tree
Hide file tree
Showing 26 changed files with 703 additions and 0 deletions.
17 changes: 17 additions & 0 deletions examples/webcomponents - cpp/.babelrc
@@ -0,0 +1,17 @@
{
"presets": ["es2015", "stage-0"],
"plugins": [
"transform-object-rest-spread",
"transform-es3-member-expression-literals",
"transform-es3-property-literals"
],
"env": {
"commonjs": {
"plugins": [
["transform-es2015-modules-commonjs", { "loose": true }]]
},
"es": {
"plugins": []
}
}
}
15 changes: 15 additions & 0 deletions examples/webcomponents - cpp/.editorconfig
@@ -0,0 +1,15 @@
# EditorConfig helps developers define and maintain
# consistent coding styles between different editors and IDEs.

root = true

[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
5 changes: 5 additions & 0 deletions examples/webcomponents - cpp/.eslintignore
@@ -0,0 +1,5 @@
**/dist/**
**/node_modules/**
**/compiled/**
**/src/prefix.js
**/src/postfix.js
34 changes: 34 additions & 0 deletions examples/webcomponents - cpp/.eslintrc
@@ -0,0 +1,34 @@
{
"ecmaFeatures": {
"modules": true
},
"env": {
"browser": true,
"node": true
},
"parser": "babel-eslint",
"rules": {
"global-require": "off",
"object-shorthand": "off",
"no-mixed-operators": "off",
"no-plusplus": "off",
"no-continue": "off",
"dot-notation": "off",
"no-restricted-syntax": "off",
"quote-props": "off",
"no-underscore-dangle": "off",
"no-param-reassign": "off",
"valid-jsdoc": 2,
"quotes": [2, "single"],
"strict": [2, "never"]
},
"extends": "airbnb/base",
"globals": {
"describe" : false,
"it" : false,
"before" : false,
"beforeEach" : false,
"after" : false,
"afterEach" : false
}
}
12 changes: 12 additions & 0 deletions examples/webcomponents - cpp/.gitignore
@@ -0,0 +1,12 @@
node_modules
.DS_Store
*.log
lib
es
coverage
_book
.nyc_output
.vscode
package-lock.json
dist
compiled
21 changes: 21 additions & 0 deletions examples/webcomponents - cpp/LICENSE.md
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Matteo Basso

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
12 changes: 12 additions & 0 deletions examples/webcomponents - cpp/index.html
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>asm-dom • webcomponents</title>
</head>
<body>
<div id="root"></div>
<script src="bundle.js"></script>
</body>
</html>
45 changes: 45 additions & 0 deletions examples/webcomponents - cpp/package.json
@@ -0,0 +1,45 @@
{
"name": "asm-dom-webcomponents-cpp",
"version": "0.0.1",
"description": "asm-dom and webcomponents example",
"scripts": {
"clean": "rimraf lib dist es .nyc_output compiled",
"compile": "npm run clean && mkdir -p compiled && npm run compile:cpp && npm run compile:asmjs && npm run compile:wasm",
"compile:cpp": "emcc -O3 -Wall -Werror --bind ../../src/cpp/asm-dom.cpp src/index.cpp -o compiled/app.bc && emcc -O3 -Wall -Werror --bind src/index.cpp -o compiled/app.o",
"compile:asmjs": "mkdir -p compiled/asmjs && emcc -O3 --bind --memory-init-file 0 --llvm-lto 3 --llvm-opts 3 --js-opts 1 --closure 1 --pre-js src/prefix.js --post-js src/postfix.js -s \"EXPORTED_RUNTIME_METHODS=['UTF8ToString']\" -s AGGRESSIVE_VARIABLE_ELIMINATION=1 -s ELIMINATE_DUPLICATE_FUNCTIONS=1 -s ABORTING_MALLOC=1 -s NO_EXIT_RUNTIME=1 -s NO_FILESYSTEM=1 -s DISABLE_EXCEPTION_CATCHING=2 compiled/app.bc -o compiled/asmjs/app.asm.js",
"compile:wasm": "mkdir -p compiled/wasm && emcc -O3 --bind --memory-init-file 0 --llvm-lto 3 --llvm-opts 3 --js-opts 1 --closure 1 --pre-js src/prefix.js --post-js src/postfix.js -s ALLOW_MEMORY_GROWTH=1 -s \"EXPORTED_RUNTIME_METHODS=['UTF8ToString']\" -s AGGRESSIVE_VARIABLE_ELIMINATION=1 -s ABORTING_MALLOC=1 -s NO_EXIT_RUNTIME=1 -s NO_FILESYSTEM=1 -s DISABLE_EXCEPTION_CATCHING=2 -s BINARYEN=1 -s \"BINARYEN_TRAP_MODE='allow'\" compiled/app.bc -o compiled/wasm/app.js",
"start": "npm run compile && webpack-dev-server --env.dev",
"build": "npm run compile && npm run build:webpack",
"build:webpack": "cross-env BABEL_ENV=commonjs webpack --env.prod src/index.js",
"lint": "eslint src"
},
"devDependencies": {
"arraybuffer-loader": "0.2.2",
"babel-cli": "6.24.1",
"babel-core": "6.24.1",
"babel-eslint": "7.2.3",
"babel-loader": "7.0.0",
"babel-plugin-transform-es2015-modules-commonjs": "6.24.1",
"babel-plugin-transform-es3-member-expression-literals": "6.22.0",
"babel-plugin-transform-es3-property-literals": "6.22.0",
"babel-plugin-transform-object-rest-spread": "6.23.0",
"babel-preset-es2015": "6.24.1",
"babel-preset-stage-0": "6.24.1",
"babel-register": "6.24.1",
"compression-webpack-plugin": "0.4.0",
"copy-webpack-plugin": "4.0.1",
"cross-env": "4.0.0",
"css-loader": "0.28.1",
"eslint": "3.19.0",
"eslint-config-airbnb": "14.1.0",
"eslint-plugin-import": "2.2.0",
"eslint-plugin-jsx-a11y": "4.0.0",
"eslint-plugin-react": "6.10.3",
"style-loader": "0.17.0",
"webpack": "2.4.1",
"webpack-dev-server": "2.4.5"
},
"dependencies": {
"babel-polyfill": "6.23.0"
}
}
75 changes: 75 additions & 0 deletions examples/webcomponents - cpp/src/app.cpp
@@ -0,0 +1,75 @@
#include "../../../src/cpp/asm-dom.hpp"
#include <string>
#include <emscripten/val.h>

using namespace asmdom;

int main() {
Config config;
config.unsafePatch = true;
init(config);

VNode* oldVnode = h("div",
Data(
Attrs {
{"id", "root"}
}
),
Children {
h("Here is an \"Hello\" component that accepts a \"name\" attribute, and emit a \"change\" event, please open the console", true),
h("br"),
h("br"),
h("hello-component",
Data(
Attrs {
{"name", "World"}
},
Callbacks {
{"onchange", [](emscripten::val e) -> bool {
emscripten::val::global("console").call<void>("log", emscripten::val("name changed:"), e["target"]["name"]);
return true;
}}
}
)
)
}
);

patch(
emscripten::val::global("document").call<emscripten::val>(
"getElementById",
std::string("root")
),
oldVnode
);

VNode* newVnode = h("div",
Data(
Attrs {
{"id", "root"}
}
),
Children {
h("Here is an \"Hello\" component that accepts a \"name\" attribute, and emit a \"change\" event, please open the console", true),
h("br"),
h("br"),
h("hello-component",
Data(
Attrs {
{"name", "asm-dom"}
},
Callbacks {
{"onchange", [](emscripten::val e) -> bool {
emscripten::val::global("console").call<void>("log", emscripten::val("name changed:"), e["target"]["name"]);
return true;
}}
}
)
)
}
);

patch(oldVnode, newVnode);

return 0;
};
37 changes: 37 additions & 0 deletions examples/webcomponents - cpp/src/components/HelloComponent.js
@@ -0,0 +1,37 @@
class HelloComponent extends HTMLElement {
static get observedAttributes() {
return ['name'];
}

get props() {
return {
name: this.name,
};
}

get name() {
return this.getAttribute('name') || 'World';
}

set name(val) {
this.setAttribute('name', val);
}

constructor() {
super();
this.attachShadow({ mode: 'open' });
this.render();
}

attributeChangedCallback() {
this.render();
this.shadowRoot.dispatchEvent(new Event('change', { bubbles: true, composed: true }));
}

render() {
const name = this.props.name;
this.shadowRoot.textContent = `Hello ${name}!`;
}
}

customElements.define('hello-component', HelloComponent);
1 change: 1 addition & 0 deletions examples/webcomponents - cpp/src/index.cpp
@@ -0,0 +1 @@
#include "app.cpp"
23 changes: 23 additions & 0 deletions examples/webcomponents - cpp/src/index.js
@@ -0,0 +1,23 @@
import '../../../src/cpp/';
import './components/HelloComponent';

(() => {
const config = {};

new Promise(
(resolve) => {
if ('WebAssembly' in window) {
import('../compiled/wasm/app.wasm')
.then((wasm) => {
config.wasmBinary = new Uint8Array(wasm);
return import('../compiled/wasm/app.js');
})
.then(resolve);
} else {
import('../compiled/asmjs/app.asm.js').then(resolve);
}
},
)
.then(Module => Module(config));
})();

3 changes: 3 additions & 0 deletions examples/webcomponents - cpp/src/postfix.js
@@ -0,0 +1,3 @@
return Module;
};
});
11 changes: 11 additions & 0 deletions examples/webcomponents - cpp/src/prefix.js
@@ -0,0 +1,11 @@
(function (root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["default"] = factory();
else
root["default"] = factory();
})(this, function() {
return function(Module) {

0 comments on commit cd53649

Please sign in to comment.