1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| const path = require('path'); const uglify = require('uglifyjs-webpack-plugin'); const htmlWebpackPlugin = require('html-webpack-plugin'); const extractTextWebpackPlugin = require('extract-text-webpack-plugin'); const webpack = require('webpack');
module.exports = { context: path.join(__dirname, 'src'), //设置根目录 entry:{ entry:'./entry.js' //入口文件 }, output:{ path: path.resolve(__dirname,'dist/js'), //打包输出文件 filename: '[name].js', //输出文件名 publicPath: '/' //html引用的虚拟路径 }, module:{ rules:[ { test:'/\.(js|jsx)$/', exclude: /node_modules/, // 忽略依赖 use:['babel-loader'] }, { test:/\.css$/, use:extractTextWebpackPlugin.extract({ //打包css文件以及分离 fallback:'style-loader', use:[ { loader:'css-loader' }, { loader:'postcss-loader', //加浏览器前缀 options:{ plugins: [ require('autoprefixer') //要设置下postcss里的autoprefixer插件 ] } } ] }) }, { test:/\.less$/, use:extractTextWebpackPlugin.extract({ //打包less文件成css以及分离 use: ['css-loader','less-loader'], // loader:['css-loader','less-loader'], // use:'css-loader!less-loader', fallback: "style-loader" }) }, { test:/\.(sass|scss)$/, use:extractTextWebpackPlugin.extract({ ////打包scss文件成css以及分离 use:['css-loader','sass-loader',{ loader:'postcss-loader', //加浏览器前缀 options: { plugins: (loader) => [ require('autoprefixer'), //要配置下postcss里的autoprefixer插件 ] } }] }) }, { test:/\.(jpg|png|gif|jpeg)$/, use:[ { loader:'url-loader', //图片压缩 options:{ limit:8192, name:'images/[name][hash:8].[ext]', //指定名字 } } ] } ] }, plugins: [ new webpack.optimize.UglifyJsPlugin(), //js压缩 new htmlWebpackPlugin({ minify:{ removeAttributeQuotes:true }, hash:true, template:'index.html' }), new extractTextWebpackPlugin("css/[name].css"), //配置分离css文件的路径名 new webpack.ProvidePlugin({ //配置jquery $: "jquery", //别名 jQuery: "jquery" }) ], node: { fs: 'empty' }, devServer:{ contentBase:path.resolve(__dirname,'dist'), //根目录路径 host:'localhost', inline: true, //实时刷新 hot:true, //热加载 compress:true, //压缩 port:7777 } };
|