防止service-worker.js与vite/rollup捆绑
我有一个使用 Vite 编译和捆绑的基于 TypeScript 的 Vuejs 项目。
我正在尝试配置构建系统来编译我的自定义服务工作线程 (src/service-worker.ts) 并将输出放置在 dist/service-worker.js 中。特别是,我不希望将其作为应用程序 JS 包的一部分包含在内,因为它需要作为静态网站的一部分在该众所周知的 URL 上提供服务。
现有的结构是这样的:
index.html
public/
favicon.ico
src/
service-worker.ts
main.ts
/* etc */
我希望输出是这样的:
dist/
index.html
assets/index.[hash].js
assets/vendor.[hash].js
/* ... */
service-worker.js # <-- I would like the file emitted here
如果服务工作线程不需要被转换/编译,我知道我可以简单地 将其包含在 public/
中,它将原封不动地复制到 dist/
文件夹中。
我看过 vite-plugin-pwa 但它相当不透明并且与工作箱相关。
其他相关问题涉及人们希望从构建输出中完全排除文件的情况,这并不完全是我想要的后。
如何编译 TypeScript 文件,但将其输出未捆绑在我的 dist 文件夹中?
I have a TypeScript-based Vuejs project compiled and bundled using Vite.
I am trying to configure the build system to compile my custom service worker (src/service-worker.ts) and place the output in dist/service-worker.js. In particular, I don't want it included as part of the application's JS bundle, because it needs to be served at that well-known URL as part of a static website.
The existing structure is like:
index.html
public/
favicon.ico
src/
service-worker.ts
main.ts
/* etc */
And I would like the output to be something like:
dist/
index.html
assets/index.[hash].js
assets/vendor.[hash].js
/* ... */
service-worker.js # <-- I would like the file emitted here
If the service worker didn't need to be transpiled/compiled, I know I could simply include it in public/
and it would be copied to the dist/
folder unchanged.
I've looked at vite-plugin-pwa but that is rather opaque and tied to workbox.
Other related questions relate to situations where people want to exclude files altogether from their build output, which is not quite what I'm after.
How can I compile a TypeScript file but leave its output unbundled in my dist folder?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过
build.rollupOptions
配置入口点和输出位置在您的 Vite 配置中:为
service-worker.ts
文件添加一个入口点(通过build.rollupOptions.input
)。为输出文件名添加一个格式化程序,将服务工作线程文件放入输出目录的根目录中(通过
build.rollupOptions.output.entryFilenames
)。演示
You can configure the entry point and output locations via
build.rollupOptions
in your Vite config:Add an entry point for the
service-worker.ts
file (viabuild.rollupOptions.input
).Add a formatter for output filenames that puts the service worker file in the root of the output directory (via
build.rollupOptions.output.entryFilenames
).demo