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

使用import {xxx}导入commonjs的module.exports导出的对象语法报错 , does not provide an export name “name” #15

Open
iamouyang21 opened this issue Dec 16, 2021 · 8 comments
Labels
feature help wanted Extra attention is needed

Comments

@iamouyang21
Copy link

Versions

  • originjs:
  • node:

Reproduction

Additional Details

Steps to reproduce

What is Expected?

What is actually happening?

导入处代码main.js:
import {name} from ./demo

导出处代码demo.js:
module.exports={
name:1111
}

提示报错:does not provide an export name “name”

@jiawulin001
Copy link
Member

jiawulin001 commented Dec 16, 2021

this way:
image

@iamouyang21
Copy link
Author

我是将vue-cli项目升级为vite,由于webpack支持esm和commonjs混合写法,所以我们项目里面有很多地方是这样写的。升级为vite后不想每个地方都去改一次。

@jiawulin001
Copy link
Member

那你可以试试这个:https://github.com/originjs/webpack-to-vite#vue-cli-conversion
通过它转换出来的项目更贴近原生Vite项目,避免了动态转换的消耗。

@iamouyang21
Copy link
Author

还有一个问题,在计算属性里面require 图片会提示require is not defuned。
代码如下:
data() {
return {
flag:true,
}
}

computed:{
demoImg:require(“@/asset/${this.flag} ?“a” :“b”.png)

抱歉,手机敲得代码,公司电脑无法登录github。

@jiawulin001
Copy link
Member

抱歉,动态的导入,我们没有处理,也处理不了, 你知道vite项目只能使用ESM,所以我们必须把require转换成import,而import后面只能是一个常量字符串,如果使用动态导入import(""),那就会变成异步的了,逻辑的执行顺序就变了,所以这种情况只能手动去处理

@iamouyang21
Copy link
Author

好的,谢谢,那我还是全局正则替换吧

@jiawulin001
Copy link
Member

好的,谢谢,那我还是全局正则替换吧

好的,转换过程中有疑问可以找我们一起讨论。

前面那个问题我在考虑要不要把export default {},这个default的对象展开属性,然后挨个导出,这样应该能解决你说的场景,你有更好的建议也欢迎告诉我们。

const code = fs.readFileSync(id).toString();
let result = transformRequire(code, id);
if (result.replaced) {
return {
contents: result.code,
loader: "js",
};

@iamouyang21
Copy link
Author

好的,谢谢,如果解决了上面的commonjs和esm混用的问题麻烦说一下哇。我将包升级,这样就不需要每个地方去修改了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants