已调试好的 Webpack 版本

  1. yarn add webpack@4.44.2 webpack-cli@3.3.12 webpack-dev-server@3.11.2 -D

package.json script 修改

  1. "scripts": {
  2. - "test": "echo \"Error: no test specified\" && exit 1"
  3. + "dev": "webpack-dev-server"
  4. },

创建目录与文件

src

  • main.js 入口文件

public

module.exports = { mode: ‘development’, entry: ‘./src/main.js’, output: { path: resolve(dirname, ‘dist’), filename: ‘main.js’, }, externals: { vue: ‘Vue’, }, devtool: ‘source-map’, module: { rules: [{ test: /.vue$/, loader: ‘vue-loader’ }], }, plugins: [ new VueLoaderPlugin(), new HtmlWebpackPlugin({ template: resolve(dirname, ‘public/index.html’), }), ], };

  1. Vue3 要修改 VueLoaderPlugin 的引入的写法,直接从 vue-loader 中解构出来
  2. ```javascript
  3. - const VueLoaderPlugin = require('vue-loader/lib/plugin');
  4. + const { VueLoaderPlugin } = require('vue-loader');

src 文件测试

App.vue

  1. <template>
  2. <div>{{ title }}</div>
  3. </template>
  4. <script>
  5. export default {
  6. name: "App",
  7. data() {
  8. return {
  9. title: "Hello, Vue!!!",
  10. };
  11. },
  12. };
  13. </script>

main.js
Vue2

  1. import App from './App.vue';
  2. new Vue({
  3. render: (h) => h(App),
  4. }).$mount('#app');

Vue3

  1. import App from './App.vue';
  2. Vue.createApp(App).mount("#app);