webpack.config.js配置文件
var path = require('path');
var node_modules = path.resolve(__dirname, 'node_modules');
var pathToReact = path.resolve(node_modules, 'react/dist/react.min.js');
var oldconfig = {
entry: [
'webpack/hot/dev-server',
'webpack-dev-server/client?http://localhost:8080',
path.resolve(__dirname, 'app/main.js')
],
output: {
path: path.resolve(__dirname, 'build'),
filename: 'bundle.js',
},
};
var config = {
entry: ['webpack/hot/dev-server', path.resolve(__dirname, 'app/main.js')],
resolve: {
alias: {
'react': pathToReact
}
},
output: {
path: path.resolve(__dirname, 'build'),
filename: 'bundle.js'
},
module: {
loaders: [{
test: /\.jsx?$/,
loader: 'babel'
}, {
test: /\.css$/,
loader: 'style!css'
},
{
test: /\.less$/,
loader: 'style!css!less'
},
{
test: /\.scss$/,
loader: 'style!css!sass'
},
{
test: /\.(png|jpg)$/,
loader: 'url?limit=25000'
}],
noParse: [pathToReact]
}
};
var deps = [
'react/dist/react.min.js',
'react-router/dist/react-router.min.js',
'moment/min/moment.min.js',
'underscore/underscore-min.js',
];
deps.forEach(function (dep) {
var depPath = path.resolve(node_modules_dir, dep);
config.resolve.alias[dep.split(path.sep)[0]] = depPath;
config.module.noParse.push(depPath);
});
module.exports = config;
package.json
{
...
"scripts": {
"build": "webpack",
"dev": "webpack-dev-server --devtool eval --progress --colors --hot --content-base build",
},
...
"devDependencies": {
"babel-core": "^6.9.0",
"babel-loader": "^6.2.4",
"css-loader": "^0.23.1",
"file-loader": "^0.8.5",
"react-hot-loader": "^1.3.0",
"style-loader": "^0.13.1",
"url-loader": "^0.5.7",
"webpack": "^1.13.1"
},
"dependencies": {
"react": "^15.1.0",
"webpack-dev-server": "^1.14.1"
}
}
dev中命令的含义
- webpack-dev-server - 在 localhost:8080 建立一个 Web 服务器
- –devtool eval - 为你的代码创建源地址。当有任何报错的时候可以让你更加精确地定位到文件和行号
- –progress - 显示合并代码进度
- –colors - Yay,命令行中显示颜色!
- –hot
- –content-base build - 指向设置的输出目录
热加载配置
webpack.config.js
module.exports = {
entry: [
'webpack-dev-server/client?http://localhost:3000',
'webpack/hot/only-dev-server',
'./index'
],
...
plugins: process.env.NODE_ENV === 'production' ? [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin()
] : [
new webpack.HotModuleReplacementPlugin()
],
}
server.js 架设一个http服务器
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config = require('./webpack.config');
new WebpackDevServer(webpack(config), {
publicPath: config.output.publicPath,
hot: true,
historyApiFallback: true
}).listen(3000, 'localhost', function (err, result) {
if (err) {
return console.log(err);
}
console.log('Listening at localhost:3000');
});