2016-01-03 01:54:07 +05:30
|
|
|
/* eslint-env node */
|
|
|
|
|
2019-06-30 20:49:28 +05:30
|
|
|
require('@babel/register');
|
2017-12-31 01:14:32 +05:30
|
|
|
|
2016-10-04 11:07:46 +05:30
|
|
|
const path = require('path');
|
|
|
|
const webpack = require('webpack');
|
2019-06-09 13:08:59 +05:30
|
|
|
const chalk = require('chalk');
|
2019-09-02 10:57:52 +05:30
|
|
|
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
|
2016-10-04 11:07:46 +05:30
|
|
|
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
2017-12-23 04:30:52 +05:30
|
|
|
const SitemapPlugin = require('sitemap-webpack-plugin').default;
|
2017-12-31 17:58:31 +05:30
|
|
|
const CSPPlugin = require('csp-webpack-plugin');
|
2016-01-03 01:54:07 +05:30
|
|
|
|
2017-06-09 01:14:28 +05:30
|
|
|
const SUPPORTED_LANGUAGES = Object.keys(require('./src/i18n/index.json'));
|
2019-07-01 09:09:59 +05:30
|
|
|
const localeFlags = require('./src/components/i18n/localeFlags').default;
|
2016-07-23 02:28:31 +05:30
|
|
|
const rootPath = path.resolve('./src');
|
2016-11-27 16:19:48 +05:30
|
|
|
const outputPath = path.join(__dirname, 'dist');
|
2016-12-07 02:36:45 +05:30
|
|
|
|
2017-12-17 19:41:29 +05:30
|
|
|
let config = {};
|
2016-10-09 23:53:00 +05:30
|
|
|
try {
|
|
|
|
config = require('./config/env.js');
|
|
|
|
} catch (err) {
|
2019-07-01 09:09:59 +05:30
|
|
|
console.log(
|
|
|
|
chalk.yellow('\nCan not find config/env.js. Running with defaults\n\n')
|
|
|
|
);
|
2019-06-09 13:08:59 +05:30
|
|
|
|
|
|
|
if (err.code !== 'MODULE_NOT_FOUND') {
|
|
|
|
console.error(err);
|
|
|
|
}
|
2016-10-09 23:53:00 +05:30
|
|
|
}
|
|
|
|
|
2019-09-02 10:57:52 +05:30
|
|
|
// TODO: add progress plugin
|
|
|
|
// TODO: configure bundle analyzer
|
|
|
|
|
2016-01-08 18:44:11 +05:30
|
|
|
/**
|
|
|
|
* TODO: https://babeljs.io/docs/plugins/
|
|
|
|
* TODO: отдельные конфиги для env (аля https://github.com/davezuko/react-redux-starter-kit)
|
2016-01-18 10:58:43 +05:30
|
|
|
* TODO: dev tools https://github.com/freeqaz/redux-simple-router-example/blob/master/index.jsx
|
2016-01-08 18:44:11 +05:30
|
|
|
* https://github.com/glenjamin/ultimate-hot-reloading-example ( обратить внимание на плагины babel )
|
|
|
|
* https://github.com/gajus/react-css-modules ( + BrowserSync)
|
2016-01-27 10:34:03 +05:30
|
|
|
* https://github.com/reactuate/reactuate
|
2016-03-21 11:46:37 +05:30
|
|
|
* https://github.com/insin/nwb
|
2016-01-08 18:44:11 +05:30
|
|
|
*
|
|
|
|
* Inspiration projects:
|
|
|
|
* https://github.com/davezuko/react-redux-starter-kit
|
|
|
|
*/
|
2016-01-03 01:54:07 +05:30
|
|
|
|
2016-07-23 02:28:31 +05:30
|
|
|
const isProduction = process.argv.some((arg) => arg === '-p');
|
2016-01-03 01:54:07 +05:30
|
|
|
|
2016-07-23 02:28:31 +05:30
|
|
|
const isTest = process.argv.some((arg) => arg.indexOf('karma') !== -1);
|
2016-01-03 01:54:07 +05:30
|
|
|
|
2016-07-23 02:28:31 +05:30
|
|
|
const isDockerized = !!process.env.DOCKERIZED;
|
2016-12-04 18:42:36 +05:30
|
|
|
const isCI = !!process.env.CI;
|
2017-08-01 23:13:16 +05:30
|
|
|
const isSilent = isCI || process.argv.some((arg) => /quiet/.test(arg));
|
2017-12-31 17:58:31 +05:30
|
|
|
const isCspEnabled = false;
|
2016-05-05 13:45:53 +05:30
|
|
|
|
2016-02-13 14:35:21 +05:30
|
|
|
process.env.NODE_ENV = isProduction ? 'production' : 'development';
|
2019-07-01 09:09:59 +05:30
|
|
|
|
2016-05-01 23:20:55 +05:30
|
|
|
if (isTest) {
|
|
|
|
process.env.NODE_ENV = 'test';
|
|
|
|
}
|
2016-01-31 21:33:04 +05:30
|
|
|
|
2016-11-27 16:19:48 +05:30
|
|
|
const webpackConfig = {
|
|
|
|
cache: true,
|
|
|
|
|
2016-01-03 01:54:07 +05:30
|
|
|
entry: {
|
2016-11-27 17:27:11 +05:30
|
|
|
app: path.join(__dirname, 'src')
|
2016-01-03 01:54:07 +05:30
|
|
|
},
|
|
|
|
|
|
|
|
output: {
|
2016-11-27 16:19:48 +05:30
|
|
|
path: outputPath,
|
2016-01-16 17:36:22 +05:30
|
|
|
publicPath: '/',
|
2016-07-30 22:10:07 +05:30
|
|
|
filename: '[name].js?[hash]'
|
2016-01-03 01:54:07 +05:30
|
|
|
},
|
|
|
|
|
|
|
|
resolve: {
|
2019-06-30 20:01:09 +05:30
|
|
|
modules: [rootPath, 'node_modules'],
|
|
|
|
extensions: ['.js', '.jsx', '.json']
|
2016-01-03 01:54:07 +05:30
|
|
|
},
|
|
|
|
|
2019-07-01 09:09:59 +05:30
|
|
|
externals: isTest
|
|
|
|
? {
|
|
|
|
sinon: 'sinon',
|
|
|
|
// http://airbnb.io/enzyme/docs/guides/webpack.html
|
|
|
|
cheerio: 'window',
|
|
|
|
'react/lib/ExecutionEnvironment': true,
|
|
|
|
'react/lib/ReactContext': true,
|
|
|
|
'react/addons': true
|
|
|
|
}
|
|
|
|
: {},
|
2016-05-01 23:20:55 +05:30
|
|
|
|
2017-12-31 17:58:31 +05:30
|
|
|
devtool: 'cheap-module-source-map',
|
2016-01-03 01:54:07 +05:30
|
|
|
|
|
|
|
plugins: [
|
|
|
|
new webpack.DefinePlugin({
|
2019-07-01 09:09:59 +05:30
|
|
|
'window.SENTRY_CDN': config.sentryCdn
|
|
|
|
? JSON.stringify(config.sentryCdn)
|
|
|
|
: undefined,
|
|
|
|
'window.GA_ID':
|
|
|
|
config.ga && config.ga.id
|
|
|
|
? JSON.stringify(config.ga.id)
|
2019-09-02 10:57:52 +05:30
|
|
|
: undefined
|
|
|
|
}),
|
|
|
|
new webpack.EnvironmentPlugin({
|
|
|
|
NODE_ENV: process.env.NODE_ENV,
|
|
|
|
APP_ENV: config.environment || process.env.NODE_ENV,
|
|
|
|
__VERSION__: config.version || '',
|
|
|
|
__DEV__: !isProduction,
|
|
|
|
__TEST__: isTest,
|
|
|
|
__PROD__: isProduction
|
2016-01-03 01:54:07 +05:30
|
|
|
}),
|
|
|
|
new HtmlWebpackPlugin({
|
2016-05-08 16:56:25 +05:30
|
|
|
template: 'src/index.ejs',
|
2016-01-04 11:32:13 +05:30
|
|
|
favicon: 'src/favicon.ico',
|
2017-01-02 19:07:02 +05:30
|
|
|
scripts: isProduction ? [] : ['/dll/vendor.dll.js'],
|
2016-10-25 12:04:41 +05:30
|
|
|
hash: false, // webpack does this for all our assets automagically
|
2016-01-03 01:54:07 +05:30
|
|
|
filename: 'index.html',
|
2016-05-08 16:56:25 +05:30
|
|
|
inject: false,
|
2016-01-03 01:54:07 +05:30
|
|
|
minify: {
|
|
|
|
collapseWhitespace: isProduction
|
2017-12-31 17:58:31 +05:30
|
|
|
},
|
2019-07-01 09:09:59 +05:30
|
|
|
isCspEnabled
|
2017-12-23 04:30:52 +05:30
|
|
|
}),
|
2019-07-01 09:09:59 +05:30
|
|
|
new SitemapPlugin(
|
|
|
|
'https://account.ely.by',
|
|
|
|
[
|
|
|
|
'/',
|
|
|
|
'/register',
|
|
|
|
'/resend-activation',
|
|
|
|
'/activation',
|
|
|
|
'/forgot-password',
|
|
|
|
'/rules'
|
|
|
|
],
|
|
|
|
{
|
|
|
|
lastMod: true,
|
|
|
|
changeFreq: 'weekly'
|
|
|
|
}
|
|
|
|
),
|
2017-06-09 01:14:28 +05:30
|
|
|
// restrict webpack import context, to create chunks only for supported locales
|
|
|
|
// @see services/i18n.js
|
|
|
|
new webpack.ContextReplacementPlugin(
|
2019-07-01 09:09:59 +05:30
|
|
|
/locale-data/,
|
|
|
|
new RegExp(`/(${SUPPORTED_LANGUAGES.join('|')})\\.js`)
|
2017-12-17 19:41:29 +05:30
|
|
|
),
|
2017-12-31 01:14:32 +05:30
|
|
|
// @see components/i18n/localeFlags.js
|
2017-12-17 19:41:29 +05:30
|
|
|
new webpack.ContextReplacementPlugin(
|
2019-07-01 09:09:59 +05:30
|
|
|
/flag-icon-css\/flags\/4x3/,
|
|
|
|
new RegExp(`/(${localeFlags.getCountryList().join('|')})\\.svg`)
|
|
|
|
)
|
2016-11-27 17:27:11 +05:30
|
|
|
],
|
2016-01-03 01:54:07 +05:30
|
|
|
|
|
|
|
module: {
|
2019-06-30 20:01:09 +05:30
|
|
|
rules: [
|
2016-01-03 01:54:07 +05:30
|
|
|
{
|
|
|
|
test: /\.scss$/,
|
2019-06-30 20:01:09 +05:30
|
|
|
use: [
|
|
|
|
'style-loader',
|
|
|
|
{
|
|
|
|
loader: 'css-loader',
|
2019-07-01 09:09:59 +05:30
|
|
|
options: {
|
|
|
|
modules: {
|
|
|
|
localIdentName: isProduction
|
|
|
|
? '[hash:base64:5]'
|
|
|
|
: '[path][name]-[local]'
|
|
|
|
},
|
|
|
|
importLoaders: 2,
|
|
|
|
sourceMap: !isProduction
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
loader: 'sass-loader',
|
|
|
|
options: {
|
|
|
|
sourceMap: !isProduction
|
|
|
|
}
|
2019-06-30 20:01:09 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
loader: 'postcss-loader',
|
|
|
|
options: {
|
|
|
|
ident: 'postcss',
|
2019-07-01 09:09:59 +05:30
|
|
|
sourceMap: !isProduction,
|
|
|
|
config: { path: __dirname }
|
2019-06-30 20:01:09 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2016-01-03 01:54:07 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.jsx?$/,
|
|
|
|
exclude: /node_modules/,
|
2019-06-30 20:49:28 +05:30
|
|
|
use: [
|
|
|
|
{
|
|
|
|
loader: 'babel-loader',
|
|
|
|
options: {
|
|
|
|
envName: 'webpack',
|
|
|
|
cacheDirectory: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2016-04-02 16:27:45 +05:30
|
|
|
},
|
|
|
|
{
|
2016-05-20 01:11:43 +05:30
|
|
|
test: /\.(png|gif|jpg|svg)$/,
|
2019-06-30 20:01:09 +05:30
|
|
|
loader: 'file-loader',
|
2016-07-23 02:28:31 +05:30
|
|
|
query: {
|
|
|
|
name: 'assets/[name].[ext]?[hash]'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2016-07-28 10:02:36 +05:30
|
|
|
test: /\.(woff|woff2|ttf)$/,
|
2019-06-30 20:01:09 +05:30
|
|
|
loader: 'file-loader',
|
2016-07-23 02:28:31 +05:30
|
|
|
query: {
|
|
|
|
name: 'assets/fonts/[name].[ext]?[hash]'
|
|
|
|
}
|
2016-05-08 16:56:25 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.html$/,
|
2019-06-30 20:01:09 +05:30
|
|
|
loader: 'html-loader'
|
2016-05-09 00:58:51 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.intl\.json$/,
|
2019-07-01 09:09:59 +05:30
|
|
|
type: 'javascript/auto',
|
|
|
|
use: ['babel-loader', 'intl-loader']
|
2016-05-22 17:36:57 +05:30
|
|
|
},
|
|
|
|
{
|
2019-07-01 09:09:59 +05:30
|
|
|
// this is consumed by postcss-import
|
|
|
|
// @see postcss.config.js
|
2016-05-22 17:36:57 +05:30
|
|
|
test: /\.font\.(js|json)$/,
|
2019-07-01 09:09:59 +05:30
|
|
|
type: 'javascript/auto',
|
|
|
|
use: ['fontgen-loader']
|
2016-01-03 01:54:07 +05:30
|
|
|
}
|
|
|
|
]
|
2016-01-03 14:41:08 +05:30
|
|
|
},
|
|
|
|
|
2016-05-09 00:58:51 +05:30
|
|
|
resolveLoader: {
|
|
|
|
alias: {
|
2019-07-01 09:09:59 +05:30
|
|
|
'intl-loader': path.resolve('./webpack-utils/intl-loader'),
|
|
|
|
'fontgen-loader': path.resolve('./webpack-utils/fontgen-loader')
|
2016-05-09 00:58:51 +05:30
|
|
|
}
|
2019-07-01 09:09:59 +05:30
|
|
|
}
|
2016-01-03 01:54:07 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
if (isProduction) {
|
2019-09-02 10:57:52 +05:30
|
|
|
webpackConfig.module.rules.forEach((rule) => {
|
|
|
|
if (rule.use && rule.use[0] === 'style-loader') {
|
|
|
|
// replace `style-loader` with `MiniCssExtractPlugin`
|
|
|
|
rule.use[0] = MiniCssExtractPlugin.loader;
|
2016-01-03 01:54:07 +05:30
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-11-27 17:27:11 +05:30
|
|
|
webpackConfig.plugins.push(
|
2019-09-02 10:57:52 +05:30
|
|
|
new MiniCssExtractPlugin({
|
|
|
|
filename: '[name].css?[hash]',
|
|
|
|
chunkFilename: '[id].css?[hash]',
|
2016-11-27 17:27:11 +05:30
|
|
|
})
|
|
|
|
);
|
2016-01-03 01:54:07 +05:30
|
|
|
|
2019-05-22 21:46:07 +05:30
|
|
|
webpackConfig.devtool = 'hidden-source-map';
|
2016-11-27 17:27:11 +05:30
|
|
|
|
2019-11-09 17:22:41 +05:30
|
|
|
webpackConfig.optimization = {
|
|
|
|
moduleIds: 'hashed',
|
|
|
|
runtimeChunk: 'single',
|
|
|
|
splitChunks: {
|
|
|
|
cacheGroups: {
|
|
|
|
vendor: {
|
|
|
|
test: (m) => String(m.context).includes('node_modules') && !String(m.context).includes('flag-icon-css'),
|
|
|
|
name: 'vendors',
|
|
|
|
chunks: 'all',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
2016-11-27 17:27:11 +05:30
|
|
|
} else {
|
|
|
|
webpackConfig.plugins.push(
|
|
|
|
new webpack.DllReferencePlugin({
|
|
|
|
context: __dirname,
|
|
|
|
manifest: require('./dll/vendor.json')
|
|
|
|
})
|
|
|
|
);
|
2016-01-03 01:54:07 +05:30
|
|
|
}
|
|
|
|
|
2016-01-08 18:44:11 +05:30
|
|
|
if (!isProduction && !isTest) {
|
2019-09-02 10:57:52 +05:30
|
|
|
// TODO: review HMR integration
|
2019-07-01 09:09:59 +05:30
|
|
|
webpackConfig.plugins.push(new webpack.HotModuleReplacementPlugin());
|
|
|
|
|
|
|
|
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
|
|
|
|
};
|
2016-10-04 11:07:46 +05:30
|
|
|
}
|
|
|
|
|
2017-12-31 17:58:31 +05:30
|
|
|
if (isCspEnabled) {
|
2019-07-01 09:09:59 +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'
|
|
|
|
})
|
|
|
|
);
|
2017-12-31 17:58:31 +05:30
|
|
|
}
|
|
|
|
|
2016-10-04 11:07:46 +05:30
|
|
|
if (isDockerized) {
|
|
|
|
webpackConfig.watchOptions = {
|
|
|
|
poll: 2000
|
|
|
|
};
|
|
|
|
webpackConfig.devServer.host = '0.0.0.0';
|
2016-01-08 18:44:11 +05:30
|
|
|
}
|
|
|
|
|
2016-12-04 18:42:36 +05:30
|
|
|
if (isSilent) {
|
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-01-03 01:54:07 +05:30
|
|
|
module.exports = webpackConfig;
|