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

require中有模版字符串没有解析 #16

Open
zjp1300 opened this issue Dec 16, 2021 · 4 comments
Open

require中有模版字符串没有解析 #16

zjp1300 opened this issue Dec 16, 2021 · 4 comments

Comments

@zjp1300
Copy link

zjp1300 commented Dec 16, 2021

const img = require(../../assets/imgs/white.svg) //使用模版字符串 没有转换
const img2 = require('../../assets/imgs/white.svg')

@jiawulin001
Copy link
Member

@originjs/vite-plugin-commonjs会把require转换成import,所以模板字符串没办法支持。
你可以试试 import-expressions.


@originjs/vit-plugin-commonjs will convert require to import, so there is no way to support template literals.
You can try import-expressions.

@zjp1300
Copy link
Author

zjp1300 commented Dec 16, 2021

image
I used my example to run your one side and found that I didn't pass

@jiawulin001
Copy link
Member

How about replacing require(`react`) withrequire('react')?

@jankerli
Copy link

node_modules/@originjs/vite-plugin-commonjs/lib/lib.js

-const requireRegex = /{0,2}require\s*(\s*(["'].?["'])\s)/g;
+const requireRegex = /
{0,2}require\s*(\s*(["'].*?["'])\s*)/g;

-importsString += import * as ${packageName} from ${item[1]};\n;
+importsString += import * as ${packageName} from ${item[1].replace(//g, """)};\n`;

我也发现了这个问题,自己修改了下源码,使用 patch-package 本地打了个补丁

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

No branches or pull requests

3 participants