accounts-frontend/webpack.config.js

337 lines
11 KiB
JavaScript
Raw Normal View History

2019-12-07 16:58:52 +05:30
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-env node */
2019-12-07 16:58:52 +05:30
require('@babel/register')({
2020-05-24 04:38:24 +05:30
extensions: ['.es6', '.es', '.jsx', '.js', '.mjs', '.ts', '.tsx'],
2019-12-07 16:58:52 +05:30
});
const path = require('path');
const webpack = require('webpack');
2019-09-02 10:57:52 +05:30
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const SitemapPlugin = require('sitemap-webpack-plugin').default;
const CSPPlugin = require('csp-webpack-plugin');
2019-11-09 17:41:09 +05:30
const WebpackBar = require('webpackbar');
const SpeedMeasurePlugin = require('speed-measure-webpack-plugin');
const EagerImportsPlugin = require('eager-imports-webpack-plugin').default;
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
2019-11-09 18:12:02 +05:30
const config = require('./config');
2020-06-05 22:11:42 +05:30
const SUPPORTED_LANGUAGES = Object.keys(require('app/i18n').default);
const localeFlags = require('app/components/i18n/localeFlags').default;
const rootPath = path.resolve('./packages');
2019-12-30 13:51:28 +05:30
const outputPath = path.join(__dirname, 'build');
2019-12-09 13:17:24 +05:30
const isProduction = process.env.NODE_ENV === 'production';
const isAnalyze = process.argv.some((arg) => arg === '--analyze');
2016-10-09 23:53:00 +05:30
const isDockerized = !!process.env.DOCKERIZED;
2020-01-02 20:15:55 +05:30
const isStorybook = process.env.APP_ENV === 'storybook';
const isCI = !!process.env.CI;
const isSilent = isCI || process.argv.some((arg) => /quiet/.test(arg));
const isCspEnabled = false;
2020-01-02 20:15:55 +05:30
const enableDll = !isProduction && !isStorybook;
process.env.NODE_ENV = isProduction ? 'production' : 'development';
2019-07-01 09:09:59 +05:30
2019-11-09 17:59:08 +05:30
const smp = new SpeedMeasurePlugin();
2016-11-27 16:19:48 +05:30
const webpackConfig = {
2020-05-24 04:38:24 +05:30
mode: isProduction ? 'production' : 'development',
2020-05-24 04:38:24 +05:30
cache: true,
2020-05-24 04:38:24 +05:30
entry: {
app: path.join(__dirname, 'packages/app'),
},
2020-05-24 04:38:24 +05:30
output: {
path: outputPath,
publicPath: '/',
filename: '[name].js?[hash]',
},
2020-05-24 04:38:24 +05:30
resolve: {
modules: [rootPath, 'node_modules'],
extensions: ['.js', '.jsx', '.json', '.ts', '.tsx'],
alias: {
'react-dom': isProduction ? 'react-dom' : '@hot-loader/react-dom',
},
2019-11-09 17:41:09 +05:30
},
2020-05-24 04:38:24 +05:30
devtool: 'cheap-module-source-map',
2020-05-24 04:38:24 +05:30
stats: {
hash: false,
version: false,
timings: true,
assets: false,
chunks: false,
modules: false,
reasons: false,
children: false,
source: false,
errors: true,
errorDetails: true,
warnings: false,
publicPath: false,
},
2020-05-24 04:38:24 +05:30
plugins: [
new WebpackBar(),
new webpack.DefinePlugin({
'window.SENTRY_DSN': config.sentryDSN ? JSON.stringify(config.sentryDSN) : undefined,
'window.GA_ID': config.ga && config.ga.id ? JSON.stringify(config.ga.id) : undefined,
}),
new webpack.EnvironmentPlugin({
NODE_ENV: process.env.NODE_ENV,
__VERSION__: config.version || '',
}),
new HtmlWebpackPlugin({
template: 'packages/app/index.ejs',
favicon: 'packages/app/favicon.ico',
scripts: enableDll ? ['/dll/vendor.dll.js'] : [],
hash: false, // webpack does this for all our assets automagically
filename: 'index.html',
inject: false,
minify: {
collapseWhitespace: isProduction,
},
isCspEnabled,
}),
new SitemapPlugin(
'https://account.ely.by',
['/', '/register', '/resend-activation', '/activation', '/forgot-password', '/rules'],
{
lastMod: true,
changeFreq: 'weekly',
},
),
// restrict webpack import context, to create chunks only for supported locales
// @see services/i18n/intlPolyfill.js
new webpack.ContextReplacementPlugin(/locale-data/, new RegExp(`/(${SUPPORTED_LANGUAGES.join('|')})\\.js$`)),
// @see components/i18n/localeFlags.js
new webpack.ContextReplacementPlugin(
/flag-icon-css\/flags\/4x3/,
new RegExp(`/(${localeFlags.getCountryList().join('|')})\\.svg`),
),
],
2020-05-24 04:38:24 +05:30
module: {
rules: [
{
test: /\.s?css$/,
use: [
{
loader: 'style-loader',
options: {
// style-loader@1.1.2 is still buggy. It breaks our icon styles
// (vertical align is broken and styles applied multiple times)
// so we can not use it and it's new `esModule` options
// esModule: true,
},
},
{
loader: 'css-loader',
options: {
modules: {
localIdentName: isProduction ? '[hash:base64:5]' : '[path][name]-[local]',
},
esModule: true,
importLoaders: 2,
sourceMap: !isProduction,
},
},
{
loader: 'sass-loader',
options: {
sourceMap: !isProduction,
},
},
{
loader: 'postcss-loader',
options: {
ident: 'postcss',
sourceMap: !isProduction,
config: { path: __dirname },
},
},
],
2019-12-25 16:45:50 +05:30
},
2020-05-24 04:38:24 +05:30
{
test: /\.[tj]sx?$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: {
envName: 'webpack',
cacheDirectory: true,
},
},
],
},
2020-05-24 04:38:24 +05:30
{
test: /\.(png|gif|jpg|svg)$/,
loader: 'file-loader',
query: {
name: 'assets/[name].[ext]?[hash]',
},
},
2020-05-24 04:38:24 +05:30
{
test: /\.(woff|woff2|ttf)$/,
loader: 'file-loader',
query: {
name: 'assets/fonts/[name].[ext]?[hash]',
},
},
2020-05-24 04:38:24 +05:30
{
test: /\.html$/,
loader: 'html-loader',
},
{
// this is consumed by postcss-import
// @see postcss.config.js
test: /\.font\.(js|json)$/,
type: 'javascript/auto',
use: ['fontgen-loader'],
},
],
2020-05-24 04:38:24 +05:30
},
2016-01-03 14:41:08 +05:30
2020-05-24 04:38:24 +05:30
resolveLoader: {
alias: {
'fontgen-loader': path.resolve('./packages/webpack-utils/fontgen-loader'),
},
},
};
2019-11-09 17:59:08 +05:30
if (isAnalyze) {
2020-05-24 04:38:24 +05:30
webpackConfig.plugins.push(new BundleAnalyzerPlugin());
2019-11-09 17:59:08 +05:30
}
if (isProduction) {
2020-05-24 04:38:24 +05:30
if (!isStorybook) {
let cssExtractApplied = false;
2019-12-30 13:53:22 +05:30
2020-05-24 04:38:24 +05:30
webpackConfig.module.rules.forEach((rule) => {
if (rule.use && (rule.use[0] === 'style-loader' || rule.use[0].loader === 'style-loader')) {
// replace `style-loader` with `MiniCssExtractPlugin`
rule.use[0] = MiniCssExtractPlugin.loader;
cssExtractApplied = true;
}
});
2020-05-24 04:38:24 +05:30
if (!cssExtractApplied) {
throw new Error('Can not locate style-loader to replace it with mini-css-extract-plugin loader');
}
2020-05-24 04:38:24 +05:30
webpackConfig.plugins.push(
new MiniCssExtractPlugin({
filename: '[name].css?[hash]',
chunkFilename: '[id].css?[hash]',
}),
);
}
2019-12-30 13:53:22 +05:30
2020-05-24 04:38:24 +05:30
webpackConfig.devtool = 'hidden-source-map';
2020-05-24 04:38:24 +05:30
webpackConfig.optimization = {
moduleIds: 'hashed',
runtimeChunk: 'single',
splitChunks: {
cacheGroups: {
vendor: {
test: (m) =>
String(m.context).includes('node_modules') &&
// icons and intl with relateed polyfills are allowed
// to be splitted to other chunks
!/\/(flag-icon-css|intl|@formatjs)\//.test(String(m.context)),
name: 'vendors',
chunks: 'all',
},
},
},
2020-05-24 04:38:24 +05:30
};
} else {
2020-01-02 20:15:55 +05:30
webpackConfig.plugins.push(
2020-05-24 04:38:24 +05:30
// force webpack to use mode: eager chunk imports in dev mode
// this will improve build performance
// this mode will be default for dev builds in webpack 5
new EagerImportsPlugin(),
2020-01-02 20:15:55 +05:30
);
2020-05-24 04:38:24 +05:30
if (enableDll) {
webpackConfig.plugins.push(
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./dll/vendor.json'),
}),
);
}
webpackConfig.devServer = {
host: 'localhost',
port: 8080,
proxy: config.apiHost && {
'/api': {
target: config.apiHost,
changeOrigin: true, // add host http-header, based on target
secure: false, // allow self-signed certs
},
},
hot: true,
inline: true,
historyApiFallback: true,
};
}
if (isCspEnabled) {
2020-05-24 04:38:24 +05:30
webpackConfig.plugins.push(
new CSPPlugin({
'default-src': "'none'",
'style-src': ["'self'", "'unsafe-inline'"],
'script-src': [
"'self'",
"'nonce-edge-must-die'",
"'unsafe-inline'",
'https://www.google-analytics.com',
'https://recaptcha.net/recaptcha/',
'https://www.gstatic.com/recaptcha/',
'https://www.gstatic.cn/recaptcha/',
],
'img-src': ["'self'", 'data:', 'www.google-analytics.com'],
'font-src': ["'self'", 'data:'],
'connect-src': ["'self'", 'https://sentry.ely.by'].concat(isProduction ? [] : ['ws://localhost:8080']),
'frame-src': ['https://www.google.com/recaptcha/', 'https://recaptcha.net/recaptcha/'],
'report-uri': 'https://sentry.ely.by/api/2/csp-report/?sentry_key=088e7718236a4f91937a81fb319a93f6',
}),
);
}
if (isDockerized) {
2020-05-24 04:38:24 +05:30
webpackConfig.watchOptions = {
poll: 2000,
};
webpackConfig.devServer.host = '0.0.0.0';
2016-01-08 18:44:11 +05:30
}
if (isSilent) {
2020-05-24 04:38:24 +05:30
webpackConfig.stats = {
hash: false,
version: false,
timings: true,
assets: false,
chunks: false,
modules: false,
reasons: false,
children: false,
source: false,
errors: true,
errorDetails: true,
warnings: false,
publicPath: false,
};
}
2019-11-09 17:41:09 +05:30
module.exports = smp.wrap(webpackConfig);