Ben*_*Dev 15 javascript typescript reactjs webpack
我目前正在尝试为 ReactJs 构建一个状态管理库。但是一旦我将它实现到我的 React 项目(使用create-react-app)中,它就会开始删除这个错误:
Failed to compile.
path/to/agile/dist/runtime.js 116:104
Module parse failed: Unexpected token (116:104)
File was processed with these loaders:
* ./node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
| if (subscriptionContainer instanceof sub_1.CallbackContainer) subscriptionContainer.callback(); // If Component based subscription call the updateMethod which every framework has to define
|
> if (subscriptionContainer instanceof sub_1.ComponentContainer) if (this.agileInstance.integration?.updateMethod) this.agileInstance.integration?.updateMethod(subscriptionContainer.component, Runtime.formatChangedPropKeys(subscriptionContainer));
| }); // Log Job
|
Run Code Online (Sandbox Code Playgroud)
如果我注释掉错误中提到的突出显示的代码行,它会跳转到另一个点并在那里抱怨。但这不可能是语法错误,因为 TypeScript 也会在 IDE 中抱怨。
工具如何工作:
一开始你必须定义一个框架,在这个例子中是 React。然后你可以创建一个 State 并通过 Hook 将这个 State 订阅到 React Functional Component。用于将 State 绑定到 React 组件的 Hook 只是创建一个回调,该回调触发重新渲染(通过改变 a useReducer)并将此回调分配给订阅的 State。
如果您想了解更多信息,请查看此 repo:https : //github.com/agile-ts/agile
依赖项:
第三方状态管理库:
"dependencies": {
"@types/chai": "^4.2.12",
"@types/mocha": "^8.0.2",
"chai": "^4.2.0",
"eslint-config-prettier": "^6.11.0",
"mocha": "^8.1.1",
"prettier": "2.0.5",
"ts-node": "^8.10.2",
"tsc-watch": "^4.1.0",
"tslib": "^2.0.0",
"typescript": "^3.9.7"
}
Run Code Online (Sandbox Code Playgroud)
v14.8.0反应项目:
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"@types/jest": "^24.0.0",
"@types/node": "^12.0.0",
"@types/react": "^16.9.0",
"@types/react-dom": "^16.9.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.3",
"typescript": "~3.7.2",
"agile-framework": "file:../../"
},
Run Code Online (Sandbox Code Playgroud)
16.13.16.14.7Pio*_*iak 43
如果您不是该库的作者,或者您不想按照接受的答案建议更改它,您可以执行以下操作:
按照@adlerer的建议更改浏览器列表:
"browserslist": [
">0.2%",
"not dead",
"not op_mini all"
],
Run Code Online (Sandbox Code Playgroud)
(确保您没有developmentin的特殊配置browserslist)
清除 npm 缓存:
npm cache clean --force
Run Code Online (Sandbox Code Playgroud)
重新安装@Gel建议的东西:
rm -rf node_modules && rm -f package-lock.json && npm i
Run Code Online (Sandbox Code Playgroud)
经过数小时的研究和试验后,这对我有所帮助。
jon*_*rpe 13
问题是您将 ES2020 发送到dist/. 如果您查看该行,它会抱怨:
if (subscriptionContainer instanceof sub_1.ComponentContainer) if (this.agileInstance.integration?.updateMethod) this.agileInstance.integration?.updateMethod(subscriptionContainer.component, Runtime.formatChangedPropKeys(subscriptionContainer));
// ^^ // ^^
Run Code Online (Sandbox Code Playgroud)
您可以看到它在发出的代码中使用了可选的链接运算符。因此,您的库的使用者需要有适当的配置来处理这种代码。您的示例消费者,即 CRA 应用程序,正在使用 Babel;尽管设置确实具有可选链的转换,但它仅在 React 应用程序本身的源代码上运行,而不是其依赖项(包括您的库)。
修复它的一种选择是发布较少的现代代码,这将减少消费者所需的配置量。例如,如果您在 中使用以下设置来定位 ES6 tsconfig.json:
if (subscriptionContainer instanceof sub_1.ComponentContainer) if (this.agileInstance.integration?.updateMethod) this.agileInstance.integration?.updateMethod(subscriptionContainer.component, Runtime.formatChangedPropKeys(subscriptionContainer));
// ^^ // ^^
Run Code Online (Sandbox Code Playgroud)
React 应用程序至少可以在不需要更改源代码的情况下启动。
小智 10
虽然已经晚了,但仍然可以帮助某人。就我而言,我忘记添加
"jsx": "react"
Run Code Online (Sandbox Code Playgroud)
在我的 tsconfig.json 中。
小智 5
就我而言,设置更保守的编译目标有助于package.json解决此问题:
"browserslist": [
">0.2%",
"not dead",
"not op_mini all"],
Run Code Online (Sandbox Code Playgroud)