webpack.config.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.BannerPlugin('NutUI v' + config.version + ' ' + new Date().toString()),
  76. new CopyWebpackPlugin([{
  77. from: path.join(__dirname, "./src/asset/"),
  78. to: path.join(__dirname, "./dist/asset")
  79. },
  80. {
  81. from: path.join(__dirname, "./src/default.html"),
  82. to: path.join(__dirname, "./dist/default.html")
  83. }
  84. ])
  85. ];
  86. if (isProduction) {
  87. webpackConfig.devtool = '#cheap-module-source-map';
  88. webpackConfig.plugins = (webpackConfig.plugins || []).concat([
  89. new webpack.DefinePlugin({
  90. 'process.env': {
  91. NODE_ENV: '"production"'
  92. },
  93. 'CONFIG': config
  94. }),
  95. new webpack.LoaderOptionsPlugin({
  96. minimize: false
  97. }),
  98. new UglifyJsPlugin({
  99. uglifyOptions: {
  100. ecma: 8,
  101. warnings: false
  102. }
  103. })
  104. ])
  105. } else {
  106. webpackConfig.devtool = '#cheap-module-eval-source-map';
  107. //webpackConfig.output.publicPath = '/';
  108. webpackConfig.plugins = (webpackConfig.plugins || []).concat([
  109. new webpack.DefinePlugin({
  110. 'CONFIG': config
  111. })
  112. ]);
  113. webpackConfig.devServer = {
  114. contentBase: path.resolve(__dirname, 'dist'),
  115. compress: true, //gzip压缩
  116. //host:'10.0.39.18',
  117. historyApiFallback: true
  118. };
  119. }