-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.prod.js
86 lines (73 loc) · 2.83 KB
/
webpack.prod.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
import { merge } from 'webpack-merge';
import webpack from 'webpack';
import path from 'path';
import commonConfigs from './webpack.common.js';
import CopyPlugin from 'copy-webpack-plugin';
import fs from 'fs';
import { dirname } from 'path';
import {fileURLToPath} from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
/**
* Custom Webpack plugin to increment the patch version in the source manifest.json file.
*/
class IncrementVersionPlugin {
constructor(options) {
this.manifestPath = options.manifestPath;
this.versionIncremented = false; // Add a flag to track if the version has been incremented
}
apply(compiler) {
compiler.hooks.emit.tapAsync('IncrementVersionPlugin', (compilation, callback) => {
if (this.versionIncremented) {
return callback(); // Skip if the version has already been incremented
}
let manifestContent = fs.readFileSync(this.manifestPath, 'utf8');
// Remove BOM if present
if (manifestContent.startsWith('\uFEFF')) {
manifestContent = manifestContent.slice(1);
}
const manifest = JSON.parse(manifestContent);
const versionParts = manifest.version.split('.').map(Number);
if (versionParts.length === 3) {
versionParts[2] += 1; // Increment the patch version
manifest.version = versionParts.join('.');
manifest.version_name = `β ${manifest.version}`;
fs.writeFileSync(this.manifestPath, JSON.stringify(manifest, null, 2));
console.log(`Version updated to ${manifest.version_name}`);
this.versionIncremented = true; // Set the flag to true after incrementing
} else {
console.error('Invalid version format in manifest.json');
}
callback();
});
}
}
const prodConfig = {
mode: 'production',
output: {
filename: '[name].js',
path:
path.resolve(__dirname, 'dist/extension/scripts'),
clean: true
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
}),
new IncrementVersionPlugin({
manifestPath: path.resolve(__dirname, 'src/manifest.json')
}),
new CopyPlugin({
patterns: [
// The extension manifest
{from: "src/manifest.json", to: "../manifest.json"}
]}),
]
};
const [workerConfig, extensionConfig] = commonConfigs;
workerConfig.mode = 'production';
// Only merge the devConfig with the extensionConfig
export default [
workerConfig, // Build the worker first
merge(extensionConfig, prodConfig) // apply the devConfig only to the extensionConfig
];