1.目录结构
2.webpack.config.js
说明:
1.涉及到的插件需要npm install安装; 2.html-webpack-plugin创建服务于 webpack bundle 的 HTML 文件; 3.clean-webpack-plugin清除dist目录重复的文件; 4.extract-text-webpack-plugin分离css文件。
var config = {
context: path.resolve(dirname,'./src'),entry: {
app: './main.js'
},output: {
path: path.resolve(dirname,'./dist'),filename: '[name].bundle.js'
},devtool: 'cheap-module-eval-source-map',module: {
rules: [
{
test: /.jsx?$/,exclude: /node_modules/,loader: 'babel-loader'
},{
test: /.css$/,use: ExtractTextPlugin.extract({
fallback: "style-loader",use: ["css-loader","postcss-loader"]
})
},{
test: /.less$/,use: ["style-loader","css-loader","less-loader"]
},{
test: /.(png|jpg)$/,loader: 'url-loader',options: {
limit: 8129
}
}
]
},devServer:{
historyApiFallback: true,host:'0.0.0.0',hot: true,//HMR模式
inline: true,//实时刷新
port: 8181 // 修改端口,一般默认是8080
},resolve: {
extensions: ['.js','.jsx','.css'],modules: [path.resolve(__dirname,'node_modules']
},plugins: [
new webpack.HotModuleReplacementPlugin(),new UglifyJsPlugin({
sourceMap: true
}),new webpack.LoaderOptionsPlugin({
minimize: true,debug: true
}),new HtmlWebpackPlugin({
template:'./templateIndex.html'
}),new ExtractTextPlugin({
filename: '[name].[hash].css',disable: false,allChunks: true,}),new CleanWebpackPlugin(['dist'])
],}
module.exports = config;
context: path.resolve(dirname,'./src'),entry: {
app: './main.js'
},output: {
path: path.resolve(dirname,'./dist'),filename: '[name].bundle.js'
},devtool: 'cheap-module-eval-source-map',module: {
rules: [
{
test: /.jsx?$/,exclude: /node_modules/,loader: 'babel-loader'
},{
test: /.css$/,use: ExtractTextPlugin.extract({
fallback: "style-loader",use: ["css-loader","postcss-loader"]
})
},{
test: /.less$/,use: ["style-loader","css-loader","less-loader"]
},{
test: /.(png|jpg)$/,loader: 'url-loader',options: {
limit: 8129
}
}
]
},devServer:{
historyApiFallback: true,host:'0.0.0.0',hot: true,//HMR模式
inline: true,//实时刷新
port: 8181 // 修改端口,一般默认是8080
},resolve: {
extensions: ['.js','.jsx','.css'],modules: [path.resolve(__dirname,'node_modules']
},plugins: [
new webpack.HotModuleReplacementPlugin(),new UglifyJsPlugin({
sourceMap: true
}),new webpack.LoaderOptionsPlugin({
minimize: true,debug: true
}),new HtmlWebpackPlugin({
template:'./templateIndex.html'
}),new ExtractTextPlugin({
filename: '[name].[hash].css',disable: false,allChunks: true,}),new CleanWebpackPlugin(['dist'])
],}
module.exports = config;
// webpack里面配置的bundle.js需要手动打包才会变化,目录可以由自己指定;
// webpack-dev-server自动检测变化自动打包的是开发环境下的bundle.js,打包路径由contentBase决定,两个文件是不一样的.
3.postcss.config.js(Autoprefixer)
// 兼容最新的5个浏览器版本