webpack.config.js
var path = require('path');
module.exports = {
entry: {
index:'./js/components/index'
},
externals: [{
'react-dom':'ReactDOM',
'react':'React'
}],
output: {
path:path.resolve(__dirname,'js/components'),
filename: 'bundle.js'
},
module: {
loaders:[
{
test: /\.js[x]?$/,
include: path.resolve(__dirname, 'js/components'),
loader: 'babel',
query: {
presets: ['es2015','react']
}
},
]
},
resolve: {
extensions: ['', '.js', '.jsx'],
alias:[
{'react-dom':'./libs/react-dom.js','react':'./libs/react.js'}
]
}
};
1.html
<!DOCTYPE html>
<html>
...
<body>
<script type="text/javascript" src='/libs/react.js'></script>
<script type="text/javascript" src='/libs/react-dom.js'></script>
<script type="text/javascript" src='/js/components/bundle.js'></script>
</body>
</html>
不修正配置文件(引荐)
Iframe mode
运转
webpack-dev-server
浏览器翻开
http://localhost:8080/webpack-dev-server/1.html
Inline mode
在1.html中增加
<script src="http://localhost:8080/webpack-dev-server.js"></script>
运转
webpack-dev-server
浏览器翻开
http://localhost:8080/1.html
修正配置文件
webpack.config.js
...
module.exports = {
entry: ['webpack/hot/dev-server','webpack-dev-server/client?http://localhost:8080','./js/components/index'],
...
plugins: [
new webpack.HotModuleReplacementPlugin()
]
};
运转
webpack-dev-server
,不必增加官网上说的--inline --hot
参数浏览器翻开
http://localhost:8080/1.html
这类体式格局的瑕玷:
会天生过剩的js,json文件,而且只能手动删除
配置文件中的
entry
只能为数组,不能用于多个进口的状况
...
entry: {
a: "./a",
b: "./b",
c: ["./c", "./d"]
},
output: {
path: path.join(__dirname, "dist"),
filename: "[name].entry.js"
}
...
上面一切体式格局都须要先运转webpack -w
监听文件变化
参考
官网文档