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

@vue/cli 4.0.5 transpileDependencies invalid #4773

Closed
ghost opened this issue Oct 30, 2019 · 15 comments · Fixed by #4797
Closed

@vue/cli 4.0.5 transpileDependencies invalid #4773

ghost opened this issue Oct 30, 2019 · 15 comments · Fixed by #4797

Comments

@ghost
Copy link

ghost commented Oct 30, 2019

Version

4.0.5

Reproduction link

https://github.com/lxfu/vue-g6

Environment info

Mac、IE11

Steps to reproduce

npm i
npm run serve

What is expected?

app should work under IE11.

What is actually happening?

The arrow function inside the dependency is not compiled


IE11 does not support arrow functions

@LinusBorg
Copy link
Member

LinusBorg commented Oct 30, 2019

Your reprodution doesn't contain any reference to transpileDepenencies.

You likely forgot to push the changes?

@ghost
Copy link
Author

ghost commented Oct 30, 2019

sorry! submitted.

@ghost
Copy link
Author

ghost commented Oct 30, 2019

The problem with the arrow function has been solved, because i uesd tnpm, but new problems have arisen, TypeError.

@ghost
Copy link
Author

ghost commented Oct 30, 2019

After adding transpileDepenencies, @antv/g6 It doesn't work on chrome.

@haoqunjiang
Copy link
Member

Workaround: set babel.config.js to:

module.exports = {
  sourceType: 'unambiguous',
  presets: ['@vue/cli-plugin-babel/preset']
};

I think we should set it as the default🤔

@LinusBorg
Copy link
Member

LinusBorg commented Oct 30, 2019

I this setting safe for arbitrary dependencies? Afaik it's only necessary when attempting to transpile a module that's using the commonjs module format.

@JessicaSachs
Copy link

I've been living with sourceType: 'unambiguous' for a year or so. It will break things in various plugins and I don't think it should be the default.

@haoqunjiang
Copy link
Member

@JessicaSachs could you elaborate on the incompatible plugins?

@JessicaSachs
Copy link

I’ve run into issues with plugins like styleguidist that do some static analysis. They’ve been fixed, but not until I submitted a reproduction repo. It’s not a common setting and there is risk of breaking existing repositories by changing the sourceType default.

@ghost
Copy link
Author

ghost commented Oct 31, 2019

I've been living with sourceType: 'unambiguous' for a year or so. It will break things in various plugins and I don't think it should be the default.

Is there any problem, can you list?

@ghost
Copy link
Author

ghost commented Oct 31, 2019

Workaround: set babel.config.js to:

module.exports = {
  sourceType: 'unambiguous',
  presets: ['@vue/cli-plugin-babel/preset']
};

I think we should set it as the default🤔

this can be ok

@haoqunjiang
Copy link
Member

haoqunjiang commented Oct 31, 2019

@JessicaSachs If you mean this issue vue-styleguidist/vue-styleguidist#436, I believe it's not related to the sourceType configuration.


@LinusBorg

According to the babel documentation, unambiguous seems to be a safer option than the default module, especially when dealing with third-party dependencies.

The edge cases are ambiguous modules as listed in https://github.com/tc39/proposal-UnambiguousJavaScriptGrammar#problem

For such a simple and ambiguous file, if no polyfills/helpers are injected:

  • With sourceType: 'module', a "use strict" pragma would be added to the top of that file and the whole module will run in strict mode.
  • With sourceType: 'unambiguous', it will be considered a CommonJS module and remain as-is.

I think the latter one is more reasonable in the context of third-party dependencies.


The real problem:

If a module references something like Symbol and a polyfill module needs to be added:

  • With sourceType: 'module', an import statement will be injected to the file, regardless of the presence of module.exports;
  • With sourceType: 'unambiguous', an require statement will be injected if no import/export is seen in the file.

Because we disabled the module transformation in babel, all import/require statements are passed down to webpack as-is. And the former case will encounter an error in webpack runtime because webpack has stricter rules on ES modules (the exports object would be read-only). And that's exactly the case in this issue.

This also means, if we don't change the default sourceType configuration, all third-party CommonJS dependencies with partially transpiled code will encounter similar problems.

These use cases are also described in the README (the 3rd option).
We currently recommend users to set useBuiltIns: 'entry', which is quite unintuitive and also sub-optimal in code size.
So I believe sourceType: 'unambiguous' could be a much better solution.

@JessicaSachs
Copy link

JessicaSachs commented Oct 31, 2019 via email

@LinusBorg
Copy link
Member

LinusBorg commented Oct 31, 2019

we could experiment with it in a beta release, maybe?

Plus we can document that babel's overrides option can be used to fine-tune this for individual packages/files if necessary:

module.exports = {
  sourceType: 'unambiguous',
  presets: ['@vue/cli-plugin-babel/preset'],
  overrides: [
    {
       test: 'node_modules/some-package/**/*',
       sourceType: 'module'
    }
  ]
};

@haoqunjiang
Copy link
Member

Yeah, we can experiment it in v4.1.0-beta.0.

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

Successfully merging a pull request may close this issue.

3 participants