I currently have a CDN set up to point to my base domain with a 1:1 mapping. I am in the process of building my bundle on the server and I want to load it using the CDN URL. After running npm run build
, I am aiming for the following structure:
public/
css/
app.css
js/
index.js
1.js.gz
1.js
2.js.gz
2.js
My goal is to have these resources loaded like this on my CDN:
https://mycdn.com/public/js/index.js
https://mycdn.com/public/css/app.css
This is my current configuration in webpack.mix.js
:
mix
.sass('resources/sass/app.css', 'public/css')
.js('resources/js/index.js', 'public/js')
The files are generated in the correct location and then included in my index.blade.php
:
<script src="{{ elixirCDN('/js/index.js') }}"></script>
I have a custom function called elixirCDN to prepend the filename with the CDN URL.
function elixirCDN($file)
{
$cdn = '';
if(config('system.cdn_url'))
{
$cdn = config('system.cdn_url');
}
return $cdn . elixir($file);
}
Everything works fine until I try using dynamic imports like this:
const Home = () => import("./Home")
In an ideal situation, I want these dynamic imports to also load from the CDN:
https://mycdn.com/public/js/1.js
However, they currently load with a relative path using my base domain:
https://mybasedomain.com/public/js/1.js
I have tried setting publicPath
to my CDN URL and using setPublicPath()
, but it hasn't had any effect. How can I ensure my chunks are also loaded from the CDN?