2016-01-03 01:54:07 +05:30
|
|
|
/* eslint-env node */
|
|
|
|
|
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');
|
2016-01-03 01:54:07 +05:30
|
|
|
|
2016-10-04 11:07:46 +05:30
|
|
|
const vendor = Object.keys(require('./package.json').dependencies);
|
2016-06-28 14:38:07 +05:30
|
|
|
|
2016-07-23 02:28:31 +05:30
|
|
|
const rootPath = path.resolve('./src');
|
|
|
|
|
2016-10-09 23:53:00 +05:30
|
|
|
var config = {};
|
|
|
|
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-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-01-03 01:54:07 +05:30
|
|
|
var webpackConfig = {
|
|
|
|
entry: {
|
|
|
|
app: path.join(__dirname, 'src'),
|
2016-06-28 14:38:07 +05:30
|
|
|
vendor: vendor
|
2016-01-03 01:54:07 +05:30
|
|
|
},
|
|
|
|
|
|
|
|
output: {
|
|
|
|
path: path.join(__dirname, 'dist'),
|
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
|
|
|
|
} : {},
|
|
|
|
|
2016-01-03 01:54:07 +05:30
|
|
|
devtool: isTest ? 'inline-source-map' : 'eval',
|
|
|
|
|
|
|
|
plugins: [
|
|
|
|
new webpack.DefinePlugin({
|
2016-02-13 14:35:21 +05:30
|
|
|
'process.env': {
|
|
|
|
NODE_ENV: JSON.stringify(process.env.NODE_ENV)
|
|
|
|
},
|
2016-01-03 01:54:07 +05:30
|
|
|
__DEV__: !isProduction,
|
|
|
|
__TEST__: isTest,
|
|
|
|
__PROD__: isProduction
|
|
|
|
}),
|
|
|
|
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',
|
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
|
2016-10-09 23:53:00 +05:30
|
|
|
},
|
|
|
|
ga: config.ga
|
2016-02-13 14:36:17 +05:30
|
|
|
}),
|
|
|
|
new webpack.ProvidePlugin({
|
|
|
|
// window.fetch polyfill
|
|
|
|
fetch: 'imports?this=>self!exports?self.fetch!whatwg-fetch'
|
2016-01-03 01:54:07 +05:30
|
|
|
})
|
|
|
|
].concat(isTest ? [] : [
|
2016-07-30 22:10:07 +05:30
|
|
|
new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.js?[hash]')
|
2016-01-03 01:54:07 +05:30
|
|
|
]).concat(isProduction ? [
|
|
|
|
new webpack.optimize.DedupePlugin(),
|
|
|
|
new webpack.optimize.UglifyJsPlugin()
|
|
|
|
] : []),
|
|
|
|
|
|
|
|
module: {
|
|
|
|
loaders: [
|
|
|
|
{
|
|
|
|
test: /\.scss$/,
|
|
|
|
extractInProduction: true,
|
2016-07-23 02:28:31 +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-01-31 21:33:04 +05:30
|
|
|
loader: 'babel'
|
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$/,
|
2016-05-22 17:36:57 +05:30
|
|
|
loader: 'babel!intl!json'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
addDependencyTo: webpack,
|
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) =>
|
|
|
|
err ? reject(err) : resolve(this.exec(source))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-03 14:41:08 +05:30
|
|
|
webpackConfig.plugins.push(new ExtractTextPlugin('styles.css', {
|
2016-01-03 01:54:07 +05:30
|
|
|
allChunks: true
|
|
|
|
}));
|
|
|
|
|
|
|
|
webpackConfig.devtool = false;
|
|
|
|
}
|
|
|
|
|
2016-01-08 18:44:11 +05:30
|
|
|
if (!isProduction && !isTest) {
|
|
|
|
webpackConfig.plugins.push(
|
|
|
|
new webpack.HotModuleReplacementPlugin(),
|
|
|
|
new webpack.NoErrorsPlugin()
|
|
|
|
);
|
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,
|
|
|
|
changeOrigin: true // add host http-header, based on target
|
|
|
|
}
|
|
|
|
},
|
|
|
|
hot: true,
|
|
|
|
inline: true,
|
|
|
|
historyApiFallback: true
|
|
|
|
};
|
|
|
|
}
|
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-01-03 01:54:07 +05:30
|
|
|
module.exports = webpackConfig;
|