My problem is that when I build a single file, it handles the URL specified in the “base” parameter perfectly. However, when I build two files, each including a file named “core.js” it seems like the “base” parameter is not used. The “modulepreload” disappears from the built files. (e.g.: const scriptRel=”modulepreload”,assetsURL=function(e){return”http://localhost.test/build/”+e%7D)
Is there a way to solve this so that the shared files are also loaded with modulepreload?
My vite.config:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
const baseBuildUrl = 'http://localhost.test/build/';
const unhashedJsFiles = ["profile", "register",];
export default defineConfig({
base: baseBuildUrl,
plugins: [
laravel({
input: [
'resources/js/profile.js',
'resources/js/register.js',
],
refresh: true,
}),
],
resolve: {
alias: [
{
find: /^~(.*)$/,
replacement: '$1',
},
],
},
build: {
rollupOptions: {
output: {
entryFileNames: function (file) {
return unhashedJsFiles.includes(file.name)
? 'assets/[name].js'
: 'assets/[name]-[hash].js';
},
chunkFileNames: function (file) {
return unhashedJsFiles.includes(file.name)
? 'assets/[name].js'
: 'assets/[name]-[hash].js';
},
}
}
},
});
Every input file imports a core.js file.
import coreApp from './core.js';
This is necessary because both of my input files can be used independently, but they need to contain the core elements.
This is how I use my built files (which is perfect for a single input file)
This request is processed by a backend server, which responds with the content of the appropriate JS file:
<script src="https://localhost.test/v1/js/register?id=myid"></script>
<script type="module">initRegister("param1", "param2");</script>
When I have only one input file, the beginning of that file looks like this:
const __vite__fileDeps=["assets/register-DYYaF7I7.css"],__vite__mapDeps=i=>i.map(i=>__vite__fileDeps[i]);
const scriptRel="modulepreload",assetsURL=function(e){return"http://localhost.test/build/"+e},seen={},__vitePreload=function(n,t,r)
Since I have multiple inputs, this changes and the assets URL disappears from it:
const __vite__fileDeps=["assets/register-DYYaF7I7.css"],__vite__mapDeps=i=>i.map(i=>__vite__fileDeps[i]);
import{_ as b,g as i}from"./core-CiAPw-a5.js";b(()=>Promise.resolve({}),__vite__mapDeps([0]));let s=null,c=null,l=null;