2016-01-03 01:54:07 +05:30
|
|
|
/* eslint-env node */
|
|
|
|
|
2017-12-31 01:14:32 +05:30
|
|
|
require('babel-register');
|
|
|
|
|
2016-10-04 11:07:46 +05:30
|
|
|
const path = require('path');
|
2016-01-08 15:12:25 +05:30
|
|
|
|
2016-10-04 11:07:46 +05:30
|
|
|
const webpack = require('webpack');
|
|
|
|
const loaderUtils = require('loader-utils');
|
|
|
|
const ExtractTextPlugin = require('extract-text-webpack-plugin');
|
|
|
|
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
|
|
|
const cssUrl = require('webpack-utils/cssUrl');
|
|
|
|
const cssImport = require('postcss-import');
|
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');
|
2017-12-31 01:14:32 +05:30
|
|
|
const localeFlags = require('./src/components/i18n/localeFlags').default;
|
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'));
|
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-07-23 02:28:31 +05:30
|
|
|
|
2016-12-07 02:36:45 +05:30
|
|
|
const packageJson = require('./package.json');
|
|
|
|
|
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) {
|
|
|
|
console.error('\n\n===\nCan not find config/env.js. Running with defaults\n===\n\n', err);
|
|
|
|
}
|
|
|
|
|
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';
|
2016-05-01 23:20:55 +05:30
|
|
|
if (isTest) {
|
|
|
|
process.env.NODE_ENV = 'test';
|
|
|
|
}
|
2016-01-31 21:33:04 +05:30
|
|
|
|
2016-01-09 19:21:55 +05:30
|
|
|
const CSS_CLASS_TEMPLATE = isProduction ? '[hash:base64:5]' : '[path][name]-[local]';
|
2016-02-06 18:13:57 +05:30
|
|
|
|
2016-07-23 18:02:28 +05:30
|
|
|
const fileCache = {};
|
|
|
|
|
2016-07-23 02:28:31 +05:30
|
|
|
const cssLoaderQuery = {
|
|
|
|
modules: true,
|
|
|
|
importLoaders: 2,
|
|
|
|
url: false,
|
|
|
|
localIdentName: CSS_CLASS_TEMPLATE,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* cssnano options
|
|
|
|
*/
|
|
|
|
sourcemap: !isProduction,
|
|
|
|
autoprefixer: {
|
|
|
|
add: true,
|
|
|
|
remove: true,
|
|
|
|
browsers: ['last 2 versions']
|
|
|
|
},
|
|
|
|
safe: true,
|
|
|
|
// отключаем минификацию цветов, что бы она не ломала такие выражения:
|
|
|
|
// composes: black from './buttons.scss';
|
|
|
|
colormin: false,
|
|
|
|
discardComments: {
|
|
|
|
removeAll: true
|
|
|
|
}
|
|
|
|
};
|
2016-04-02 16:27:45 +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: {
|
2016-04-02 16:27:45 +05:30
|
|
|
root: rootPath,
|
2016-01-03 01:54:07 +05:30
|
|
|
extensions: ['', '.js', '.jsx']
|
|
|
|
},
|
|
|
|
|
2016-05-01 23:20:55 +05:30
|
|
|
externals: isTest ? {
|
2016-07-30 16:14:43 +05:30
|
|
|
sinon: 'sinon',
|
2016-05-01 23:20:55 +05:30
|
|
|
// http://airbnb.io/enzyme/docs/guides/webpack.html
|
2016-07-23 02:28:31 +05:30
|
|
|
cheerio: 'window',
|
2016-05-01 23:20:55 +05:30
|
|
|
'react/lib/ExecutionEnvironment': true,
|
|
|
|
'react/lib/ReactContext': true,
|
|
|
|
'react/addons': true
|
|
|
|
} : {},
|
|
|
|
|
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({
|
2017-01-03 11:26:38 +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) : undefined,
|
2016-02-13 14:35:21 +05:30
|
|
|
'process.env': {
|
2016-12-07 02:36:45 +05:30
|
|
|
NODE_ENV: JSON.stringify(process.env.NODE_ENV),
|
2016-12-07 02:46:34 +05:30
|
|
|
APP_ENV: JSON.stringify(config.environment || process.env.NODE_ENV),
|
2018-03-17 21:43:12 +05:30
|
|
|
__VERSION__: JSON.stringify(config.version || ''),
|
2016-12-07 02:36:45 +05:30
|
|
|
__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
|
|
|
},
|
|
|
|
isCspEnabled,
|
2016-11-27 17:27:11 +05:30
|
|
|
}),
|
2017-12-23 04:30:52 +05:30
|
|
|
new SitemapPlugin('https://account.ely.by', [
|
|
|
|
'/',
|
|
|
|
'/register',
|
|
|
|
'/resend-activation',
|
|
|
|
'/activation',
|
|
|
|
'/forgot-password',
|
|
|
|
'/rules',
|
|
|
|
], {
|
|
|
|
lastMod: true,
|
|
|
|
changeFreq: 'weekly',
|
|
|
|
}),
|
2016-11-27 17:27:11 +05:30
|
|
|
new webpack.ProvidePlugin({
|
|
|
|
React: 'react'
|
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(
|
2017-12-17 19:41:29 +05:30
|
|
|
/locale-data/, new RegExp(`/(${SUPPORTED_LANGUAGES.join('|')})\\.js`)
|
|
|
|
),
|
2017-12-31 01:14:32 +05:30
|
|
|
// @see components/i18n/localeFlags.js
|
2017-12-17 19:41:29 +05:30
|
|
|
new webpack.ContextReplacementPlugin(
|
2017-12-31 01:14:32 +05:30
|
|
|
/flag-icon-css\/flags\/4x3/, new RegExp(`/(${localeFlags.getCountryList().join('|')})\\.svg`)
|
2017-12-17 19:41:29 +05:30
|
|
|
),
|
2016-11-27 17:27:11 +05:30
|
|
|
],
|
2016-01-03 01:54:07 +05:30
|
|
|
|
|
|
|
module: {
|
|
|
|
loaders: [
|
|
|
|
{
|
|
|
|
test: /\.scss$/,
|
|
|
|
extractInProduction: true,
|
2017-12-31 01:14:32 +05:30
|
|
|
loader: `style!css?${ JSON.stringify(cssLoaderQuery) }!sass!postcss?syntax=postcss-scss`
|
2016-01-03 01:54:07 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.jsx?$/,
|
|
|
|
exclude: /node_modules/,
|
2016-11-27 16:19:48 +05:30
|
|
|
loader: 'babel?cacheDirectory=true'
|
2016-04-02 16:27:45 +05:30
|
|
|
},
|
|
|
|
{
|
2016-05-20 01:11:43 +05:30
|
|
|
test: /\.(png|gif|jpg|svg)$/,
|
2016-07-28 10:02:36 +05:30
|
|
|
loader: 'file',
|
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)$/,
|
2016-07-23 02:28:31 +05:30
|
|
|
loader: 'file',
|
|
|
|
query: {
|
|
|
|
name: 'assets/fonts/[name].[ext]?[hash]'
|
|
|
|
}
|
|
|
|
|
2016-05-12 10:20:30 +05:30
|
|
|
},
|
2016-05-01 23:20:55 +05:30
|
|
|
{
|
|
|
|
test: /\.json$/,
|
2016-05-22 17:36:57 +05:30
|
|
|
exclude: /(intl|font)\.json/,
|
2016-05-01 23:20:55 +05:30
|
|
|
loader: 'json'
|
2016-05-08 16:56:25 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.html$/,
|
|
|
|
loader: 'html'
|
2016-05-09 00:58:51 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.intl\.json$/,
|
2017-08-01 23:01:47 +05:30
|
|
|
loader: 'babel!intl'
|
2016-05-22 17:36:57 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.font\.(js|json)$/,
|
|
|
|
loader: 'raw!fontgen'
|
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: {
|
2016-07-23 02:51:04 +05:30
|
|
|
intl: path.resolve('webpack-utils/intl-loader')
|
2016-05-09 00:58:51 +05:30
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-07-23 02:28:31 +05:30
|
|
|
postcss() {
|
|
|
|
return [
|
|
|
|
cssImport({
|
|
|
|
path: rootPath,
|
2016-04-02 16:27:45 +05:30
|
|
|
|
2016-07-23 02:28:31 +05:30
|
|
|
resolve: ((defaultResolve) =>
|
|
|
|
(url, basedir, importOptions) =>
|
|
|
|
defaultResolve(loaderUtils.urlToRequest(url), basedir, importOptions)
|
|
|
|
)(require('postcss-import/lib/resolve-id')),
|
2016-04-02 16:27:45 +05:30
|
|
|
|
2016-07-23 02:28:31 +05:30
|
|
|
load: ((defaultLoad) =>
|
|
|
|
(filename, importOptions) => {
|
|
|
|
if (/\.font.(js|json)$/.test(filename)) {
|
2016-07-23 18:02:28 +05:30
|
|
|
if (!fileCache[filename] || !isProduction) {
|
|
|
|
// do not execute loader on the same file twice
|
|
|
|
// this is an overcome for a bug with ExtractTextPlugin, for isProduction === true
|
|
|
|
// when @imported files may be processed mutiple times
|
|
|
|
fileCache[filename] = new Promise((resolve, reject) =>
|
|
|
|
this.loadModule(filename, (err, source) =>
|
2016-11-12 14:33:44 +05:30
|
|
|
err ? reject(err) : resolve(this.exec(source, rootPath))
|
2016-07-23 18:02:28 +05:30
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileCache[filename];
|
2016-07-23 02:28:31 +05:30
|
|
|
}
|
2016-04-02 16:27:45 +05:30
|
|
|
|
2016-07-23 02:28:31 +05:30
|
|
|
return defaultLoad(filename, importOptions);
|
|
|
|
}
|
|
|
|
)(require('postcss-import/lib/load-content'))
|
|
|
|
}),
|
|
|
|
|
|
|
|
cssUrl(this)
|
|
|
|
];
|
|
|
|
}
|
2016-01-03 01:54:07 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
if (isProduction) {
|
2016-01-08 18:44:11 +05:30
|
|
|
webpackConfig.module.loaders.forEach((loader) => {
|
2016-01-03 01:54:07 +05:30
|
|
|
if (loader.extractInProduction) {
|
2016-07-23 18:02:28 +05:30
|
|
|
// remove style-loader from chain and pass through ExtractTextPlugin
|
2016-07-23 02:28:31 +05:30
|
|
|
const parts = loader.loader.split('!');
|
2016-07-23 18:02:28 +05:30
|
|
|
|
2016-01-09 19:21:55 +05:30
|
|
|
loader.loader = ExtractTextPlugin.extract(
|
2016-07-23 18:02:28 +05:30
|
|
|
parts[0], // style-loader
|
|
|
|
parts.slice(1) // css-loader and rest
|
2016-01-09 19:21:55 +05:30
|
|
|
.join('!')
|
|
|
|
.replace(/[&?]sourcemap/, '')
|
|
|
|
);
|
2016-01-03 01:54:07 +05:30
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-11-27 17:27:11 +05:30
|
|
|
webpackConfig.plugins.push(
|
|
|
|
new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.js?[hash]'),
|
|
|
|
new webpack.optimize.OccurenceOrderPlugin(true),
|
|
|
|
new webpack.optimize.DedupePlugin(),
|
|
|
|
new webpack.optimize.UglifyJsPlugin(),
|
2017-01-02 18:33:38 +05:30
|
|
|
new ExtractTextPlugin('styles.css?[hash]', {
|
2016-11-27 17:27:11 +05:30
|
|
|
allChunks: true
|
|
|
|
})
|
|
|
|
);
|
2016-01-03 01:54:07 +05:30
|
|
|
|
|
|
|
webpackConfig.devtool = false;
|
2016-11-27 17:27:11 +05:30
|
|
|
|
2017-12-17 19:41:29 +05:30
|
|
|
const ignoredPlugins = [
|
|
|
|
'flag-icon-css',
|
|
|
|
];
|
|
|
|
|
|
|
|
webpackConfig.entry.vendor = Object.keys(packageJson.dependencies)
|
2017-12-31 03:08:54 +05:30
|
|
|
.filter((module) => !ignoredPlugins.includes(module));
|
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) {
|
|
|
|
webpackConfig.plugins.push(
|
|
|
|
new webpack.HotModuleReplacementPlugin(),
|
2017-08-23 01:04:43 +05:30
|
|
|
new webpack.NoErrorsPlugin()
|
2016-01-08 18:44:11 +05:30
|
|
|
);
|
2016-10-04 11:07:46 +05:30
|
|
|
|
2016-10-09 23:53:00 +05:30
|
|
|
if (config.apiHost) {
|
|
|
|
webpackConfig.devServer = {
|
|
|
|
host: 'localhost',
|
|
|
|
port: 8080,
|
|
|
|
proxy: {
|
|
|
|
'/api': {
|
|
|
|
target: config.apiHost,
|
2016-12-11 19:47:11 +05:30
|
|
|
changeOrigin: true, // add host http-header, based on target
|
|
|
|
secure: false // allow self-signed certs
|
2016-10-09 23:53:00 +05:30
|
|
|
}
|
|
|
|
},
|
|
|
|
hot: true,
|
|
|
|
inline: true,
|
|
|
|
historyApiFallback: true
|
|
|
|
};
|
|
|
|
}
|
2016-10-04 11:07:46 +05:30
|
|
|
}
|
|
|
|
|
2017-12-31 17:58:31 +05:30
|
|
|
if (isCspEnabled) {
|
|
|
|
webpackConfig.plugins.push(new CSPPlugin({
|
|
|
|
'default-src': '\'none\'',
|
|
|
|
'style-src': ['\'self\'', '\'unsafe-inline\''],
|
|
|
|
'script-src': [
|
|
|
|
'\'self\'',
|
2018-01-28 16:11:38 +05:30
|
|
|
'\'nonce-edge-must-die\'',
|
2017-12-31 17:58:31 +05:30
|
|
|
'\'unsafe-inline\'',
|
|
|
|
'https://www.google-analytics.com',
|
2018-03-17 20:51:53 +05:30
|
|
|
'https://recaptcha.net/recaptcha/',
|
2017-12-31 17:58:31 +05:30
|
|
|
'https://www.gstatic.com/recaptcha/',
|
2018-03-17 20:51:53 +05:30
|
|
|
'https://www.gstatic.cn/recaptcha/',
|
2017-12-31 17:58:31 +05:30
|
|
|
],
|
|
|
|
'img-src': ['\'self\'', 'data:', 'www.google-analytics.com'],
|
|
|
|
'font-src': ['\'self\'', 'data:'],
|
2018-01-04 04:33:35 +05:30
|
|
|
'connect-src': ['\'self\'', 'https://sentry.ely.by'].concat(isProduction ? [] : ['ws://localhost:8080']),
|
2018-03-17 20:51:53 +05:30
|
|
|
'frame-src': [
|
|
|
|
'https://www.google.com/recaptcha/',
|
|
|
|
'https://recaptcha.net/recaptcha/',
|
|
|
|
],
|
2017-12-31 17:58:31 +05:30
|
|
|
'report-uri': 'https://sentry.ely.by/api/2/csp-report/?sentry_key=088e7718236a4f91937a81fb319a93f6',
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
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;
|