Skip to content

Commit fb7fa34

Browse files
committed
Initial - Export Ember app as a custom Element
1 parent f5dd853 commit fb7fa34

File tree

8 files changed

+130
-7
lines changed

8 files changed

+130
-7
lines changed

addon/create-web-components.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import config from 'ember-get-config'
2+
3+
let name = config.modulePrefix;
4+
5+
// fake private method
6+
function _appendStyles(dest, styles) {
7+
let files = Object.values(styles)
8+
files.forEach((f)=> {
9+
let style = document.createElement('link')
10+
style.setAttribute('href', f)
11+
style.setAttribute('rel', 'stylesheet')
12+
dest.appendChild(style)
13+
})
14+
}
15+
16+
class ApplicationContainer extends HTMLElement {
17+
#styles = (config.webComponentsAssets || {}).styles
18+
#shadowRoot
19+
#application
20+
21+
constructor() {
22+
super()
23+
this.#shadowRoot = this.attachShadow({mode: 'closed'})
24+
// The 2 divs are a trick in how Ember finds their parent
25+
let rootParent = document.createElement('div')
26+
_appendStyles(rootParent, this.#styles)
27+
let rootElement = document.createElement('div')
28+
this.#shadowRoot.appendChild(rootParent)
29+
rootParent.appendChild(rootElement)
30+
31+
// Actually start the app
32+
let app = require(`${name}/app`).default.create({
33+
rootElement
34+
})
35+
this.#application = app
36+
}
37+
38+
// That makes the application accessible via:
39+
// document.querySelector('application-name').__EMBER_APPLICATION
40+
get __EMBER_APPLICATION() {
41+
return this.#application
42+
}
43+
}
44+
45+
let componentName = name
46+
if (-1 === config.modulePrefix.indexOf('-')) {
47+
componentName += '-app'
48+
}
49+
50+
customElements.define(componentName, ApplicationContainer)
51+
52+
53+
/**
54+
* TODO
55+
* RECOMMEND to remove export-application-global
56+
* Allow for multiple instances (clean exportApplicationGlobal)
57+
* Destruction of comonent <=> Destruction of the app
58+
* Attributes and slots
59+
* More isolation
60+
*/

index.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
'use strict';
22

33
module.exports = {
4-
name: require('./package').name
4+
name: require('./package').name,
5+
config(env, config) {
6+
if (!this.app) {
7+
return;
8+
}
9+
this.app.options.storeConfigInMeta = false;
10+
this.app.options.autoRun = false;
11+
return {
12+
webComponentsAssets : {
13+
styles: this._styles
14+
}
15+
}
16+
},
17+
included(addon) {
18+
let { options } = addon
19+
let styles = {
20+
// Why tho?? The structure of options.outputPaths is weird and inconsistent
21+
...options.outputPaths.app.css,
22+
vendor: options.outputPaths.vendor.css
23+
}
24+
25+
this._styles = styles
26+
27+
this.app.import('vendor/register-components.js')
28+
}
529
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"ember-cli-template-lint": "^1.0.0-beta.1",
3737
"ember-cli-uglify": "^2.1.0",
3838
"ember-disable-prototype-extensions": "^1.1.3",
39-
"ember-export-application-global": "^2.0.0",
39+
"ember-get-config": "^0.2.4",
4040
"ember-load-initializers": "^2.0.0",
4141
"ember-maybe-import-regenerator": "^0.1.6",
4242
"ember-qunit": "^4.4.1",

tests/dummy/app/index.html

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,21 @@
99

1010
{{content-for "head"}}
1111

12-
<link integrity="" rel="stylesheet" href="{{rootURL}}assets/vendor.css">
13-
<link integrity="" rel="stylesheet" href="{{rootURL}}assets/dummy.css">
12+
<style>
13+
h2 {
14+
font-style: italic;
15+
}
16+
17+
dummy-app {
18+
overflow: auto;
19+
}
20+
21+
.grid {
22+
display: grid;
23+
grid-template-columns: repeat(2, 1fr);
24+
grid-gap: 10px;
25+
}
26+
</style>
1427

1528
{{content-for "head-footer"}}
1629
</head>
@@ -20,6 +33,10 @@
2033
<script src="{{rootURL}}assets/vendor.js"></script>
2134
<script src="{{rootURL}}assets/dummy.js"></script>
2235

36+
<div class="grid">
37+
<dummy-app></dummy-app>
38+
<h2>Hello outside of the app</h2>
39+
</div>
2340
{{content-for "body-footer"}}
2441
</body>
2542
</html>

tests/dummy/app/styles/app.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
h2 {
2+
text-decoration: underline
3+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<h2 id="title">Welcome to Ember</h2>
1+
<h2>Hello from within the app</h2>
22

33
{{outlet}}

vendor/register-components.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
window.addEventListener("DOMContentLoaded", function() {
2+
require("ember-web-component-container/create-web-components");
3+
});

yarn.lock

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,6 +1992,14 @@ broccoli-debug@^0.6.4, broccoli-debug@^0.6.5:
19921992
symlink-or-copy "^1.1.8"
19931993
tree-sync "^1.2.2"
19941994

1995+
broccoli-file-creator@^1.1.1:
1996+
version "1.2.0"
1997+
resolved "https://registry.yarnpkg.com/broccoli-file-creator/-/broccoli-file-creator-1.2.0.tgz#27f1b25b1b00e7bb7bf3d5d7abed5f4d5388df4d"
1998+
integrity sha512-l9zthHg6bAtnOfRr/ieZ1srRQEsufMZID7xGYRW3aBDv3u/3Eux+Iawl10tAGYE5pL9YB4n5X4vxkp6iNOoZ9g==
1999+
dependencies:
2000+
broccoli-plugin "^1.1.0"
2001+
mkdirp "^0.5.1"
2002+
19952003
broccoli-filter@^1.2.2, broccoli-filter@^1.2.3:
19962004
version "1.3.0"
19972005
resolved "https://registry.yarnpkg.com/broccoli-filter/-/broccoli-filter-1.3.0.tgz#71e3a8e32a17f309e12261919c5b1006d6766de6"
@@ -2198,7 +2206,7 @@ broccoli-plugin@1.1.0:
21982206
rimraf "^2.3.4"
21992207
symlink-or-copy "^1.0.1"
22002208

2201-
broccoli-plugin@^1.0.0, broccoli-plugin@^1.2.0, broccoli-plugin@^1.2.1, broccoli-plugin@^1.3.0, broccoli-plugin@^1.3.1:
2209+
broccoli-plugin@^1.0.0, broccoli-plugin@^1.1.0, broccoli-plugin@^1.2.0, broccoli-plugin@^1.2.1, broccoli-plugin@^1.3.0, broccoli-plugin@^1.3.1:
22022210
version "1.3.1"
22032211
resolved "https://registry.yarnpkg.com/broccoli-plugin/-/broccoli-plugin-1.3.1.tgz#a26315732fb99ed2d9fb58f12a1e14e986b4fabd"
22042212
integrity sha512-DW8XASZkmorp+q7J4EeDEZz+LoyKLAd2XZULXyD9l4m9/hAKV3vjHmB1kiUshcWAYMgTP1m2i4NnqCE/23h6AQ==
@@ -3011,7 +3019,7 @@ ember-cli-babel-plugin-helpers@^1.0.0, ember-cli-babel-plugin-helpers@^1.1.0:
30113019
resolved "https://registry.yarnpkg.com/ember-cli-babel-plugin-helpers/-/ember-cli-babel-plugin-helpers-1.1.0.tgz#de3baedd093163b6c2461f95964888c1676325ac"
30123020
integrity sha512-Zr4my8Xn+CzO0gIuFNXji0eTRml5AxZUTDQz/wsNJ5AJAtyFWCY4QtKdoELNNbiCVGt1lq5yLiwTm4scGKu6xA==
30133021

3014-
ember-cli-babel@^6.0.0-beta.4, ember-cli-babel@^6.0.0-beta.7, ember-cli-babel@^6.16.0, ember-cli-babel@^6.8.1:
3022+
ember-cli-babel@^6.0.0-beta.4, ember-cli-babel@^6.0.0-beta.7, ember-cli-babel@^6.16.0, ember-cli-babel@^6.3.0, ember-cli-babel@^6.8.1:
30153023
version "6.18.0"
30163024
resolved "https://registry.yarnpkg.com/ember-cli-babel/-/ember-cli-babel-6.18.0.tgz#3f6435fd275172edeff2b634ee7b29ce74318957"
30173025
integrity sha512-7ceC8joNYxY2wES16iIBlbPSxwKDBhYwC8drU3ZEvuPDMwVv1KzxCNu1fvxyFEBWhwaRNTUxSCsEVoTd9nosGA==
@@ -3343,6 +3351,14 @@ ember-export-application-global@^2.0.0:
33433351
dependencies:
33443352
ember-cli-babel "^6.0.0-beta.7"
33453353

3354+
ember-get-config@^0.2.4:
3355+
version "0.2.4"
3356+
resolved "https://registry.yarnpkg.com/ember-get-config/-/ember-get-config-0.2.4.tgz#118492a2a03d73e46004ed777928942021fe1ecd"
3357+
integrity sha1-EYSSoqA9c+RgBO13eSiUICH+Hs0=
3358+
dependencies:
3359+
broccoli-file-creator "^1.1.1"
3360+
ember-cli-babel "^6.3.0"
3361+
33463362
ember-load-initializers@^2.0.0:
33473363
version "2.1.0"
33483364
resolved "https://registry.yarnpkg.com/ember-load-initializers/-/ember-load-initializers-2.1.0.tgz#b402815ab9c823ff48a1369b52633721987e72d4"

0 commit comments

Comments
 (0)