问题
I cached my images using laravel-mix. For the image in blade, it works. However I couldn't figure out how to access that versioned paths from javascript.
Is it possible to read mix-manifest.json
from compiled javascript? So that I can create a mixin and get the versioned paths
For example, in my mix-manifest.json I have
"/images/hello.svg": "/images/hello.svg?id=0360de485c68385550da",
and I thought if I can read the mix-manifest.json
from my compiled javascript, I can create a helper method like
export default {
methods: {
mixPath(path) {
let manifestObj = // read mix-manifest.json
return manifestObj[path];
}
}
}
回答1:
I could access mix-manifest.json with
const vm = this;
if (!Vue.prototype.BROWSER_BUILD) {
let path = require("path");
this.manifestPaths = JSON.parse(require('fs').readFileSync(path.resolve('./mix-manifest.json'), 'utf8'));
} else {
axios.get('/mix-manifest.json').then(json => {
vm.manifestPaths = json.data
})
}
And the method:
methods: {
mix(path) {
if (typeof this.manifestPaths[path] == 'undefined') {
return this.manifestPaths[path];
}
return path;
}
}
If you know a better way, i'd be happy to know :)
来源:https://stackoverflow.com/questions/59583374/how-to-read-mix-manifest-json-from-javascript