forked from tszarzynski/cra-append-sw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·187 lines (172 loc) · 4.39 KB
/
index.js
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env node
const fs = require("fs");
const MemoryFs = require("memory-fs");
const webpack = require("webpack");
const Dotenv = require("dotenv-webpack");
const program = require("commander");
const path = require("path");
const BUILD_SW_FILE_PATH = "build/service-worker.js";
const BUNDLE_FILE_NAME = "bundle.js";
/**
* Command line options
*/
program
.arguments("<file>")
.option("-s, --skip-compile", "skip compilation")
.option(
"-e, --env [path]",
"path to environment variables files [./.env]",
"./.env"
)
.option(
"-m, --mode <mode>",
"output mode [dev|build|replace]",
/^(dev|build|replace)$/i
)
.action(function(file) {
if (program.mode === "dev") {
process.env.BABEL_ENV = "development";
process.env.NODE_ENV = "development";
} else {
process.env.BABEL_ENV = "production";
process.env.NODE_ENV = "production";
}
if (program.skipCompile) {
read(file).then(result => append(result, file));
} else {
compile(file).then(({ result, stats }) => append(result, file));
}
})
.parse(process.argv);
/**
* Compile entry file using WebPack
*
* @param {String} entry Path to entry file
* @returns {Promise}
*/
function compile(entry) {
const compiler = webpack({
mode: program.mode === "dev" ? "development" : "production",
entry: [entry],
output: {
filename: BUNDLE_FILE_NAME,
path: "/"
},
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: "babel-loader",
options: {
presets: [
[
"react-app",
{
targets: {
browsers: ["defaults"]
}
}
]
],
plugins: ["@babel/plugin-transform-runtime"]
}
}
}
]
},
plugins: [
new Dotenv({
path: program.env, // Path to .env file (this is the default)
safe: false, // load .env.example (defaults to "false" which does not use dotenv-safe)
silent: true,
systemvars: true // Load all system variables and REACT .env as well
})
// new webpack.optimize.UglifyJsPlugin()
]
});
compiler.outputFileSystem = new MemoryFs();
return new Promise((resolve, reject) => {
compiler.run((err, stats) => {
if (err) return reject(err);
if (stats.hasErrors() || stats.hasWarnings()) {
return reject(
new Error(
stats.toString({
errorDetails: true,
warnings: true
})
)
);
}
const result = compiler.outputFileSystem.data[
BUNDLE_FILE_NAME
].toString();
resolve({ result, stats });
});
});
}
/**
* Read entry file
*
* @param {String} entry Path to entry file
* @returns {Promise}
*/
function read(entry) {
return new Promise((resolve, reject) => {
fs.readFile(entry, "utf8", (error, result) => {
if (error) {
reject(error);
}
resolve(result);
});
});
}
/**
* Append custonm code to exisitng ServiceWorker
*
* @param {String} code
* @returns {Promise}
*/
function append(code, file) {
if (program.mode === "dev") {
const filename = path.basename(file);
return writeFile(code, `public/${filename}`);
} else if (program.mode === "build") {
const filename = path.basename(file);
return writeFile(code, `build/${filename}`);
} else if (program.mode === "replace") {
const filename = path.basename(file);
return writeFile(code, BUILD_SW_FILE_PATH);
} else {
// Append to "build/service-worker.js"
return new Promise((resolve, reject) => {
// Read exisitng SW file
fs.readFile(BUILD_SW_FILE_PATH, "utf8", (error, data) => {
if (error) {
reject(error);
}
// append custom code
const result = data + code;
// Write modified SW file
fs.writeFile(BUILD_SW_FILE_PATH, result, "utf8", error => {
if (error) {
reject(error);
}
resolve();
});
});
});
}
}
function writeFile(content, file) {
return new Promise((resolve, reject) => {
fs.writeFile(file, content, "utf8", error => {
if (error) {
reject(error);
}
resolve();
});
});
}