Best way to setup a SPA router with lit-element and simple-wc-router using webpack.
I currently have this working but am wondering if there is a better way to put this all together.
webpack-config.js:
module.exports = ({ mode, presets }) => {
return webpackMerge(
{
entry: {
app: './src/index.js'
},
index.js
import './app-router.js';
window.addEventListener('load', () => {
initRouter();
});
function initRouter() {
var approuter = document.createElement("app-router");
document.getElementsByTagName("BODY")[0].append(approuter);
}
app-router.js
import { LitElement, html } from 'lit-element';
import { Router } from 'simple-wc-router';
import './ui/side-panel.js';
import './admin/admin-home.js';
class AppRouter extends Router(LitElement) {
static get routes() {
return [
{
path: "/admin",
component: 'admin-home',
import: () => {
console.log('path /admin...');
import(/* webpackChunkName: "adminutils" */ './admin/admin-home');
}
},
{
path: '/admin/billing',
component: 'admin-billing',
import: () => import(/* webpackChunkName: "adminutils" */ './admin/admin-billing')
},
{
path: "*",
component: 'not-found-view',
import: () => import(/* webpackChunkName: "not-found-view" */ './views/not-found-view')
}
];
}
This currently works. But in order to use the app-router element which has the routing logic, I have to append a HTML element.
Per LitElement docs: https://lit-element.polymer-project.org/guide/use the way to use a component after importing would be
<app-router></app-router>
I could not find a way to use a web component without the html part, hence: document.getElementsByTagName("BODY")[0].append(approuter);
Is there a more elegant way to use the app-router web component?
Is there a better way to set up the routing component assuming we're using webpack?