webpack.prod.config.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. const webpack = require('webpack')
  2. const path = require('path')
  3. const HtmlWebpackPlugin = require('html-webpack-plugin') //html插件
  4. const ExtractTextPlugin = require("extract-text-webpack-plugin")//文本插件
  5. const readEntryFile = require('./webpack.common.js')
  6. const autoprefixer = require('autoprefixer')
  7. const postcssSprites = require('postcss-sprites')
  8. const UglifyJSPlugin = require('uglifyjs-webpack-plugin')
  9. const svnpath = '../../../pub' || 'D://fed/pub'//支持相对和绝对地址
  10. const version = '1.0.0'
  11. const filepath = '/zt2017/webpack/'
  12. const staticPath = /:\/\//.test(svnpath) ? path.join(svnpath, filepath, version) : path.join(__dirname, svnpath, filepath, version)//根据相对还是绝对地址拼接成绝对地址
  13. const cdn = ['//pub.dwstatic.com', filepath, version].join('')
  14. /**
  15. * 默认入口是有vendor的 如果不需要可以去除
  16. */
  17. var entry = {
  18. vendor: 'jquery'
  19. }
  20. var plugins = [
  21. new webpack.optimize.CommonsChunkPlugin('vendor'),
  22. new webpack.optimize.UglifyJsPlugin(),
  23. new webpack.ProvidePlugin({
  24. $: "jquery",
  25. jQuery: "jquery",
  26. "window.jQuery": "jquery"
  27. }),
  28. new webpack.DefinePlugin({
  29. 'process.env': {
  30. 'NODE_ENV': JSON.stringify('production')
  31. }
  32. }),
  33. new ExtractTextPlugin({
  34. filename: "css/global_[contenthash:6].css"
  35. }),
  36. ]
  37. readEntryFile().forEach(function (file) {
  38. plugins.push(new HtmlWebpackPlugin({
  39. filename: file.name + '.html',
  40. template: file.htmlentry,
  41. inject: 'head',
  42. chunksSortMode: 'manual',
  43. chunks: ['vendor', file.name],
  44. }))
  45. entry[file.name] = file.jsentry
  46. }, this);
  47. module.exports = {
  48. entry: entry,
  49. output: {
  50. publicPath: cdn,
  51. filename: 'js/[name]_[chunkhash:6].js',
  52. path: path.resolve(staticPath, '')
  53. },
  54. module: {
  55. rules: [
  56. {
  57. test: /\.js$/,
  58. use: [{
  59. loader: 'babel-loader'
  60. }],
  61. exclude: /node_modules/,
  62. },
  63. {
  64. test: /\.css$/,
  65. use: ExtractTextPlugin.extract({
  66. fallback: "style-loader",
  67. use: "css-loader",
  68. publicPath: path.resolve(cdn, './css')
  69. })
  70. },
  71. {
  72. test: /\.scss$/, use: ExtractTextPlugin.extract({
  73. fallback: 'style-loader',
  74. publicPath: path.resolve(cdn, './css'),
  75. use: [{
  76. loader: "css-loader",
  77. }, {
  78. loader: "postcss-loader",
  79. options: {
  80. ident: 'postcss',
  81. plugins: () => [
  82. autoprefixer({
  83. browsers: ['last 2 versions', 'Firefox ESR', '> 1%', 'ie >= 8', 'iOS >= 8', 'Android >= 4'],
  84. }),
  85. postcssSprites({
  86. stylesheetPath: './src/img',
  87. spritePath: path.resolve(staticPath, './img/sprite'),
  88. filterBy: [
  89. function (image) {
  90. if (image.originalUrl.indexOf('?__sprite') === -1) {
  91. return Promise.reject()
  92. }
  93. return Promise.resolve()
  94. }
  95. ]
  96. })
  97. ],
  98. },
  99. }, {
  100. loader: "sass-loader"
  101. }]
  102. })
  103. },
  104. {
  105. test: require.resolve('jquery'),
  106. use: [{
  107. loader: 'expose-loader',
  108. options: 'jQuery'
  109. }, {
  110. loader: 'expose-loader',
  111. options: '$'
  112. }]
  113. },
  114. {
  115. test: /\.(gif|jpg|png|woff|svg|eot|ttf)\??.*$/,
  116. loader: 'file-loader',
  117. options: {
  118. publicPath: cdn,
  119. outputPath: '/img/',
  120. name(file) {
  121. if (file.indexOf('sprite') === -1)
  122. return '[name]_[hash:6].[ext]'
  123. else
  124. return 'sprite/[name]_[hash:6].[ext]'
  125. },
  126. }
  127. },
  128. {
  129. test: /\.tmpl$/,
  130. use: 'raw-loader'
  131. }
  132. ]
  133. },
  134. // devtool: 'source-map',
  135. plugins: plugins
  136. }