如何使用 Parcel 捆绑器运行 ReactJS 应用程序的生产构建?

问题描述 投票:0回答:2

我只是尝试使用 Parcel build /src/index.html 生成构建, 我能够构建应用程序,但是当我尝试从浏览器打开构建时,它说构建后无法从index.html访问index.js。

<body>
    <div id="root"></div>
    <script src="index.js" type="module"></script>
</body>

无论如何,我在本地运行该项目没有任何问题。 请看下面的图片以了解我的担忧。 谢谢。

Console error after opening index.html from build

Project structure

javascript reactjs bundler parceljs parcel
2个回答
2
投票

您需要从网络服务器提供

dist
目录。

F.I.从您的终端:

cd dist
npx serve

0
投票

如果是 npm 项目,可以在 package.json 的脚本中添加此内容

"build": "parcel build index.html", 

这将为您提供生产版本或简单地运行

npx parcel build index.html

并在本地运行应用程序

cd dist
npx serve
© www.soinside.com 2019 - 2024. All rights reserved.