telegram-tt/webpack.config.js

201 lines
5.5 KiB
JavaScript
Raw Normal View History

2021-04-09 12:29:59 +02:00
const path = require('path');
const dotenv = require('dotenv');
const {
DefinePlugin,
EnvironmentPlugin,
ProvidePlugin,
ContextReplacementPlugin,
NormalModuleReplacementPlugin,
} = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
2021-04-09 12:29:59 +02:00
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { GitRevisionPlugin } = require('git-revision-webpack-plugin');
const StatoscopeWebpackPlugin = require('@statoscope/webpack-plugin').default;
const WebpackContextExtension = require('./dev/webpackContextExtension');
2022-02-25 21:52:32 +01:00
const appVersion = require('./package.json').version;
2021-04-09 12:29:59 +02:00
const {
HEAD,
APP_ENV = 'production',
APP_MOCKED_CLIENT = '',
} = process.env;
2021-04-09 12:29:59 +02:00
dotenv.config();
module.exports = (_env, { mode = 'production' }) => {
2021-04-09 12:29:59 +02:00
return {
mode,
2021-04-09 12:29:59 +02:00
entry: './src/index.tsx',
target: 'web',
2021-04-09 12:29:59 +02:00
devServer: {
port: 1234,
host: '0.0.0.0',
2022-02-02 22:48:08 +01:00
allowedHosts: "all",
hot: false,
2022-02-02 22:48:08 +01:00
static: [
{
directory: path.resolve(__dirname, 'public'),
},
{
directory: path.resolve(__dirname, 'node_modules/emoji-data-ios'),
},
{
directory: path.resolve(__dirname, 'node_modules/opus-recorder/dist'),
},
{
directory: path.resolve(__dirname, 'src/lib/webp'),
},
{
directory: path.resolve(__dirname, 'src/lib/rlottie'),
},
{
directory: path.resolve(__dirname, 'src/lib/secret-sauce'),
},
],
devMiddleware: {
stats: 'minimal',
},
2021-04-09 12:29:59 +02:00
},
2021-04-09 12:29:59 +02:00
output: {
filename: '[name].[contenthash].js',
chunkFilename: '[id].[chunkhash].js',
assetModuleFilename: '[name].[contenthash][ext]',
path: path.resolve(__dirname, 'dist'),
clean: true,
2021-04-09 12:29:59 +02:00
},
2021-04-09 12:29:59 +02:00
module: {
rules: [
{
test: /\.(ts|tsx|js)$/,
loader: 'babel-loader',
exclude: /node_modules/,
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
importLoaders: 1,
},
},
'postcss-loader',
],
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
modules: {
exportLocalsConvention: 'camelCase',
auto: true,
localIdentName: mode === 'production' ? '[hash:base64]' : '[name]__[local]'
}
}
},
2021-04-09 12:29:59 +02:00
'postcss-loader',
'sass-loader',
],
},
{
test: /\.(woff(2)?|ttf|eot|svg|png|jpg|tgs)(\?v=\d+\.\d+\.\d+)?$/,
type: 'asset/resource',
2021-04-09 12:29:59 +02:00
},
{
test: /\.wasm$/,
type: 'asset/resource',
2021-04-09 12:29:59 +02:00
},
{
test: /\.(txt|tl)$/i,
type: 'asset/source',
2021-04-09 12:29:59 +02:00
},
],
},
2021-04-09 12:29:59 +02:00
resolve: {
extensions: ['.js', '.ts', '.tsx'],
fallback: {
path: require.resolve('path-browserify'),
os: require.resolve('os-browserify/browser'),
buffer: require.resolve('buffer/'),
fs: false,
},
2021-04-09 12:29:59 +02:00
},
2021-04-09 12:29:59 +02:00
plugins: [
// Clearing of the unused files for code highlight for smaller chunk count
new ContextReplacementPlugin(
/highlight\.js\/lib\/languages/,
/^((?!\.js\.js).)*$/
),
...(APP_MOCKED_CLIENT === '1' ? [new NormalModuleReplacementPlugin(
/src\/lib\/gramjs\/client\/TelegramClient\.js/,
'./MockClient.ts'
)] : []),
new HtmlWebpackPlugin({
appName: APP_ENV === 'production' ? 'Telegram Web' : 'Telegram Web Beta',
appleIcon: APP_ENV === 'production' ? 'apple-touch-icon' : 'apple-touch-icon-dev',
mainIcon: APP_ENV === 'production' ? 'icon-192x192' : 'icon-dev-192x192',
2021-04-09 12:29:59 +02:00
template: 'src/index.html',
}),
new MiniCssExtractPlugin({
filename: '[name].[contenthash].css',
chunkFilename: '[name].[chunkhash].css',
ignoreOrder: true,
}),
new EnvironmentPlugin({
APP_ENV,
APP_MOCKED_CLIENT,
2022-02-20 13:05:25 +01:00
APP_NAME: null,
2022-02-25 21:52:32 +01:00
APP_VERSION: appVersion,
TELEGRAM_T_API_ID: undefined,
TELEGRAM_T_API_HASH: undefined,
TEST_SESSION: null,
2021-04-09 12:29:59 +02:00
}),
new DefinePlugin({
APP_REVISION: DefinePlugin.runtimeValue(() => {
const { branch, commit } = getGitMetadata();
const shouldDisplayCommit = APP_ENV === 'staging' || !branch || branch === 'HEAD';
return JSON.stringify(shouldDisplayCommit ? commit : branch);
}, mode === 'development' ? true : []),
}),
new ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
}),
new StatoscopeWebpackPlugin({
statsOptions: {
context: __dirname,
},
saveReportTo: path.resolve('./public/statoscope-report.html'),
saveStatsTo: path.resolve('./public/build-stats.json'),
normalizeStats: true,
open: 'file',
extensions: [new WebpackContextExtension()],
}),
2021-04-09 12:29:59 +02:00
],
devtool: 'source-map',
2021-04-09 12:29:59 +02:00
...(APP_ENV !== 'production' && {
2021-04-09 12:29:59 +02:00
optimization: {
chunkIds: 'named',
}
2021-04-09 12:29:59 +02:00
}),
};
};
function getGitMetadata() {
const gitRevisionPlugin = new GitRevisionPlugin();
const branch = HEAD || gitRevisionPlugin.branch();
const commit = gitRevisionPlugin.commithash().substring(0, 7);
return { branch, commit };
}