-
-
Save scq000/08f389addf415c3fc9b3cfe249b8fb22 to your computer and use it in GitHub Desktop.
Webpack 2 config file: Hot reloading, split css, generate HTML, split vendor files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const webpack = require('webpack'); | |
const path = require('path'); | |
const ExtractTextPlugin = require('extract-text-webpack-plugin'); | |
const BrowserSyncPlugin = require('browser-sync-webpack-plugin'); | |
const HtmlWebpackPlugin = require('html-webpack-plugin'); | |
module.exports = { | |
// Path to project entry points. | |
entry: { | |
// replace it with your project related path. | |
app: '[entry point]' | |
}, | |
// Path to output all files, currently it set up for /public. | |
output: { | |
path: path.resolve(__dirname, 'public'), | |
filename: 'js/[name].js', | |
publicPath: '/' | |
}, | |
// Source map support | |
devtool: 'inline-source-map', | |
// Webpack dev server configuration. | |
devServer: { | |
contentBase: path.resolve(__dirname, './public') | |
}, | |
module: { | |
rules: [ | |
// Babel loader compile es6/jsx to es5. | |
{ | |
test: /\.(js|jsx)$/, | |
// Include all js/jsx files from this dirictory. | |
include: __dirname + '/assets', | |
use: { | |
loader: 'babel-loader', | |
options: { | |
'presets': [ | |
'react', | |
'es2015', | |
'stage-0' | |
] | |
} | |
} | |
}, | |
// Less loader compile less to css. | |
{ | |
test: /\.less$/, | |
include: __dirname + '/assets', | |
use: ExtractTextPlugin.extract({ | |
fallback: 'style-loader', | |
use: [ | |
{ | |
loader: 'css-loader', | |
options: { | |
sourceMap: true, | |
importLoaders: 1 | |
} | |
}, | |
{ | |
loader: 'less-loader', | |
options: { | |
sourceMap: true | |
} | |
} | |
] | |
}) | |
}, | |
{ | |
test: /\.json$/, | |
include: __dirname + '/assets', | |
use: 'json-loader' | |
}, | |
{ | |
test: /\.(eot|svg|ttf|woff|woff2)$/, | |
use: 'file-loader?name=fonts/[name].[ext]' | |
} | |
] | |
}, | |
plugins: [ | |
// Separate css files from js. | |
new ExtractTextPlugin({ | |
filename: 'css/styles.css', | |
allChunks: true | |
}), | |
// Separate vendor files from custom files. | |
new webpack.optimize.CommonsChunkPlugin({ | |
names: ['vendor', 'manifest'], | |
// Select all dependencies that come from node_modules folder. | |
minChunks: function(module) { | |
return module.context && module.context.indexOf('node_modules') !== -1; | |
} | |
}), | |
// Generate index.html with all files included. | |
new HtmlWebpackPlugin({ | |
template: './assets/index.html', | |
hash: true, | |
title: '[TITLE]', | |
chunksSortMode: 'dependency' | |
}), | |
// Sync you code changes with browser. | |
new BrowserSyncPlugin({ | |
host: 'localhost', | |
port: 3000, | |
proxy: 'http://localhost:8080/' | |
}) | |
] | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment