Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

**NOTE: for newer changelog, go to [release page](https://github.com/shadowwalker/next-pwa/releases) instead.**

## 6.0.0

### Fix

1. dependency upgrades
2. Update require to import

## 5.6.0

Summary
BREAKING CHANGE
Start from version 5.6.0. This plugin function signature has been changed to follow the recommended pattern from next.js. Mainly extracting pwa config from mixing into rest of the next.js config. This is also less intrusive. See following commit for details.

## 5.5.6

Version 5.5.5 included a broken change which prevent pwa config to be effective. This release will fix that.

## 5.5.0

### Fix
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 ShadowWalker [email protected] https://weiw.io
Copyright (c) 2024 AlexJesus <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
460 changes: 217 additions & 243 deletions README.md

Large diffs are not rendered by default.

162 changes: 59 additions & 103 deletions build-custom-worker.js
Original file line number Diff line number Diff line change
@@ -1,120 +1,76 @@
'use strict'
// build-custom-worker.js
'use strict';

const path = require('path')
const fs = require('fs')
const webpack = require('webpack')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const TerserPlugin = require('terser-webpack-plugin')
import path from 'path';
import fs from 'fs';
import webpack from 'webpack';
import { createWorkerWebpackConfig } from './webpack-worker-config.js';

const buildCustomWorker = ({ id, basedir, customWorkerDir, destdir, plugins, minify }) => {
let workerDir = undefined

if (fs.existsSync(path.join(basedir, customWorkerDir))) {
workerDir = path.join(basedir, customWorkerDir)
} else if (fs.existsSync(path.join(basedir, 'src', customWorkerDir))) {
workerDir = path.join(basedir, 'src', customWorkerDir)
/**
* 构建自定义 Service Worker
* @param {Object} options 构建选项
* @param {string} options.id - 构建 id
* @param {string} options.basedir - 项目根目录
* @param {string} options.customWorkerDir - 自定义 worker 目录(相对于 basedir)
* @param {string} options.destdir - 输出目录
* @param {Array} options.plugins - 额外的 webpack 插件
* @param {boolean} options.minify - 是否压缩代码
* @returns {Promise<string|undefined>} 构建成功返回生成的文件名,否则返回 undefined
*/
const buildCustomWorker = async ({ id, basedir, customWorkerDir, destdir, plugins, minify }) => {
// 寻找自定义 worker 目录:先在 basedir 下,再在 basedir/src 下查找
let workerDir;
const candidate1 = path.join(basedir, customWorkerDir);
const candidate2 = path.join(basedir, 'src', customWorkerDir);
if (fs.existsSync(candidate1)) {
workerDir = candidate1;
} else if (fs.existsSync(candidate2)) {
workerDir = candidate2;
}
if (!workerDir) return;

if (!workerDir) return

const name = `worker-${id}.js`
const name = `worker-${id}.js`;
// 检查入口文件 index.ts 或 index.js
const customWorkerEntries = ['ts', 'js']
.map(ext => path.join(workerDir, `index.${ext}`))
.filter(entry => fs.existsSync(entry))

if (customWorkerEntries.length === 0) return
.filter(entry => fs.existsSync(entry));

if (customWorkerEntries.length === 0) return;
if (customWorkerEntries.length > 1) {
console.warn(
`> [PWA] WARNING: More than one custom worker found (${customWorkerEntries.join(
','
)}), not building a custom worker`
)
return
);
return;
}
const customWorkerEntry = customWorkerEntries[0];
console.log(`> [PWA] Custom worker found: ${customWorkerEntry}`);
console.log(`> [PWA] Build custom worker: ${path.join(destdir, name)}`);

// 使用共享 webpack 配置打包 custom worker
const config = createWorkerWebpackConfig({
entry: customWorkerEntry,
destdir,
filename: name,
testRule: /\.(t|j)s$/i,
minify,
extraPlugins: plugins
});

const customWorkerEntry = customWorkerEntries[0]
console.log(`> [PWA] Custom worker found: ${customWorkerEntry}`)
console.log(`> [PWA] Build custom worker: ${path.join(destdir, name)}`)
webpack({
mode: 'none',
target: 'webworker',
entry: {
main: customWorkerEntry
},
resolve: {
extensions: ['.ts', '.js'],
fallback: {
module: false,
dgram: false,
dns: false,
path: false,
fs: false,
os: false,
crypto: false,
stream: false,
http2: false,
net: false,
tls: false,
zlib: false,
child_process: false
// 使用 Promise 封装 webpack.run
await new Promise((resolve, reject) => {
webpack(config).run((error, status) => {
if (error || status.hasErrors()) {
console.error(`> [PWA] Failed to build custom worker`);
console.error(status ? status.toString({ colors: true }) : error);
return reject(error || new Error('Webpack compilation errors'));
}
},
module: {
rules: [
{
test: /\.(t|j)s$/i,
use: [
{
loader: 'babel-loader',
options: {
presets: [
[
'next/babel',
{
'transform-runtime': {
corejs: false,
helpers: true,
regenerator: false,
useESModules: true
},
'preset-env': {
modules: false,
targets: 'chrome >= 56'
}
}
]
]
}
}
]
}
]
},
output: {
path: destdir,
filename: name
},
plugins: [
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: [path.join(destdir, 'worker-*.js'), path.join(destdir, 'worker-*.js.map')]
})
].concat(plugins),
optimization: minify
? {
minimize: true,
minimizer: [new TerserPlugin()]
}
: undefined
}).run((error, status) => {
if (error || status.hasErrors()) {
console.error(`> [PWA] Failed to build custom worker`)
console.error(status.toString({ colors: true }))
process.exit(-1)
}
})
resolve();
});
});

return name
}
return name;
};

module.exports = buildCustomWorker
export default buildCustomWorker;
Loading