-
-
Notifications
You must be signed in to change notification settings - Fork 364
[Vue] Allow passing multiple contexts #461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,18 +9,21 @@ | |
|
||
'use strict'; | ||
|
||
export function registerVueControllerComponents(context: __WebpackModuleApi.RequireContext) { | ||
export function registerVueControllerComponents(contexts: any) { | ||
const vueControllers: { [key: string]: object } = {}; | ||
|
||
const importAllVueComponents = (r: __WebpackModuleApi.RequireContext) => { | ||
r.keys().forEach((key) => (vueControllers[key] = r(key).default)); | ||
}; | ||
|
||
importAllVueComponents(context); | ||
[].concat(contexts).forEach((context: __WebpackModuleApi.RequireContext) => importAllVueComponents(context)); | ||
|
||
// Expose a global Vue loader to allow rendering from the Stimulus controller | ||
(window as any).resolveVueComponent = (name: string): object => { | ||
const component = vueControllers[`./${name}.vue`]; | ||
const component = Object.values( | ||
Object.fromEntries(Object.entries(vueControllers).filter(([key]) => key.endsWith(`${name}.vue`))) | ||
)[0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tbh, this makes my head spin a little bit. If I import 2 contexts, and each contains a With the new code, is the "key" inside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would then not be possible or just very hacky with the way require context works. I personally think that a project should never have 2 components with the same name and that the component names should always be very explicit and never as generic as just Menu. (https://vuejs.org/style-guide/rules-essential.html) But if this should be possible, I think we can close the pull request for now? |
||
|
||
if (typeof component === 'undefined') { | ||
throw new Error(`Vue controller "${name}" does not exist`); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<template> | ||
<h1>This entry is called {{ entry }}</h1> | ||
</template> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we: