Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

fangbinwei
Copy link

@fangbinwei fangbinwei commented Dec 20, 2020

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

@@ -176,7 +176,11 @@ var component = normalizer(
}

if (needsHotReload) {
code += `\n` + genHotReloadCode(id, hasFunctional, templateRequest)
code = [
`import Vue from 'vue'`,
Copy link
Member

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.

Copy link
Author

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

Copy link
Member

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
  }
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the import statement added by vue-loader-v15 doesn't conflict with the sciprt in HelloWorld.vue?

image

some problem I find

  • webpack will generated some code to await the script tag resolve, it doesn't compatible with ie11 (Need to investigate)

Copy link
Member

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…

@xuemanchi
Copy link

Any news?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

not able to use external "vue" script
3 participants