How to read mix-manifest.json from javascript

自闭症网瘾萝莉.ら 提交于 2020-01-25 07:18:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!