How do I cache bust imported modules in es6?

后端 未结 7 1090
梦谈多话
梦谈多话 2021-01-31 09:21

ES6 modules allows us to create a single point of entry like so:

7条回答
  •  终归单人心
    2021-01-31 09:40

    From my point of view dynamic imports could be a solution here.

    Step 1) Create a manifest file with gulp or webpack. There you have an mapping like this:

    export default {
        "/vendor/lib-a.mjs": "/vendor/lib-a-1234.mjs",
        "/vendor/lib-b.mjs": "/vendor/lib-b-1234.mjs"
    };
    

    Step 2) Create a file function to resolve your paths

    import manifest from './manifest.js';
    
    const busted (file) => {
     return manifest[file];
    };
    
    export default busted;
    

    Step 3) Use dynamic import

    import busted from '../busted.js';
    
    import(busted('/vendor/lib-b.mjs'))
      .then((module) => {
        module.default();
    });
    

    I give it a short try in Chrome and it works. Handling relative paths is tricky part here.

提交回复
热议问题