webpack.config.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 UglifyJSPlugin = require('uglifyjs-webpack-plugin');
  8. var autoprefixer = require('autoprefixer');
  9. var webpackConfig = module.exports = {};
  10. var isProduction = process.env.NODE_ENV === 'production';
  11. webpackConfig.entry = {
  12. app: './src/app.js',
  13. demo: './src/demo.js',
  14. nutui: './src/nutui.js'
  15. };
  16. webpackConfig.output = {
  17. path: path.resolve(__dirname, 'dist'),
  18. publicPath: "/",
  19. filename: '[name].js',
  20. library: 'nutui',
  21. libraryTarget: 'umd',
  22. umdNamedDefine: true
  23. };
  24. webpackConfig.module = {
  25. rules: [{
  26. test: /\.css$/,
  27. use: ['style-loader', 'css-loader', 'postcss-loader']
  28. }, {
  29. test: /\.scss$/,
  30. use: ['style-loader', 'css-loader', 'sass-loader', 'postcss-loader']
  31. }, {
  32. test: /\.vue$/,
  33. loader: 'vue-loader',
  34. options: {
  35. postcss: [autoprefixer()]
  36. }
  37. }, {
  38. test: /\.js$/,
  39. loader: 'babel-loader',
  40. exclude: /node_modules/,
  41. }, {
  42. test: /\.svg$/,
  43. loader: 'svg-sprite-loader'
  44. }, {
  45. test: /\.(png|jpg|gif|webp)$/,
  46. loader: 'url-loader',
  47. options: {
  48. limit: 3000,
  49. name: 'img/[name].[ext]',
  50. }
  51. }, ]
  52. };
  53. webpackConfig.externals = {
  54. 'vue': {
  55. root: 'Vue',
  56. commonjs: 'vue',
  57. commonjs2: 'vue',
  58. amd: 'vue'
  59. }
  60. };
  61. webpackConfig.plugins = [
  62. new HtmlWebpackPlugin({
  63. filename: path.resolve(__dirname, './dist/index.html'),
  64. template: './src/index.html',
  65. chunks: ['app']
  66. }),
  67. new HtmlWebpackPlugin({
  68. filename: path.resolve(__dirname, './dist/demo.html'),
  69. template: './src/demo.html',
  70. chunks: ['demo']
  71. }),
  72. /*new ExtractTextPlugin({
  73. filename: 'nutui.css'
  74. }),*/
  75. /* new webpack.optimize.UglifyJsPlugin({
  76. mangle: {
  77. keep_fnames: true
  78. }
  79. }), */
  80. new webpack.BannerPlugin('NutUI v' + config.version + ' ' + new Date().toString()),
  81. new CopyWebpackPlugin([{
  82. from: path.join(__dirname, "./src/asset/"),
  83. to: path.join(__dirname, "./dist/asset")
  84. },
  85. {
  86. from: path.join(__dirname, "./src/default.html"),
  87. to: path.join(__dirname, "./dist/default.html")
  88. }
  89. ])
  90. ];
  91. if (isProduction) {
  92. webpackConfig.devtool = '#cheap-module-source-map';
  93. webpackConfig.plugins = (webpackConfig.plugins || []).concat([
  94. new webpack.DefinePlugin({
  95. 'process.env': {
  96. NODE_ENV: '"production"'
  97. },
  98. 'CONFIG': config
  99. }),
  100. new webpack.LoaderOptionsPlugin({
  101. minimize: false
  102. }),
  103. new webpack.optimize.UglifyJsPlugin({
  104. ecma: 6,
  105. compress: {
  106. warnings: false
  107. }
  108. })
  109. ])
  110. } else {
  111. webpackConfig.devtool = '#cheap-module-eval-source-map';
  112. //webpackConfig.output.publicPath = '/';
  113. webpackConfig.plugins = (webpackConfig.plugins || []).concat([
  114. new webpack.DefinePlugin({
  115. 'CONFIG': config
  116. })
  117. ]);
  118. webpackConfig.devServer = {
  119. contentBase: path.resolve(__dirname, 'dist'),
  120. compress: true, //gzip压缩
  121. //host:'192.168.191.1',
  122. historyApiFallback: true
  123. };
  124. }