1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-23 11:56:00 +01:00
bitwarden-browser/webpack.config.js

268 lines
7.6 KiB
JavaScript
Raw Normal View History

2021-12-17 15:57:11 +01:00
const path = require("path");
const fs = require("fs");
const webpack = require("webpack");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const HtmlWebpackInjector = require("html-webpack-injector");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const { AngularWebpackPlugin } = require("@ngtools/webpack");
const pjson = require("./package.json");
const config = require("./config.js");
2018-06-05 05:10:41 +02:00
2021-12-17 15:57:11 +01:00
const ENV = process.env.ENV == null ? "development" : process.env.ENV;
const NODE_ENV = process.env.NODE_ENV == null ? "development" : process.env.NODE_ENV;
const envConfig = config.load(ENV);
config.log(envConfig);
2018-06-05 05:10:41 +02:00
const moduleRules = [
2021-12-17 15:57:11 +01:00
{
test: /\.ts$/,
enforce: "pre",
loader: "tslint-loader",
},
{
test: /\.(html)$/,
loader: "html-loader",
},
{
test: /.(ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
exclude: /loading(|-white).svg/,
generator: {
filename: "fonts/[name][ext]",
2018-06-05 05:10:41 +02:00
},
2021-12-17 15:57:11 +01:00
type: "asset/resource",
},
{
test: /\.(jpe?g|png|gif|svg|webp|avif)$/i,
2021-12-17 15:57:11 +01:00
exclude: /.*(fontawesome-webfont)\.svg/,
generator: {
filename: "images/[name][ext]",
},
2021-12-17 15:57:11 +01:00
type: "asset/resource",
},
{
test: /\.scss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
"css-loader",
"sass-loader",
],
},
{
test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/,
loader: "@ngtools/webpack",
},
2018-06-05 05:10:41 +02:00
];
const plugins = [
2021-12-17 15:57:11 +01:00
new CleanWebpackPlugin(),
// ref: https://github.com/angular/angular/issues/20357
new webpack.ContextReplacementPlugin(
/\@angular(\\|\/)core(\\|\/)fesm5/,
path.resolve(__dirname, "./src")
),
new HtmlWebpackPlugin({
template: "./src/index.html",
filename: "index.html",
chunks: ["theme_head", "app/polyfills", "app/vendor", "app/main"],
}),
new HtmlWebpackInjector(),
new HtmlWebpackPlugin({
template: "./src/connectors/duo.html",
filename: "duo-connector.html",
chunks: ["connectors/duo"],
}),
new HtmlWebpackPlugin({
template: "./src/connectors/u2f.html",
filename: "u2f-connector.html",
chunks: ["connectors/u2f"],
}),
new HtmlWebpackPlugin({
template: "./src/connectors/webauthn.html",
filename: "webauthn-connector.html",
chunks: ["connectors/webauthn"],
}),
new HtmlWebpackPlugin({
template: "./src/connectors/webauthn-mobile.html",
filename: "webauthn-mobile-connector.html",
chunks: ["connectors/webauthn"],
}),
new HtmlWebpackPlugin({
template: "./src/connectors/webauthn-fallback.html",
filename: "webauthn-fallback-connector.html",
chunks: ["connectors/webauthn-fallback"],
}),
new HtmlWebpackPlugin({
template: "./src/connectors/sso.html",
filename: "sso-connector.html",
chunks: ["connectors/sso"],
}),
new HtmlWebpackPlugin({
template: "./src/connectors/captcha.html",
filename: "captcha-connector.html",
chunks: ["connectors/captcha"],
}),
new HtmlWebpackPlugin({
template: "./src/connectors/captcha-mobile.html",
filename: "captcha-mobile-connector.html",
chunks: ["connectors/captcha"],
}),
new CopyWebpackPlugin({
patterns: [
{ from: "./src/.nojekyll" },
{ from: "./src/manifest.json" },
{ from: "./src/favicon.ico" },
{ from: "./src/browserconfig.xml" },
{ from: "./src/app-id.json" },
{ from: "./src/404.html" },
{ from: "./src/404", to: "404" },
{ from: "./src/images", to: "images" },
{ from: "./src/locales", to: "locales" },
{ from: "./src/scripts", to: "scripts" },
{ from: "./node_modules/qrious/dist/qrious.min.js", to: "scripts" },
{ from: "./node_modules/braintree-web-drop-in/dist/browser/dropin.js", to: "scripts" },
{
from: "./src/version.json",
transform(content, path) {
return content.toString().replace("process.env.APPLICATION_VERSION", pjson.version);
},
},
],
}),
new MiniCssExtractPlugin({
filename: "[name].[contenthash].css",
chunkFilename: "[id].[contenthash].css",
}),
new webpack.EnvironmentPlugin({
ENV: ENV,
NODE_ENV: NODE_ENV === "production" ? "production" : "development",
APPLICATION_VERSION: pjson.version,
CACHE_TAG: Math.random().toString(36).substring(7),
URLS: envConfig["urls"] ?? {},
STRIPE_KEY: envConfig["stripeKey"] ?? "",
BRAINTREE_KEY: envConfig["braintreeKey"] ?? "",
PAYPAL_CONFIG: envConfig["paypal"] ?? {},
}),
new webpack.ProvidePlugin({
process: "process/browser",
}),
new AngularWebpackPlugin({
tsConfigPath: "tsconfig.json",
entryModule: "src/app/app.module#AppModule",
sourceMap: true,
}),
];
2018-06-05 05:10:41 +02:00
2018-09-18 17:59:03 +02:00
// ref: https://webpack.js.org/configuration/dev-server/#devserver
2021-12-17 15:57:11 +01:00
let certSuffix = fs.existsSync("dev-server.local.pem") ? ".local" : ".shared";
const devServer =
NODE_ENV !== "development"
? {}
: {
server: {
type: "https",
options: {
key: fs.readFileSync("dev-server" + certSuffix + ".pem"),
cert: fs.readFileSync("dev-server" + certSuffix + ".pem"),
},
2021-12-09 22:12:53 +01:00
},
2021-12-17 15:57:11 +01:00
// host: '192.168.1.9',
proxy: {
"/api": {
target: envConfig.dev?.proxyApi,
2021-12-17 15:57:11 +01:00
pathRewrite: { "^/api": "" },
secure: false,
2021-12-17 15:57:11 +01:00
changeOrigin: true,
},
"/identity": {
target: envConfig.dev?.proxyIdentity,
2021-12-17 15:57:11 +01:00
pathRewrite: { "^/identity": "" },
secure: false,
2021-12-17 15:57:11 +01:00
changeOrigin: true,
},
"/events": {
target: envConfig.dev?.proxyEvents,
2021-12-17 15:57:11 +01:00
pathRewrite: { "^/events": "" },
secure: false,
2021-12-17 15:57:11 +01:00
changeOrigin: true,
},
"/notifications": {
target: envConfig.dev?.proxyNotifications,
2021-12-17 15:57:11 +01:00
pathRewrite: { "^/notifications": "" },
secure: false,
2021-12-17 15:57:11 +01:00
changeOrigin: true,
},
},
2021-12-17 15:57:11 +01:00
hot: false,
allowedHosts: envConfig.dev?.allowedHosts ?? "auto",
client: {
overlay: {
2021-12-09 22:12:53 +01:00
errors: true,
warnings: false,
2021-12-17 15:57:11 +01:00
},
2021-12-09 22:12:53 +01:00
},
2021-12-17 15:57:11 +01:00
};
const webpackConfig = {
2021-12-17 15:57:11 +01:00
mode: NODE_ENV,
devtool: "source-map",
devServer: devServer,
entry: {
"app/polyfills": "./src/app/polyfills.ts",
"app/main": "./src/app/main.ts",
"connectors/u2f": "./src/connectors/u2f.js",
"connectors/webauthn": "./src/connectors/webauthn.ts",
"connectors/webauthn-fallback": "./src/connectors/webauthn-fallback.ts",
"connectors/duo": "./src/connectors/duo.ts",
"connectors/sso": "./src/connectors/sso.ts",
"connectors/captcha": "./src/connectors/captcha.ts",
theme_head: "./src/theme.js",
},
externals: {
u2f: "u2f",
},
optimization: {
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: "app/vendor",
chunks: (chunk) => {
return chunk.name === "app/main";
},
2018-07-21 04:46:03 +02:00
},
2021-12-17 15:57:11 +01:00
},
2018-07-21 04:46:03 +02:00
},
2021-12-17 15:57:11 +01:00
minimizer: [
new TerserPlugin({
terserOptions: {
safari10: true,
2021-12-09 22:12:53 +01:00
},
2021-12-17 15:57:11 +01:00
}),
],
},
resolve: {
extensions: [".ts", ".js"],
symlinks: false,
modules: [path.resolve("node_modules")],
fallback: {
buffer: false,
util: require.resolve("util/"),
assert: false,
2018-06-05 05:10:41 +02:00
},
2021-12-17 15:57:11 +01:00
},
output: {
filename: "[name].[contenthash].js",
path: path.resolve(__dirname, "build"),
},
module: { rules: moduleRules },
plugins: plugins,
2018-06-05 05:10:41 +02:00
};
module.exports = webpackConfig;