From 36cc6ebd32462ef4cba870ceb10cdbdc4d99318c Mon Sep 17 00:00:00 2001 From: fangbinwei Date: Sun, 20 Dec 2020 21:40:15 +0800 Subject: [PATCH] fix: compatible with webpack5 externals script fixes https://github.com/vuejs/vue-cli/issues/6149 --- lib/codegen/hotReload.js | 2 +- lib/index.js | 6 +++++- test/core.spec.js | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/codegen/hotReload.js b/lib/codegen/hotReload.js index a1315a89d..b37a04982 100644 --- a/lib/codegen/hotReload.js +++ b/lib/codegen/hotReload.js @@ -16,7 +16,7 @@ exports.genHotReloadCode = (id, functional, templateRequest) => { /* hot reload */ if (module.hot) { var api = require(${hotReloadAPIPath}) - api.install(require('vue')) + api.install(Vue) if (api.compatible) { module.hot.accept() if (!api.isRecorded('${id}')) { diff --git a/lib/index.js b/lib/index.js index 7760df47b..e2a27b024 100644 --- a/lib/index.js +++ b/lib/index.js @@ -176,7 +176,11 @@ var component = normalizer( } if (needsHotReload) { - code += `\n` + genHotReloadCode(id, hasFunctional, templateRequest) + code = [ + `import Vue from 'vue'`, + code, + genHotReloadCode(id, hasFunctional, templateRequest) + ].join('\n') } // Expose filename. This is used by the devtools and Vue runtime warnings. diff --git a/test/core.spec.js b/test/core.spec.js index 84a5af1d5..da3526033 100644 --- a/test/core.spec.js +++ b/test/core.spec.js @@ -28,6 +28,20 @@ test('basic', done => { }) }) +test('hot reload code run', done => { + mockBundleAndRun({ + entry: 'basic.vue', + plugins: [ + // bundle with hot-reload code + new (require('webpack').HotModuleReplacementPlugin)() + ] + }, ({ jsdomError }) => { + // run code generated by 'genHotReloadCode' without error + expect(jsdomError).toBeUndefined() + done() + }) +}) + test('pre-processors', done => { mockBundleAndRun({ entry: 'pre.vue',