Read env variable from service worker file using workbox

感情迁移 提交于 2019-12-10 14:46:52

问题


I'm working with workbox-sw and workbox-webpack-plugin My src sw.js file at root folder (same level with my .env file)

How can I get env variables and accessing it into my src sw.js file

This is from my 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!


回答1:


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.)

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?



来源:https://stackoverflow.com/questions/48398658/read-env-variable-from-service-worker-file-using-workbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!