我有一个小的TypeScript React项目,我使用Parcel捆绑在一起。我想添加MSW来模拟服务器请求,但我无法使其正常工作。
我有一个非常简约的包裹设置:
"scripts": {
"start": "parcel index.html",
"build": "parcel build index.html"
},
至于MSW,一切都是根据他们的documentation来完成的。下面是服务工作者的初始化方式:
if (process.env.NODE_ENV === "development") {
const { worker } = require("./mocks/browser")
worker.start()
}
当与Webpack捆绑在一起时,完全相同的实现可以完美地工作。但在Parcel中,它会在控制台中导致以下错误:
The script has an unsupported MIME type ('text/html').
[MSW] Failed to register a Service Worker: Failed to register a ServiceWorker for scope ('http://localhost:1234/') with script ('http://localhost:1234/mockServiceWorker.js'): The script has an unsupported MIME type ('text/html').
有没有办法让它正常工作?
发布于 2021-01-21 23:54:55
解决方案:npx msw init dist
事实证明,我在运行npx msw init
时使用的是适用于Create-React-App
的public
,而不是使用Parcel时应该使用的dist
。
https://stackoverflow.com/questions/65829550
复制相似问题