zhoukekestar / notes

My Personal Notes.
https://zhoukekestar.github.io/notes/
MIT License
17 stars 8 forks source link

使用Vue编写非单页应用 (Code non-SPA application in Vue) #31

Closed zhoukekestar closed 7 years ago

zhoukekestar commented 7 years ago

https://zhoukekestar.github.io/notes/2017/07/10/how-to-write-non-spa-with-vue.html

zhoukekestar commented 7 years ago

源码中的vue-loader 版本需要 12.0.1, 用了13.0.1的,是不行,踩坑特别提醒。具体看源码。

看了最新的 vue-loader后,发现这个webpack 的一个更新,需要在插件中,更换成factory().default:

class UMDPlugin {

  apply(compiler) {
    compiler.plugin('emit', (compilation, callback) => {
      Object.keys(compilation.assets).map(file => {
        if (/\.js$/.test(file)) {
          let source = `${compilation.assets[file].source()}`;
          source = source.replace(/root\["(\S*?)"\] = factory\(\);/g, (s, name) => `root.Vue && root.Vue.component("${name.replace(/\S*?\\/, '')}", factory().default);`);

          compilation.assets[file] = {
            source: () => source,
            size: () => source.length,
          };
        }
      });

      callback();
    });
  }
}