webpack.config.js 3.5 KB

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