Skip to content

Commit e735c03

Browse files
committed
Cleaned up webpack config, thanks to @greengrape
1 parent b2a1e61 commit e735c03

File tree

3 files changed

+31
-70
lines changed

3 files changed

+31
-70
lines changed

webpack-iwa-template/webpack.common.js

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ const __filename = fileURLToPath(import.meta.url);
2323
const __dirname = path.dirname(__filename);
2424

2525
const mainConfig = {
26-
name: "main",
26+
name: "main",
2727
entry: {
2828
main: "./src/index.ts",
29+
sw: './src/service-worker.ts'
2930
},
3031
output: {
31-
filename: "[name].bundle.js",
32+
filename: (pathData) => {
33+
return pathData.chunk.name === 'sw'
34+
? "service-worker.js"
35+
: "[name].bundle.js";
36+
},
3237
path: path.resolve(__dirname, "dist"),
38+
clean: true,
3339
},
3440
plugins: [
3541
new HtmlWebpackPlugin({
@@ -64,28 +70,4 @@ const mainConfig = {
6470
},
6571
};
6672

67-
const serviceWorkerConfig = {
68-
name: "service-worker",
69-
entry: {
70-
"service-worker": path.resolve(__dirname, "src", "service-worker.ts"),
71-
},
72-
target: "webworker",
73-
output: {
74-
filename: "service-worker.js",
75-
path: path.resolve(__dirname, "dist"),
76-
},
77-
module: {
78-
rules: [
79-
{
80-
test: /\.tsx?$/,
81-
use: "ts-loader",
82-
exclude: /node_modules/,
83-
},
84-
],
85-
},
86-
resolve: {
87-
extensions: [".ts", ".js"],
88-
},
89-
};
90-
91-
export default [mainConfig, serviceWorkerConfig];
73+
export default mainConfig;

webpack-iwa-template/webpack.dev.js

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -15,33 +15,27 @@
1515
*/
1616

1717
import { merge } from "webpack-merge";
18-
import commonConfigs from "./webpack.common.js";
18+
import common from "./webpack.common.js";
1919
import dotenv from "dotenv";
2020

2121
dotenv.config();
2222

2323
const PORT = process.env.PORT;
2424

25-
export default commonConfigs.map((config) => {
26-
if (config.target === "webworker") {
27-
return merge(config, { mode: "development" });
28-
}
29-
30-
return merge(config, {
31-
mode: "development",
32-
devtool: "inline-source-map",
33-
devServer: {
34-
port: PORT || 4321,
35-
allowedHosts: "all",
36-
client: {
37-
webSocketURL: {
38-
protocol: "ws",
39-
hostname: "localhost",
40-
port: PORT || 4321,
41-
},
25+
export default merge(common, {
26+
mode: "development",
27+
devtool: "inline-source-map",
28+
devServer: {
29+
port: PORT || 4321,
30+
allowedHosts: "all",
31+
client: {
32+
webSocketURL: {
33+
protocol: "ws",
34+
hostname: "localhost",
35+
port: PORT || 4321,
4236
},
43-
static: "./dist",
44-
hot: true,
4537
},
46-
});
47-
});
38+
static: "./dist",
39+
hot: true,
40+
},
41+
});

webpack-iwa-template/webpack.prod.js

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -15,50 +15,35 @@
1515
*/
1616

1717
import { merge } from "webpack-merge";
18-
import commonConfigs from "./webpack.common.js";
18+
import common from "./webpack.common.js";
1919
import WebBundlePlugin from "webbundle-webpack-plugin";
2020
import { parsePemKey, NodeCryptoSigningStrategy, readPassphrase } from "wbn-sign";
2121
import dotenv from "dotenv";
2222
import fs from "fs";
23-
import path from "path";
24-
import { fileURLToPath } from "url";
2523

2624
dotenv.config();
2725

28-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
29-
3026
const PRIVATE_KEY_PATH = process.env.PRIVATE_KEY_PATH;
3127
const PRIVATE_KEY_PASSWORD = process.env.PRIVATE_KEY_PASSWORD;
3228

3329
if (!PRIVATE_KEY_PATH) {
3430
throw Error("Build Failed: Specify PRIVATE_KEY_PATH in your .env file");
3531
}
3632

33+
//Get the key and decrypt it
3734
const privateKey = parsePemKey(
3835
fs.readFileSync(PRIVATE_KEY_PATH),
3936
PRIVATE_KEY_PASSWORD || (await readPassphrase(PRIVATE_KEY_PATH)),
4037
);
4138

42-
const prodConfigs = commonConfigs.map((config) =>
43-
merge(config, {
44-
mode: "production",
45-
}),
46-
);
47-
48-
const webBundleConfig = {
49-
name: "webbundle",
50-
entry: {},
39+
export default merge(common, {
5140
mode: "production",
52-
dependencies: ["main", "service-worker"],
5341
plugins: [
5442
new WebBundlePlugin({
55-
static: { dir: path.resolve(__dirname, "dist") },
5643
output: "iwa-template.swbn",
5744
integrityBlockSign: {
5845
strategy: new NodeCryptoSigningStrategy(privateKey),
5946
},
6047
}),
6148
],
62-
};
63-
64-
export default [...prodConfigs, webBundleConfig];
49+
});

0 commit comments

Comments
 (0)