#vite build error
1 messages · Page 1 of 1 (latest)
Hi, @mild tapir
import { createApp, h } from "vue";
import { createInertiaApp, Link, Head } from '@inertiajs/vue3';
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers';
import Layout from "./Shared/Backendlayout.vue";
createInertiaApp({
resolve: async name => {
//let page = (await import(`./Pages/${name}`)).default;
const page = await resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue'));
if (page.default.layout === undefined) {
page.default.layout = Layout;
}
return page;
},
setup({ el, App, props, plugin}) {
createApp({ render: () => h(App, props) })
.use(plugin)
.component("Link", Link)
.component("Head", Head)
.mixin({ methods: { route } })
.mixin(require('./base.js')) <-- Could you show me as an example how to do this part? I've tried many things but couldn't do it.
.mount(el);
},
progress: {
color: '#5671f0',
}
});
Could you show me as an example how to do this part? I've tried many things but couldn't do it.
You need to import the file, take a look at how imports and exports work in ESM
Where can I find this information? I don't know what ESM is. Is there a source?
https://www.digitalocean.com/community/tutorials/js-modules-es6
Basically the first hit when searching for "ESM import export"
import {usePage} from '@inertiajs/vue3'
export const __ = (key, replace = {}) => {
let translation = usePage().props.language[key]
? usePage().props.language[key]
: key
Object.keys(replace).forEach(function (key) {
translation = translation.replace(':' + key, replace[key])
});
return translation
}
I've reorganized the file in this way. Will it work if I import it like this?
import { createApp, h } from "vue";
import { createInertiaApp, Link, Head } from '@inertiajs/vue3';
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers';
import Layout from "./Shared/Backendlayout.vue";
import __ from "./basee.js"
createInertiaApp({
resolve: async name => {
//let page = (await import(`./Pages/${name}`)).default;
const page = await resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue'));
if (page.default.layout === undefined) {
page.default.layout = Layout;
}
return page;
},
setup({ el, App, props, plugin}) {
createApp({ render: () => h(App, props) })
.use(plugin)
.component("Link", Link)
.component("Head", Head)
.mixin({ methods: { route } })
//.mixin(require('./base.js'))
.mount(el);
},
progress: {
color: '#5671f0',
}
});
new error: TypeError: c.__ is not a function
You'd still need to register that method as a mixin if you want to use it as mixin. Could be just .mixin({ methods: { route, __ } })
new error : error during build:
RollupError: "default" is not exported by "resources/js/basee.js", imported by "resources/js/app.js".
Yeah, you have a named export, not a default export
what is default export.
Maybe try searching a bit. I'm not spoonfeeding on purpose, but instead giving you the information needed to solve your issue, that way you'll research and learn more than just copy-pasting.
I never copy and paste. I don't have English, I learn by trying to see how it works.
I think what you mean is this.
import {usePage} from '@inertiajs/vue3'
export const transLanguage = (key, replace = {}) => {
let translation = usePage().props.language[key]
? usePage().props.language[key]
: key
Object.keys(replace).forEach(function (key) {
translation = translation.replace(':' + key, replace[key])
});
return translation
}
export default function __() {
return transLanguage;
}
Your first example was alright, you just need to use the named import, like import { __ } from '...'
What is the difference between calling 'import __ from 'base.js';' and 'import { __ } from 'base.js';'? What do the curly braces do?
The first one references a default export (the __ is kinda irrelevant there, you can name it however you want). With the curly braces you reference a named export
It seems that when using curly braces, we specify the name of a variable or method within the file to only import what we have specified. Am I correct?
Not really, you use the name with which it's exported. If you do export const __ = ... then yes, __ would be the name of that export
I tried, if there is no export default, it imports the whole file to that name. If we use curly braces and specify the name of a variable, this time it only imports that variable.
You might want to read a bit more on what modules are, how imports and exports work, Vite basically relies heavily on it
For example Wes Bos has a whole section on the subject; https://wesbos.com/javascript/14-es-modules-and-structuring-larger-apps/78-modules