-
Notifications
You must be signed in to change notification settings - Fork 916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: compatible with webpack5 externals script #1774
base: master
Are you sure you want to change the base?
Conversation
@@ -176,7 +176,11 @@ var component = normalizer( | |||
} | |||
|
|||
if (needsHotReload) { | |||
code += `\n` + genHotReloadCode(id, hasFunctional, templateRequest) | |||
code = [ | |||
`import Vue from 'vue'`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it have to be import
? I'm not sure if it would break some CommonJS code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if it would break some CommonJS code.
I'm not sure what 'some CommonJS code' means here
As far as I know, import
in webpack can handle both ES Module and CommonJS
import Vue from 'vue/dist/vue.esm.js'
console.log(Vue.version) // works
import Vue from 'vue/dist/vue.common.js'
console.log(Vue.version) //works
but require
is different
const Vue = require('vue/dist/vue.esm.js')
console.log(Vue.default.version)
const Vue = require('vue/dist/vue.common.js')
console.log(Vue.version)
So Vue = vue.__esModule ? vue.default : vue
in vue-hot-reload-api
is necessary for require('vue')
. But it's redundant for import Vue from 'vue'
, since webpack will check __esModule
in generated code and import Vue as expected.
Therefore, I think import Vue form 'vue'
won't break codes below
var api = require(`path_to_vue-hot-reload-api`)// or import api from 'path_to_vue-hot-reload-api'
api.install(...)
Does it have to be import?
From the documentation, externalsType: 'script'
works fine with import
.
And it can work with require
like example below. Promise fulfilled after the script is loaded.
require('vue').then(Vue => {
})
When using import Vue from 'vue'
webpack will handle the Promise, I think it's because ESModule can be statically analyzed. Compared to require
, webpack can do more for import
at the compile stage.
I didn't see externalsType: script
related discussions, maybe I can get more information from source code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try this (in HelloWorld.vue
):
import Vue from 'vue' // eslint-disable-line
module.exports = {
name: 'HelloWorld',
props: {
msg: String
}
}
vs
module.exports = {
name: 'HelloWorld',
props: {
msg: String
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh sorry, I forgot it is in another module rather than the script module…
Any news? |
fixes vuejs/vue-cli#6149 when using Vue2 with vue-loader
I have added test to verify that
api.install(Vue)
will be executed correctly