Closed jgauna closed 1 year ago
Hi @jgauna!
Could you please provide an example repo with this issue, including all the files you've sent and also including the components?
It is likely not the dist structure, Vite rewrites that structure to be flat and your main.js likely had the correct URLs. I'll be able to dig into it and see why it may not be working with a test repo though.
Thanks!
Hey @Accudio thanks for the answer :)
If anyone is struggling with this, you should know that as @Accudio says, Vite creates a flat dist
folder with all of the chunks from your project, so you can't have the same folder structure as in dev mode. The solution is to use Resolve.alias in order to resolve the module's paths from your project. So, in your vite.config.js
you should have something like this (read the code's comment) π
//import {VitePWA} from 'vite-plugin-pwa';
import {defineConfig} from 'vite';
import {compression} from 'vite-plugin-compression2';
import {splitVendorChunkPlugin} from 'vite';
import compress from 'compression';
import {VitePluginFonts} from 'vite-plugin-fonts';
import VitePluginBrowserSync from 'vite-plugin-browser-sync';
import PathEnv from 'vite-plugin-patch-env';
import mockData from 'vite-plugin-mock-data';
import VitePluginHtmlEnv from 'vite-plugin-html-env';
import path from 'path';
// Icons
import Icons from 'unplugin-icons/vite';
import IconsResolver from 'unplugin-icons/resolver';
import AutoImport from 'unplugin-auto-import/vite';
export default defineConfig({
root: './',
base: './',
publicDir: 'public',
// Here's the fix for loading modules when building for prod πππ
resolve: {
alias: {
'@home': path.resolve(__dirname, './js/components/Home')
}
},
build: {
emptyOutDir: true,
rollupOptions: {
output: {
chunkFileNames: 'assets/js/[name]-[hash].js',
entryFileNames: 'assets/js/[name]-[hash].js',
assetFileNames: ({name}) => {
if (/\.(gif|jpe?g|png|svg)$/.test(name ?? '')) {
return 'assets/img/[name]-[hash][extname]';
}
if (/\.css$/.test(name ?? '')) {
return 'assets/css/[name]-[hash][extname]';
}
return 'assets/[name]-[hash][extname]';
}
}
}
},
css: {
preprocessorOptions: {
scss: {
includePaths: ['./assets/css']
}
}
},
plugins: [
splitVendorChunkPlugin(),
VitePluginFonts({
google: {
families: ['Josefin Sans']
}
}),
VitePluginHtmlEnv({
compiler: true
}),
mockData({
mockAssetsDir: './mockAssets'
}),
PathEnv(),
compression(),
AutoImport({
resolvers: [
IconsResolver({
prefix: false,
extension: 'raw',
enabledCollections: ['carbon', 'mdi']
})
]
}),
Icons({
autoInstall: true,
compiler: 'raw',
scale: 1.2, // Scale of icons against 1em
defaultStyle: '', // Style apply to icons
defaultClass: '' // Class names apply to icons
}),
VitePluginBrowserSync({
bs: {
reloadDelay: 500,
notify: false,
ghostMode: false
}
})
]
});
And your main.js
AsyncAlpine.init(Alpine)
// Same as alias key in vite.config.js π
.data('homeModule', () => import('@home/home.js'))
.start()
And if you want to make it more awesome you could use https://github.com/Subwaytime/vite-aliases which creates all aliases automatically according to your project structure.
@Accudio maybe you can update your example so it's well documented. Btw, fantastic work, I love this lib :)
@jgauna Glad you worked it out and thank you!
I've added a comment to the Vite example with a link to your solution above. Since it doesn't affect the base Vite implementation without configuration that seems like a good solution to me. If it ends up being a more common issue (I don't really use Vite so am not sure) then I'll make a page in the docs.
Thanks!
Hi! I've been trying to build a project using AsyncAlpine + Vite for the last two days with no luck. I'm writing because I have no idea how to accomplish this.
This is my folder structure:
This is my vite.config.js:
This is my main.js:
This is my index.html:
When I run
vite build && vite preview
I got:And I think this problem is related to the dist structure:
Since every module is in
assets/js/components
the dist index.html couldn't find the module because it's not the same folder structure as dev mode. Any thoughts?Regards