Feature request - 'mix()' function for javascript
See original GitHub issueIssue: There are situations in a project where I need to reference a versioned file in a javascript file. I could of course get the mix-manifest.json
file contents and print that to the page as a javascript object, then on the client side I could lookup the correct versioned path but this is a bit messy.
Solution: It would be nicer if perhaps there was a function like laravel’s mix()
function but for javascript. Perhaps something like below:
//laravel-mix.js
window.laravelMix = function($assetUrl) {
var json = require('../../../public/mix-manifest.json');
for (var prop in json) if($assetUrl == prop) return json[prop];
return $assetUrl;
};
// Usage
laravelMix('/css/base.css'); // OUTPUTS: /css/base.fdsa89f0dsa890fdsa890.css
// webpack.mix.js
mix.js('resources/assets/js/laravel-mix.js', 'public/js/laravel-mix.js')
Unfortunately this won’t work as laravel-mix.js
requires
mix-manifest.json
before mix-manifest.json
is updated meaning its contents is outdated.
Alternatively, perhaps creating a webpack plugin that transforms the paths to versioned paths would be better. Unfortunately I’m not that good with webpack so I wouldn’t know how to do this.
Issue Analytics
- State:
- Created 6 years ago
- Comments:5
Top GitHub Comments
We would also like to have mix function in javascript. We have the same issue that we need a versioned file in a javascript file. It would be very nice to have it!
Hi! 👋
Long time Laravel Project user; first time Stand-Alone Project user.
I just built a non-Laravel SPA that used Mix’s versioning and hmr features.
I implemented my own JavaScript
mix()
helper by porting Laravel’sIlluminate\Foundation\Mix
class from PHP to JavaScript, then compiling it into my HTML template vialodash.template
each time Mix dispatches abuild
event.Here’s my simplified HTML template…
Here’s my partial
webpack.mix.js
file, withversion()
toggle andbuild
event listener…Here’s my JavaScript
mix()
helper, ported from Laravel’s PHPMix
class…With my JavaScript
mix()
helper implemented, running the followingnpm
commands produce the resulting HTML template output…npm run dev
,npm run watch
, etcnpm run prod
npm run hot
I hope this approach helps others looking for a similar end result.
Cheers!
🤓👍