-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
132 lines (119 loc) · 3.54 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import * as wp from 'webpack';
import * as WebpackPwaManifest from 'webpack-pwa-manifest';
import * as WorkboxPlugin from 'workbox-webpack-plugin';
const { InjectManifest } = WorkboxPlugin;
const { GenerateSW } = WorkboxPlugin;
export interface IPWAConfig {
swSrc?: string | undefined;
importWorkboxFrom?: 'cdn' | 'local' | 'disabled';
chunks?: string[];
include?: string[];
exclude?: string[];
excludeChunks?: string[] | string;
precacheManifestFilename?: string;
swDest: string;
globDirectory?: string | undefined;
clientsClaim?: boolean;
skipWaiting?: boolean;
globPatterns?: string[];
globIgnores?: string[];
maximumFileSizeToCacheInBytes?: number;
dontCacheBustUrlsMatching?: RegExp | null;
modifyUrlPrefix?: null | { [name: string]: string };
}
interface IApplication {
platform: string;
id?: string;
url: string;
}
export interface IManifest {
background_color: string;
description: string;
display: 'fullscreen' | 'standalone';
filename: string;
fingerprints?: boolean;
icons?: WebpackPwaManifest.Icon[];
includeDirectory?: boolean;
ios?: boolean;
name: string;
orientation: 'portrait' | 'landscape';
publicPath?: string;
related_applications?: IApplication[];
short_name: string;
start_url: string;
theme_color: string;
}
interface IModifyConfigOption {
manifestConfig?: IManifest;
pwaConfig?: IPWAConfig;
}
interface IModifyConfigTargetDevOptions {
target: 'node' | 'web';
dev: boolean;
}
interface IRazzleUserOptions {
[name: string]: any;
}
type IRazzleModifyFunc = (
baseConfig: wp.Configuration,
{ target, dev }: IModifyConfigTargetDevOptions,
webpack: wp.Compiler,
userOptions: IRazzleUserOptions
) => wp.Configuration;
const defaultWorkboxConfig = {
importWorkboxFrom: 'local',
swSrc: undefined
};
/**
*
* @param {IPWAConfig} pwaConfig - workbox-webpack-plugin options
* @description The plugin configurations passed here will be used to create the service worker via webpack.
* See here for details https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin
*
*/
const pwa = (pwaConfig: IPWAConfig): wp.Plugin => {
const c = { ...defaultWorkboxConfig, ...pwaConfig };
if (pwaConfig.swSrc) {
return new InjectManifest(c);
}
delete c.swSrc;
return new GenerateSW(c);
};
/**
*
* @param {IManifest} manifestConfig - webpack-pwa-manifest options
* @description The plugin configurations passed here will be used to create the
* a web manifest via webpack.
* See here for details: https://github.com/arthurbergmz/webpack-pwa-manifest
*
*/
const manifest = (manifestConfig: IManifest): wp.Plugin =>
new WebpackPwaManifest(manifestConfig);
/**
* @param {IModifyConfigOption} pluginConfig - configuration objects for webpack-pwa-manifest and/or workbox-webpack-plugin.
* @description returns a function which closes over the pluginConfig and returns an object that can
* be passed along to razzle.
* @returns IRazzleModifyFunc
*/
export default (pluginConfig: IModifyConfigOption): IRazzleModifyFunc => {
return (
baseConfig: wp.Configuration,
{ target, dev }: IModifyConfigTargetDevOptions,
webpack: wp.Compiler,
userOptions = {}
) => {
const config = { ...baseConfig };
if (target === 'web') {
const { manifestConfig, pwaConfig } = pluginConfig;
if (manifestConfig) {
config &&
config.plugins &&
config.plugins.push(manifest(manifestConfig));
}
if (pwaConfig) {
config && config.plugins && config.plugins.push(pwa(pwaConfig));
}
}
return config;
};
};