2016-01-03 01:54:07 +05:30
|
|
|
/* eslint-env node */
|
|
|
|
|
|
|
|
var path = require('path');
|
2016-01-08 15:12:25 +05:30
|
|
|
|
2016-01-03 01:54:07 +05:30
|
|
|
var webpack = require('webpack');
|
|
|
|
var ExtractTextPlugin = require('extract-text-webpack-plugin');
|
|
|
|
var HtmlWebpackPlugin = require('html-webpack-plugin');
|
2016-01-03 14:41:08 +05:30
|
|
|
var cssnano = require('cssnano');
|
2016-04-02 16:27:45 +05:30
|
|
|
var cssUrl = require("postcss-url");
|
2016-01-08 15:12:25 +05:30
|
|
|
var iconfontImporter = require('./webpack/node-sass-iconfont-importer');
|
2016-01-03 01:54:07 +05:30
|
|
|
|
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
|
|
|
|
|
|
|
var isProduction = process.argv.some(function(arg) {
|
|
|
|
return arg === '-p';
|
|
|
|
});
|
|
|
|
|
|
|
|
var isTest = process.argv.some(function(arg) {
|
|
|
|
return arg.indexOf('karma') !== -1;
|
|
|
|
});
|
|
|
|
|
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
|
|
|
var config;
|
|
|
|
|
|
|
|
try {
|
|
|
|
config = require('./config/dev.json');
|
|
|
|
} catch (err) {
|
|
|
|
console.error('\n\n===\nPlease create dev.json config under ./config based on template.dev.json\n===\n\n');
|
|
|
|
throw err;
|
|
|
|
}
|
2016-01-09 17:45:31 +05:30
|
|
|
|
2016-04-02 16:27:45 +05:30
|
|
|
var rootPath = path.resolve('./src');
|
|
|
|
|
2016-01-03 01:54:07 +05:30
|
|
|
var webpackConfig = {
|
|
|
|
entry: {
|
|
|
|
app: path.join(__dirname, 'src'),
|
|
|
|
vendor: [
|
2016-01-31 22:11:49 +05:30
|
|
|
'babel-polyfill',
|
2016-02-13 14:36:17 +05:30
|
|
|
'whatwg-fetch',
|
2016-01-31 22:11:49 +05:30
|
|
|
'classnames',
|
2016-01-03 01:54:07 +05:30
|
|
|
'history',
|
2016-01-31 22:11:49 +05:30
|
|
|
'intl-format-cache',
|
|
|
|
'intl-messageformat',
|
2016-01-03 01:54:07 +05:30
|
|
|
'react',
|
2016-01-31 22:11:49 +05:30
|
|
|
'react-dom',
|
|
|
|
'react-helmet',
|
|
|
|
'react-intl',
|
|
|
|
'react-motion',
|
2016-01-03 01:54:07 +05:30
|
|
|
'react-redux',
|
|
|
|
'react-router',
|
2016-01-31 22:11:49 +05:30
|
|
|
'react-router-redux',
|
2016-01-03 01:54:07 +05:30
|
|
|
'redux',
|
2016-01-31 22:11:49 +05:30
|
|
|
'redux-thunk'
|
2016-01-03 01:54:07 +05:30
|
|
|
]
|
|
|
|
},
|
|
|
|
|
|
|
|
output: {
|
|
|
|
path: path.join(__dirname, 'dist'),
|
2016-01-16 17:36:22 +05:30
|
|
|
publicPath: '/',
|
2016-01-03 01:54:07 +05:30
|
|
|
filename: '[name].js'
|
|
|
|
},
|
|
|
|
|
|
|
|
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 ? {
|
|
|
|
// http://airbnb.io/enzyme/docs/guides/webpack.html
|
|
|
|
'cheerio': 'window',
|
|
|
|
'react/lib/ExecutionEnvironment': true,
|
|
|
|
'react/lib/ReactContext': true,
|
|
|
|
'react/addons': true
|
|
|
|
} : {},
|
|
|
|
|
2016-01-03 01:54:07 +05:30
|
|
|
devServer: {
|
|
|
|
host: 'localhost',
|
|
|
|
port: 8080,
|
|
|
|
proxy: {
|
|
|
|
'/api*': {
|
2016-02-06 18:13:57 +05:30
|
|
|
headers: {
|
|
|
|
host: config.apiHost.replace(/https?:|\//g, '')
|
|
|
|
},
|
|
|
|
target: config.apiHost
|
2016-01-03 01:54:07 +05:30
|
|
|
}
|
|
|
|
},
|
|
|
|
hot: true,
|
|
|
|
inline: true,
|
|
|
|
historyApiFallback: true
|
|
|
|
},
|
|
|
|
|
|
|
|
devtool: isTest ? 'inline-source-map' : 'eval',
|
|
|
|
|
|
|
|
plugins: [
|
2016-01-08 15:12:25 +05:30
|
|
|
new iconfontImporter.Plugin(),
|
2016-01-03 01:54:07 +05:30
|
|
|
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({
|
|
|
|
template: 'src/index.html',
|
2016-01-04 11:32:13 +05:30
|
|
|
favicon: 'src/favicon.ico',
|
2016-01-03 01:54:07 +05:30
|
|
|
hash: isProduction,
|
|
|
|
filename: 'index.html',
|
|
|
|
inject: 'body',
|
|
|
|
minify: {
|
|
|
|
collapseWhitespace: isProduction
|
|
|
|
}
|
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 ? [] : [
|
|
|
|
new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.js')
|
|
|
|
]).concat(isProduction ? [
|
|
|
|
new webpack.optimize.DedupePlugin(),
|
|
|
|
new webpack.optimize.UglifyJsPlugin()
|
|
|
|
] : []),
|
|
|
|
|
|
|
|
module: {
|
|
|
|
loaders: [
|
|
|
|
{
|
|
|
|
test: /\.scss$/,
|
|
|
|
extractInProduction: true,
|
2016-01-09 17:45:31 +05:30
|
|
|
loader: 'style!css?modules&importLoaders=2&localIdentName=' + CSS_CLASS_TEMPLATE + '!postcss!sass'
|
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
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.(png|gif|jpg)$/,
|
|
|
|
loader: 'url?limit=1000'
|
2016-05-01 23:20:55 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.json$/,
|
|
|
|
loader: 'json'
|
2016-01-03 01:54:07 +05:30
|
|
|
}
|
|
|
|
]
|
2016-01-03 14:41:08 +05:30
|
|
|
},
|
|
|
|
|
2016-01-08 15:12:25 +05:30
|
|
|
sassLoader: {
|
|
|
|
importer: iconfontImporter({
|
|
|
|
test: /\.font.(js|json)$/,
|
|
|
|
types: ['woff', 'eot', 'ttf']
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2016-01-03 14:41:08 +05:30
|
|
|
postcss: [
|
2016-04-02 16:27:45 +05:30
|
|
|
cssUrl({
|
|
|
|
url: function(url, decl, from, dirname, to, options, result) {
|
|
|
|
// scss не правильно резолвит относительные урлы.
|
|
|
|
// добавляем к урлам остаток пути, что бы они были относительно root
|
|
|
|
//
|
|
|
|
// Например:
|
|
|
|
// file: components/ui/foo.scss
|
|
|
|
// images/foo.png -> components/ui/images/foo.png
|
|
|
|
|
|
|
|
if (url[0] !== '/') {
|
|
|
|
var relativeToRoot = dirname.split(rootPath + '/')[1];
|
|
|
|
|
|
|
|
return path.join(relativeToRoot, url);
|
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
}),
|
2016-01-03 14:41:08 +05:30
|
|
|
cssnano({
|
2016-01-09 19:21:55 +05:30
|
|
|
// sourcemap: !isProduction,
|
2016-01-03 14:41:08 +05:30
|
|
|
autoprefixer: {
|
|
|
|
add: true,
|
|
|
|
remove: true,
|
|
|
|
browsers: ['last 2 versions']
|
|
|
|
},
|
|
|
|
safe: true,
|
2016-01-09 17:25:09 +05:30
|
|
|
// отключаем минификацию цветов, что бы она не ломала такие выражения:
|
|
|
|
// composes: black from './buttons.scss';
|
|
|
|
colormin: false,
|
2016-01-03 14:41:08 +05:30
|
|
|
discardComments: {
|
|
|
|
removeAll: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
]
|
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) {
|
|
|
|
var parts = loader.loader.split('!');
|
2016-01-09 19:21:55 +05:30
|
|
|
loader.loader = ExtractTextPlugin.extract(
|
|
|
|
parts[0],
|
|
|
|
parts.slice(1)
|
|
|
|
.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-01-03 01:54:07 +05:30
|
|
|
module.exports = webpackConfig;
|