alibaba / rax

🐰 Rax is a progressive framework for building universal application. https://rax.js.org
Other
8k stars 627 forks source link

[Question]单页下webpack5+swc似乎不能使用 #2312

Closed xmsz closed 2 years ago

xmsz commented 2 years ago

背景

报错

ncaught (in promise) TypeError: this.setState is not a function at a.ma.h (index.js:1) at M.e..e. (index.js:1) at index.js:1 at index.js:1 at Array.map () at Object.a [as triggerHandles] (index.js:1) at index.js:1


然后我关掉swc,正常

--------------------

然后我在已有的项目,使用的使用遇到另外问题

ERR! Module not found: Error: Can't resolve './random' in '/Users/chenshijian/code/act-20211020/node_modules/.pnpm/random@3.0.6/node_modules/random/dist/esm' ERR! Did you mean 'random.js'? ERR! BREAKING CHANGE: The request './random' failed to resolve only because it was resolved as fully specified ERR! (probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '.mjs' file, or a '.js' file where the package.json contains '"type": "module"'). ERR! The extension in the request is mandatory for it to be fully specified. ERR! Add the extension to the request. ERR! webpack compile error Error: webpack compile error


如果我两个都开启,编译虽然成功,但出现另外错误

530.746260a7e03294009d99.js:1 Uncaught (in promise) ReferenceError: exports is not defined



--------------

我们想要加快一下构建效率,因为
- 有个多页项目只是页面路由多10个页面要build好几分钟。单页倒是速度正常
- 压缩的话,原先直接打包了接近1mb大小的js... 
SoloJiang commented 2 years ago

报错可以提供下复现代码么

xmsz commented 2 years ago

报错可以提供下复现代码么

复现第一个问题this.setState is not a function,就是默认新建的项目就可以复现

npm init rax demo-2021122701
cd demo-2021122701
npm install
// build.json
{
  "targets": ["web"],
  "webpack5": true,
  "swc": true,
  "plugins": []
}
npm run build

然后用浏览器打开构建出来的文件就行

xmsz commented 2 years ago

报错可以提供下复现代码么

复现Module not found: Error: Can't resolve './random',就是

npm i random
import random from 'random'

就可以复现,webpack4构建没问题

SoloJiang commented 2 years ago

ok 我来试下

SoloJiang commented 2 years ago
  1. 确定是 swc 压缩的场景存在问题,后续我来跟进解决,临时解法: 指定 esbuild 作为压缩器即可:
    "minify": {
    "type": "esbuild"
    },
SoloJiang commented 2 years ago
  1. https://github.com/transitive-bullshit/random/pull/36/files 看起来是这个库本身的问题