UNPKG

5.1 kBJavaScriptView Raw
1const path = require('path')
2const webpack = require('webpack')
3const ExtractTextPlugin = require('extract-text-webpack-plugin');
4const HtmlWebpackPlugin = require('html-webpack-plugin');//html模板
5const autoprefixer = require('autoprefixer');
6const pxtorem = require('postcss-pxtorem');
7var souceMap="source-map";
8console.log("当前的环境变量:"+process.env.NODE_ENV)
9if(process.env.NODE_ENV === "prod"){
10 console.log("生产环境打包!")
11 souceMap=false;
12}else{
13 console.log("开发环境编译!")
14}
15
16/*const Visualizer = require('webpack-visualizer-plugin'); // remove it in production environment.
17const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; // remove it in production environment.
18const otherPlugins = process.argv[1].indexOf('webpack-dev-server') >= 0 ? [] : [
19 new Visualizer(), // remove it in production environment.
20 new BundleAnalyzerPlugin({
21 defaultSizes: 'parsed',
22 // generateStatsFile: true,
23 statsOptions: {source: false}
24 }), // remove it in production environment.
25];*/
26
27const postcssOpts = {
28 ident: 'postcss', // https://webpack.js.org/guides/migrating/#complex-options
29 plugins: () => [
30 autoprefixer({
31 browsers: ['last 2 versions', 'Firefox ESR', '> 1%', 'ie >= 8', 'iOS >= 8', 'Android >= 4'],
32 }),
33 // pxtorem({ rootValue: 100, propWhiteList: [] })
34 ],
35};
36
37module.exports = {
38 devtool: souceMap, // or 'inline-source-map'
39 devServer: {
40 disableHostCheck: true
41 },
42
43 entry: {
44 "index": path.resolve(__dirname, 'src/index'),
45 //添加要打包在vendors.js里面的库
46 vendors:['react','react-dom']
47 },
48
49 output: {
50 filename: '[name].[chunkhash:5].js',
51 chunkFilename: '[id].chunk.js',
52 path: path.join(__dirname, '/build'),
53 // publicPath: '/build/'
54 },
55
56 resolve: {
57 modules: [path.resolve(__dirname, 'node_modules'), path.join(__dirname, 'src')],
58 extensions: ['.web.js', '.jsx', '.js', '.json'],
59 //设置别名方便引用
60 alias: {
61 Comps: path.resolve(__dirname, 'src/components/'),//组件
62 Utils: path.resolve(__dirname, 'src/util/'),//工具包
63 RestUrl: path.resolve(__dirname, 'src/actions/RestUrl/'),//rest、http服务地址
64 Img: path.resolve(__dirname, 'src/assets/img/')//图片
65 }
66 },
67
68 module: {
69 rules: [
70 {
71 test: /\.jsx?$/, exclude: /node_modules/, loader: 'babel-loader',
72 options: {
73 plugins: [
74 'external-helpers', // why not work?
75 ["transform-runtime", {polyfill: false}],
76 ["import", [{"style": "css", "libraryName": "antd-mobile"}]]
77 ],
78 presets: ['es2015', 'stage-0', 'react']
79 // presets: [['es2015', { modules: false }], 'stage-0', 'react'] // tree-shaking
80 }
81 },
82 {test: /\.(jpg|png)$/, loader: "url-loader?limit=8192&name=img/[name]_[hash:5].[ext]"},
83 // 注意:如下不使用 ExtractTextPlugin 的写法,不能单独 build 出 css 文件
84 // { test: /\.less$/i, loaders: ['style-loader', 'css-loader', 'less-loader'] },
85 // { test: /\.css$/i, loaders: ['style-loader', 'css-loader'] },
86 {
87 test: /\.less$/i, use: ExtractTextPlugin.extract({
88 fallback: 'style-loader',
89 use: [
90 'css-loader', {loader: 'postcss-loader', options: postcssOpts}, 'less-loader'
91 ]
92 })
93 },
94 {
95 test: /\.css$/i, use: ExtractTextPlugin.extract({
96 fallback: 'style-loader',
97 use: [
98 'css-loader', {loader: 'postcss-loader', options: postcssOpts}
99 ]
100 })
101 },
102 {
103 test: /\.svg$/i,
104 use:'svg-sprite-loader',
105 include: [
106 require.resolve('antd-mobile').replace(/warn\.js$/, ''), // antd-mobile使用的svg目录
107 path.resolve(__dirname, './node_modules/yylib-antd-mobile/svg/'), // svg文件目录
108 path.resolve(__dirname, './src/'), // 个人的svg文件目录,如果自己有svg需要在这里配置
109 ]
110 },
111 ]
112 },
113 plugins: [
114 new webpack.optimize.ModuleConcatenationPlugin(),
115 // new webpack.optimize.CommonsChunkPlugin('shared.js'),
116 new webpack.optimize.CommonsChunkPlugin({
117 // minChunks: 2,
118 name: 'shared',
119 filename: 'shared.[chunkhash:5].js'
120 }),
121 new ExtractTextPlugin({filename: '[name].[contenthash:5].css', allChunks: true}),
122 new HtmlWebpackPlugin({template: './public/index.html'}),
123 //...otherPlugins
124 ]
125}