-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from joekrump/experiment-with-dynamic-imports-…
…of-all-vue-components Experiment with dynamic imports of all vue components
- Loading branch information
Showing
13 changed files
with
180 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import PandaInfo from "@/entrypoints/views/pandas/index/PandaInfo.vue"; | ||
import Zap from "@/entrypoints/views/pandas/index/Zap.vue"; | ||
import { mountComponent } from "@/helpers/mount-component"; | ||
|
||
const mountPandasInfo = () => mountComponent("#pandas-view", PandaInfo); | ||
const mountZap = () => mountComponent("#lazy-load", Zap); | ||
|
||
export { mountPandasInfo, mountZap }; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import RootApp from "@/entrypoints/views/root/RootApp.vue"; | ||
import { mountComponent } from "@/helpers/mount-component"; | ||
|
||
const mountRootApp = () => { | ||
const componentDependencies = { | ||
WelcomeItem: () => import("@/components/WelcomeItem.vue"), | ||
IconDocumentation: () => import("@/components/icons/IconDocumentation.vue"), | ||
IconTooling: () => import("@/components/icons/IconTooling.vue"), | ||
IconEcosystem: () => import("@/components/icons/IconEcosystem.vue"), | ||
IconCommunity: () => import("@/components/icons/IconCommunity.vue"), | ||
}; | ||
|
||
return mountComponent("#root-view", RootApp, componentDependencies); | ||
}; | ||
|
||
export { mountRootApp }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { AsyncComponentLoader, Component } from "vue"; | ||
import { createApp, type App, defineAsyncComponent } from "vue"; | ||
|
||
export function mountComponent( | ||
querySelector: string, | ||
component: Component, | ||
componentDependencies?: Record<string, AsyncComponentLoader<Component>> | ||
) { | ||
const rootContainer = document.querySelector(querySelector) as HTMLElement; | ||
let app: App | undefined; | ||
|
||
if (rootContainer !== null) { | ||
const props = rootContainer.dataset.props; | ||
app = createApp(component, props ? JSON.parse(props) : undefined); | ||
|
||
if (componentDependencies !== undefined) { | ||
globallyRegisterComponentsOnApp(app, componentDependencies); | ||
} | ||
|
||
app.mount(rootContainer); | ||
localStorage.removeItem("dynamic import failed count"); | ||
} else { | ||
console.error("No container found for Vue component: ", querySelector); | ||
} | ||
|
||
return app; | ||
} | ||
|
||
function globallyRegisterComponentsOnApp( | ||
app: App, | ||
components: Record<string, AsyncComponentLoader<Component>> | ||
) { | ||
for (const [name, asyncComponentLoader] of Object.entries(components)) { | ||
app.component(name, defineAsyncComponent(asyncComponentLoader)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { App } from "vue"; | ||
|
||
type AsyncAppMounter = () => Promise<() => App | undefined>; | ||
|
||
export type { AsyncAppMounter } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import <%= capitalize_name %> from "@/entrypoints/views/<%= route_path %>/<%= capitalize_name %>.vue"; | ||
import { mountComponent } from "@/helpers/mount-component"; | ||
|
||
const mount<%= capitalize_name %> = () => { | ||
// optional: add any components that this app depends on here. They will be | ||
// globally available to this app. | ||
// | ||
// Example: | ||
// const componentDependencies = { | ||
// WelcomeItem: () => import("@/components/WelcomeItem.vue"), | ||
// }; | ||
// return mountComponent("#vue-root", <%= capitalize_name %>, componentDependencies); | ||
|
||
return mountComponent("#vue-root", <%= capitalize_name %>); | ||
}; | ||
|
||
export { mount<%= capitalize_name %> }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 24 additions & 5 deletions
29
test/lib/generators/templates/rails_app/app/frontend/helpers/routes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,27 @@ | ||
const RootApp = async () => | ||
(await import("@/entrypoints/views/root/App.vue")).default; | ||
const mountRootApp = async () => | ||
(await import("@/entrypoints/views/root/mount-apps").mountRootApp); | ||
|
||
const routes = { | ||
"/": [["#root-view", RootApp]], | ||
|
||
const routes: { | ||
[routePath: string]: { | ||
[elementIdSelector: string]: AsyncAppMounter; | ||
}; | ||
} = { | ||
"/": { | ||
"#root-view": mountRootApp, | ||
}, | ||
// new route entry here | ||
}; | ||
|
||
export const getVueComponents = (url: string) => routes[url]; | ||
export const getVueComponents = ( | ||
url: string | ||
): { [elementIdSelector: string]: AsyncAppMounter } => { | ||
const pattern = /^\/((?:[^\/]+\/)*[^\/]+)\/\d+(\/\w+)?$/; | ||
const result = url.match(pattern); | ||
|
||
if (result !== null) { | ||
url = result[2] ? `/${result[1]}/:id${result[2]}` : `/${result[1]}/:id`; | ||
} | ||
|
||
return routes[url]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters