Skip to content

Instantly share code, notes, and snippets.

@IpWebmaster
Created February 5, 2016 06:47
Show Gist options
  • Select an option

  • Save IpWebmaster/9cb325dbf03ef2511c0e to your computer and use it in GitHub Desktop.

Select an option

Save IpWebmaster/9cb325dbf03ef2511c0e to your computer and use it in GitHub Desktop.
'use strict';
const NODE_ENV = process.env.NODE_ENV || 'development',
webpack = require('webpack'),
ExtractTextPlugin = require('extract-text-webpack-plugin'),
AssetsPlugin = require('assets-webpack-plugin'),
CompressionPlugin = require("compression-webpack-plugin"),
rimraf = require('rimraf'),
autoprefixer = require('autoprefixer');
/**
* Добавление хеша при разных средах разработки
* @param template
* @param hash
* @returns {*}
*/
function addHash(template, hash) {
return NODE_ENV == 'production' ?
template.replace(/\.[^.]+$/, `.[${hash}]$&`) : template; // `${template}?hash=[${hash}]`
}
module.exports = {
context: __dirname + '/frontend/app',
// Точки входа
entry: {
app: './app.js'
},
watch: NODE_ENV == 'development',
// Сколько ждать при изменении файла чтоб watch отработал
watchOptions: {
aggregateTimeout: 100
},
devtool: NODE_ENV == 'development' ? 'inline-source-map' : null, // cheap-inline-module-source-map
// Куда нужно выкладывать
output: {
path: __dirname + '/frontend/web/build',
publicPath: '/build/',
filename: addHash('[name].js', 'chunkhash')
},
// Где икать обычные файлы (более быстрая сборка)
resolve: {
root: __dirname + '/frontend/vendor',
modulesDirectories: ['node_modules'],
extensions: ['', '.js', '.styl']
},
// Где искать лоадеры (более быстрая сборка)
resolveLoader: {
modulesDirectories: ['node_modules'],
moduleTemplates: ['*-loader', '*'],
extensions: ['', '.js']
},
// Модули
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel',
exclude: /\/node_modules\//,
query: {
presets: ['es2015'],
plugins: ['transform-runtime']
}
},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract('style', 'css?sourceMap!postcss')
},
{
// resolve css - все пути переписываются относительно корневого сайта
test: /\.styl$/,
loader: ExtractTextPlugin.extract('css?sourceMap!postcss!stylus')
},
{
// Работает так же как и file-loader
// Файлы меньше 10 кбайт будут в data url
test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot|ico)$/,
loader: addHash('url?name=[path][name].[ext]&limit=10000', 'hash:6')
}
],
noParse: /jquery\/dist\/jquery.js/
},
postcss: function () {
return [
autoprefixer({
browsers: ['last 2 versions', 'ie > 7', '> 1%']
})
];
},
plugins: [
{
apply: (compiler) => {
// Удаляет старые файлы сборки
rimraf.sync(compiler.options.output.path);
}
},
new ExtractTextPlugin(addHash('[name].css', 'contenthash'), { allChunks: true }),
new webpack.NoErrorsPlugin(),
new webpack.DefinePlugin({
NODE_ENV: JSON.stringify(NODE_ENV)
}),
// Регистрация глобально доступных библиотек
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
_: 'lodash'
}),
new AssetsPlugin({
filename: 'assets.json',
path: __dirname + '/frontend/web/build'
})
]
};
// Минификация
if (NODE_ENV == 'production') {
module.exports.plugins.push(
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false,
drop_console: true,
unsafe: true
}
})
);
module.exports.plugins.push(
new CompressionPlugin({
asset: "{file}.gz",
algorithm: "gzip",
regExp: /\.js$|\.css$/,
threshold: 10240,
minRatio: 0.8
})
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment