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

RFC:webpack 特殊语法排除 typeof 的场景 #1291

Open
sorrycc opened this issue Jun 19, 2024 · 1 comment · May be fixed by #1688
Open

RFC:webpack 特殊语法排除 typeof 的场景 #1291

sorrycc opened this issue Jun 19, 2024 · 1 comment · May be fixed by #1688
Labels
good first issue Good for newcomers rfc

Comments

@sorrycc
Copy link
Member

sorrycc commented Jun 19, 2024

问题

比如

https://github.com/opensumi/core/blob/6273dbac4c3e1879260482d85030b7257aaf09b0/packages/utils/src/os.ts#L64

包含代码,

export function getNodeRequire() {
  return typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require;
}

其中 __webpack_require__ 会被认定为特殊语法,然后报错。

解法

排除 typeof 的场景。

新增一个 visitor,在遇到 __webpack_require__ 等 webpack 特有语法时,转换为 typeof undefined

比如:

- return typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require;
+ return typeof undefined === 'function' ? __non_webpack_require__ : require;

这个一直修修补补,感觉需要一个更彻底的解,比如通过配置提供一个 webpack 常用 runtime 层的兼容补丁。

@sorrycc sorrycc added the rfc label Jun 19, 2024
@stormslowly
Copy link
Member

stormslowly commented Jun 19, 2024

单点的看这个问题 ,修改了之后三元表达式返回 require,如果在 platform web 的情况下,这个 require 应该会变成 __mako_require__ 不知道后继的逻辑还能不能对。

@sorrycc sorrycc added the good first issue Good for newcomers label Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers rfc
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants