webpack.config.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. var webpack = require('webpack');
  2. var path = require('path');
  3. var config = require('./package.json');
  4. var HtmlWebpackPlugin = require('html-webpack-plugin');
  5. var ExtractTextPlugin = require('extract-text-webpack-plugin');
  6. var CopyWebpackPlugin = require('copy-webpack-plugin');
  7. var autoprefixer = require('autoprefixer');
  8. var webpackConfig = module.exports = {};
  9. var isProduction = process.env.NODE_ENV === 'production';
  10. webpackConfig.entry = {
  11. app: './src/app.js',
  12. demo: './src/demo.js',
  13. nutui:'./src/nutui.js'
  14. };
  15. webpackConfig.output = {
  16. path: path.resolve(__dirname, 'dist'),
  17. publicPath:"/",
  18. filename: '[name].js',
  19. library: 'nutui',
  20. libraryTarget: 'umd',
  21. umdNamedDefine: true
  22. };
  23. webpackConfig.module = {
  24. rules: [{
  25. test: /\.css$/,
  26. use: [ 'style-loader', 'css-loader', 'postcss-loader' ]
  27. }, {
  28. test: /\.scss$/,
  29. use: [ 'style-loader', 'css-loader', 'sass-loader', 'postcss-loader' ]
  30. }, {
  31. test: /\.vue$/,
  32. loader: 'vue-loader',
  33. options: {
  34. postcss: [autoprefixer()]
  35. }
  36. }, {
  37. test: /\.js$/,
  38. loader: 'babel-loader',
  39. exclude: /node_modules/,
  40. }, {
  41. test: /\.(png|jpg|gif|webp)$/,
  42. loader: 'url-loader',
  43. options: {
  44. limit: 3000,
  45. name: 'img/[name].[ext]',
  46. }
  47. }, ]
  48. };
  49. webpackConfig.plugins = [
  50. new HtmlWebpackPlugin({
  51. filename: path.resolve(__dirname, './dist/index.html'),
  52. template: './src/index.html',
  53. chunks:['app']
  54. }),
  55. new HtmlWebpackPlugin({
  56. filename: path.resolve(__dirname, './dist/demo.html'),
  57. template: './src/demo.html',
  58. chunks:['demo']
  59. }),
  60. new webpack.optimize.UglifyJsPlugin({
  61. compress: false
  62. }),
  63. new webpack.BannerPlugin('NutUI v' + config.version + ' ' + new Date().toString()),
  64. new CopyWebpackPlugin([
  65. { from: path.join(__dirname, "./src/asset/"), to: path.join(__dirname, "./dist/asset") }
  66. ])
  67. ];
  68. if (isProduction) {
  69. webpackConfig.devtool = '#cheap-module-source-map';
  70. webpackConfig.plugins = (webpackConfig.plugins || []).concat([
  71. new webpack.DefinePlugin({
  72. 'process.env': {
  73. NODE_ENV: '"production"'
  74. },
  75. 'CONFIG':config
  76. }),
  77. new webpack.LoaderOptionsPlugin({
  78. minimize: false
  79. })
  80. ])
  81. } else {
  82. webpackConfig.devtool = '#cheap-module-eval-source-map';
  83. //webpackConfig.output.publicPath = '/';
  84. webpackConfig.plugins = (webpackConfig.plugins || []).concat([
  85. new webpack.DefinePlugin({
  86. 'CONFIG':config
  87. })
  88. ]);
  89. webpackConfig.devServer = {
  90. contentBase: path.resolve(__dirname, 'dist'),
  91. compress: true, //gzip压缩
  92. //host:'192.168.191.1',
  93. historyApiFallback: true
  94. };
  95. }