webpack.config.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. chunkFilename: "[name].[hash].js",
  21. library: 'nutui',
  22. libraryTarget: 'umd',
  23. umdNamedDefine: true
  24. };
  25. webpackConfig.module = {
  26. rules: [{
  27. test: /\.css$/,
  28. use: ['style-loader', 'css-loader', 'postcss-loader']
  29. }, {
  30. test: /\.scss$/,
  31. use: ['style-loader', 'css-loader', 'sass-loader', 'postcss-loader']
  32. }, {
  33. test: /\.vue$/,
  34. loader: 'vue-loader',
  35. options: {
  36. loaders: {
  37. scss: 'vue-style-loader!css-loader!sass-loader'
  38. },
  39. postcss: [autoprefixer()]
  40. }
  41. }, {
  42. test: /\.js$/,
  43. loader: 'babel-loader',
  44. exclude: /node_modules/,
  45. }, {
  46. test: /\.svg$/,
  47. loader: 'svg-sprite-loader'
  48. }, {
  49. test: /\.(png|jpg|gif|webp)$/,
  50. loader: 'url-loader',
  51. options: {
  52. limit: 3000,
  53. name: 'img/[name].[ext]',
  54. }
  55. }, ]
  56. };
  57. webpackConfig.externals = {
  58. 'vue': {
  59. root: 'Vue',
  60. commonjs: 'vue',
  61. commonjs2: 'vue',
  62. amd: 'vue'
  63. }
  64. };
  65. webpackConfig.plugins = [
  66. new HtmlWebpackPlugin({
  67. filename: path.resolve(__dirname, './dist/index.html'),
  68. template: './src/index.html',
  69. hash:true,
  70. chunks: ['app']
  71. }),
  72. new HtmlWebpackPlugin({
  73. filename: path.resolve(__dirname, './dist/demo.html'),
  74. template: './src/demo.html',
  75. chunks: ['demo']
  76. }),
  77. /*new ExtractTextPlugin({
  78. filename: 'nutui.css'
  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 UglifyJsPlugin({
  104. uglifyOptions: {
  105. ecma: 8,
  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:'10.0.39.18',
  122. historyApiFallback: true
  123. };
  124. }