使用工作箱从服务工作文件中读取env变量 [英] Read env variable from service worker file using workbox
问题描述
我正在使用 workbox-sw
和 workbox-webpack-plugin
我的src sw.js
根文件夹中的文件(与我的.env文件相同)
I'm working with workbox-sw
and workbox-webpack-plugin
My src sw.js
file at root folder (same level with my .env file)
如何获取env变量和访问我的src sw.js
文件
How can I get env variables and accessing it into my src sw.js
file
这是来自我的 webpack.config .js
new workboxPlugin({
globDirectory: 'dist',
globPatterns: ['**/*.{html,js,css,svg,otf,png,jpg,gif}'],
swSrc: './sw.js',
swDest: path.join('public', 'sw.js')
})
每一个帮助将不胜感激!谢谢大家!
Every help will be appreciated! Thanks everyone!
推荐答案
因为你正在使用 swSrc:'。/ sw.js'
,这意味着 ./ sw.js
文件将成为输出的最终服务工作文件的基础。 (它将在构建过程中插入预先缓存信息。)
Because you're using swSrc: './sw.js'
, that implies that the ./sw.js
file will be the basis for the final service worker file that's output. (It will have the precache information inserted into it as part of the build process.)
这意味着您可以处理 ./ sw.js
但是你想在 workbox-webpack-plugin
运行之前,包括通过用环境变量替换占位符来修改它。我相信通常会使用 webpack.DefinePlugin
吗?
This means that you can process ./sw.js
however you'd like before the workbox-webpack-plugin
runs, including modifying it by replacing placeholders with environment variables. I believe that webpack.DefinePlugin
is normally used for that?
这篇关于使用工作箱从服务工作文件中读取env变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!