我正在尝试使用Workbox创建一个Webpack构建器,正如我所看到的,Vue CLI 3正在使用。 在我的package.json文件中,我做了一个“构建”来构建和编译文件,并做了一个“ dev”来运行它。
"build": "webpack --mode development",
"dev": "nodemon --inspect --watch webpack.config.js main.js"
我使用InjectManifest
中的workbox-webpack-plugin
处理我的webpack.config.js
文件中的构建。我尝试了不同的选择,看看它们做了什么。
webpack.config.js
const path = require('path')
const webpack = require('webpack')
const HTMLWebpackPlugin = require('html-webpack-plugin')
const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin')
const {GenerateSW, InjectManifest} = require('workbox-webpack-plugin')
module.exports = {
mode: 'production',
entry: {
app: [
'@babel/polyfill',
'./src/main.js'
]
},
output: {
path: path.resolve(__dirname, 'dist/'),
filename: '[name]-[hash].js',
publicPath: '/'
},
devServer: {
inline: false,
contentBase: "./dist",
overlay: true,
hot: true,
stats: {
colors: true
}
},
module: {
rules: [
{
test: /\.html$/,
use: {
loader: 'html-loader'
}
},
{
test: /\.js?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['@babel/preset-env']
}
},
{
test: /\.css$/,
loader: [
'style-loader',
'css-loader'
]
},
{
test: /\.scss$/,
use: [
{
loader: "style-loader"
},
{
loader: "css-loader", options: {
sourceMap: true
}
},
{
loader: "sass-loader", options: {
sourceMap: true
}
}
]
}
]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new HTMLWebpackPlugin({
template: './public/index.html'
}),
// new GenerateSW({
// include: [/\.html$/, /\.js$/, /\.css$/],
// exclude: '/node_modules'
// }),
new InjectManifest({
swSrc: './src/js/registerServiceWorker.js',
swDest: '/serviceworker.js',
exclude: '/node_modules'
})
// service worker caching
// new SWPrecacheWebpackPlugin(
// {
// cacheId: 'webpack-project',
// filename: 'service-worker.js',
// staticFileGlobs: [
// 'dist/**/*.{js,css}',
// '/'
// ],
// minify: true,
// stripPrefix: 'dist/',
// dontCacheBustUrlsMatching: /\.\w{6}\./
// }
// )
]
}
构建项目时,我会创建以下文件:
app-e8dd91....js
index.html
precache-manifest.d92js8....js
serviceworker.js
index.html文件仅将app-e8d91 .... js文件作为脚本添加,从而保留了未使用预缓存的清单文件和服务工作者,这导致本地主机没有服务工作者。
我有一个registerServiceWorker.js文件,该文件正在injectManifest插件中引用。
registerServiceWorker.js
import { register } from "register-service-worker";
const path = require('path')
register(path.resolve(__dirname, 'service-worker.js'), {
ready() {
console.log(
"App is being served from cache by a service worker."
);
},
cached() {
console.log("Content has been cached for offline use.");
},
updated() {
console.log("New content is available; please refresh.");
},
offline() {
console.log(
"No internet connection found. App is running in offline mode."
);
},
error(error) {
console.error("Error during service worker registration:", error);
}
});
我如何使我的内部版本创建并添加service worker + manifest.json文件,使其在localhost上也可以离线工作?
文件结构:
webpack.config.js
server.js (handle server + webpackHotMiddleware + express)
dist (builded folder)
src (folder)
main.js
js (folder)
registerServiceWorker.js
public (folder)
index.html
manifest.json
favicon.ico