allerta-vvf/server/resources/webpack.prod.js

130 lines
3.8 KiB
JavaScript
Raw Normal View History

2021-04-05 18:23:53 +02:00
const { merge } = require("webpack-merge");
const common = require("./webpack.common.js");
2021-03-01 17:24:17 +01:00
const TerserPlugin = require("terser-webpack-plugin");
2021-02-20 21:05:03 +01:00
const SentryWebpackPlugin = require("@sentry/webpack-plugin");
2021-04-05 18:23:53 +02:00
const AfterBuildPlugin = require("@fiverr/afterbuild-webpack-plugin");
const child_process = require("child_process");
const InjectPlugin = require("webpack-inject-plugin").default;
const colors = require("colors/safe");
const fs = require("fs");
const glob = require("glob");
2021-02-20 21:05:03 +01:00
2020-11-27 23:53:14 +01:00
function git(command) {
2021-04-05 18:23:53 +02:00
return child_process.execSync(`git ${command}`, { encoding: "utf8" }).trim();
2020-11-27 23:53:14 +01:00
}
2021-04-05 18:23:53 +02:00
var webpack = require("webpack");
2020-11-27 23:53:14 +01:00
2021-02-20 21:05:03 +01:00
if (!fs.existsSync("config.json")) {
fs.copyFileSync("config_sample.json", "config.json");
}
const removeSourceMapUrlAfterBuild = () => {
//based on @rbarilani https://github.com/rbarilani/remove-source-map-url-webpack-plugin
glob("./dist/*.js", function (er, files) {
let countMatchAssets = 0;
files.push("../sw.js");
//console.log(files);
files.forEach((key) => {
countMatchAssets += 1;
2021-04-05 18:23:53 +02:00
let asset = fs.readFileSync(key, "utf8");
2021-02-20 21:05:03 +01:00
let source = asset.split("//# sourceMappingURL=")[0].replace(/\n$/, "");
fs.writeFileSync(key, source);
});
if (countMatchAssets) {
console.log(colors.green(`remove-source-map-url: ${countMatchAssets} asset(s) processed`));
}
});
glob("./dist/*.js.map", function (er, files) {
files.push("../sw.js.map");
files.forEach((key) => {
fs.unlinkSync(key);
});
});
}
2021-04-05 18:57:52 +02:00
var configFile = require("./config.json");
const sentry_enabled = configFile.sentry_enabled &&
configFile.sentry_auth_token &&
configFile.sentry_organization &&
configFile.sentry_project;
2021-02-20 21:05:03 +01:00
var prod_config = {
2021-04-05 18:23:53 +02:00
mode: "production",
2021-02-20 21:05:03 +01:00
devtool: false,
module: {
rules: [
{
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: {
2021-04-05 18:23:53 +02:00
loader: "babel-loader",
2021-02-20 21:05:03 +01:00
options: {
2021-04-05 18:23:53 +02:00
presets: ["@babel/preset-env"],
plugins: ["@babel/plugin-transform-runtime"]
2020-11-27 23:53:14 +01:00
}
}
2021-02-20 21:05:03 +01:00
}
]
},
plugins: [],
optimization: {
mergeDuplicateChunks: true,
minimize: true,
2021-03-01 17:24:17 +01:00
minimizer: [new TerserPlugin({
2021-02-20 21:05:03 +01:00
parallel: true,
extractComments: true,
sourceMap: sentry_enabled ? true : false
})]
}
};
module.exports = (env) => {
2021-04-05 18:23:53 +02:00
//run webpack build with "--env sentry_environment=custom-sentry-env" to replace Sentry environment
2021-02-20 21:05:03 +01:00
if(env.sentry_environment){
console.log(colors.green("INFO using custom sentry_environment "+env.sentry_environment));
2021-04-05 18:57:52 +02:00
configFile.sentry_environment = env.sentry_environment;
2021-02-20 21:05:03 +01:00
}
2021-04-05 18:57:52 +02:00
if(!configFile.sentry_environment){
configFile.sentry_environment = "prod";
2021-02-20 21:05:03 +01:00
}
if(sentry_enabled){
prod_config.plugins.push(
new webpack.SourceMapDevToolPlugin({
2021-04-05 18:23:53 +02:00
filename: "[file].map"
2021-02-20 21:05:03 +01:00
}),
new SentryWebpackPlugin({
2021-04-05 18:57:52 +02:00
authToken: configFile.sentry_auth_token,
org: configFile.sentry_organization,
project: configFile.sentry_project,
2021-04-05 18:23:53 +02:00
urlPrefix: "~/dist",
include: "./dist",
setCommits: {
auto: true
2021-02-22 16:10:33 +01:00
},
2021-04-05 18:23:53 +02:00
release: "allerta-vvf-frontend@"+git("describe --always")
2021-02-20 21:05:03 +01:00
}),
new AfterBuildPlugin(removeSourceMapUrlAfterBuild),
new InjectPlugin(function() {
return "import './src/sentry.js';";
2021-04-05 18:23:53 +02:00
},{ entryName: "main" })
2021-02-20 21:05:03 +01:00
);
console.log(colors.green("INFO Sentry Webpack plugins enabled"));
}
2021-02-20 21:05:03 +01:00
prod_config.plugins.push(
new webpack.EnvironmentPlugin({
2021-04-05 18:23:53 +02:00
GIT_VERSION: git("describe --always"),
GIT_AUTHOR_DATE: git("log -1 --format=%aI"),
2021-02-20 21:05:03 +01:00
BUNDLE_DATE: Date.now(),
2021-04-05 18:23:53 +02:00
BUNDLE_MODE: "production",
2021-04-05 18:57:52 +02:00
config: configFile
2021-02-20 21:05:03 +01:00
})
);
return merge(common, prod_config);
}