|
| 1 | +# Migration guide from `http-vue-loader` to `vue3-sfc-loader` |
| 2 | + |
| 3 | +First of all, `http-vue-loader` and `vue3-sfc-loader` serves the same need: run `.vue` files client-side without requiring any bundler. |
| 4 | + |
| 5 | + |
| 6 | +## main differences between `http-vue-loader` and `vue3-sfc-loader` (for Vue2) |
| 7 | + |
| 8 | +| | `http-vue-loader` | `vue3-sfc-loader` | |
| 9 | +|-:|:-:|:-:| |
| 10 | +| supports Vue2 | yes | yes | |
| 11 | +| supports Vue3 | no | yes | |
| 12 | +| supports ES6 | no | yes | |
| 13 | +| `fetch` resources asynchronously | no | yes | |
| 14 | +| uses native vue compiler | no | yes | |
| 15 | +| scoped style support | basic | full | |
| 16 | +| handle http requests by default | yes | no | |
| 17 | +| handle CSS injection by default | yes | no | |
| 18 | +| supports nested import/require | no | yes | |
| 19 | +| recommended for production | no | yes | |
| 20 | +| bundle size (min gz) | 2.7KB | 382KB | |
| 21 | + |
| 22 | + |
| 23 | +## example from `http-vue-loader` to `vue3-sfc-loader` |
| 24 | + |
| 25 | +#### `http-vue-loader` |
| 26 | + |
| 27 | +``` |
| 28 | +<html> |
| 29 | + <head> |
| 30 | + <script src="https://unpkg.com/vue"></script> |
| 31 | + <script src="https://unpkg.com/http-vue-loader"></script> |
| 32 | + </head> |
| 33 | + <body> |
| 34 | + <div id="app"> |
| 35 | + <my-component></my-component> |
| 36 | + </div> |
| 37 | +
|
| 38 | + <script> |
| 39 | + new Vue({ |
| 40 | + el: '#app', |
| 41 | + components: { |
| 42 | + 'my-component': httpVueLoader('my-component.vue') |
| 43 | + } |
| 44 | + }); |
| 45 | + </script> |
| 46 | + </body> |
| 47 | +</html> |
| 48 | +``` |
| 49 | + |
| 50 | + |
| 51 | +#### `vue3-sfc-loader` |
| 52 | + |
| 53 | +``` |
| 54 | +<html> |
| 55 | + <head> |
| 56 | + <script src="https://unpkg.com/vue"></script> |
| 57 | + <script src="https://unpkg.com/vue3-sfc-loader/dist/vue2-sfc-loader.js"></script> |
| 58 | + </head> |
| 59 | + <body> |
| 60 | + <div id="app"> |
| 61 | + <my-component></my-component> |
| 62 | + </div> |
| 63 | +
|
| 64 | + <script> |
| 65 | +
|
| 66 | + const { loadModule } = window['vue2-sfc-loader']; |
| 67 | +
|
| 68 | + const options = { |
| 69 | + moduleCache: { |
| 70 | + vue: Vue, |
| 71 | + }, |
| 72 | + getFile(filepath) { |
| 73 | + |
| 74 | + fetch(filepath).then(res => { |
| 75 | +
|
| 76 | + if ( !res.ok ) |
| 77 | + throw Object.assign(new Error(res.statusText + ' ' + url), { res }); |
| 78 | + return { |
| 79 | + getContentData: asBinary => asBinary ? res.arrayBuffer() : res.text(), |
| 80 | + } |
| 81 | + }); |
| 82 | + }, |
| 83 | + addStyle(textContent) { |
| 84 | +
|
| 85 | + const style = Object.assign(document.createElement('style'), { textContent }); |
| 86 | + const ref = document.head.getElementsByTagName('style')[0] || null; |
| 87 | + document.head.insertBefore(style, ref); |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + new Vue({ |
| 92 | + el: '#app', |
| 93 | + components: { |
| 94 | + 'my-component': () => loadModule('/my-component.vue', options) |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + </script> |
| 99 | + </body> |
| 100 | +</html> |
| 101 | +
|
| 102 | +``` |
0 commit comments