webpack.prod.config.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 svnpath = '../../../pub' || 'D://fed/pub'//支持相对和绝对地址
  9. const version = ''
  10. const filepath = '/zq2018/tksjbox/'
  11. const staticPath = /:\/\//.test(svnpath) ? path.join(svnpath, filepath, version) : path.join(__dirname, svnpath, filepath, version)//根据相对还是绝对地址拼接成绝对地址
  12. const cdn = ['//pub.dwstatic.com', filepath, version].join('')
  13. /**
  14. * 默认入口是有vendor的 如果不需要可以去除
  15. */
  16. var entry = {
  17. vendor: ['jquery']
  18. }
  19. var plugins = [
  20. new webpack.optimize.CommonsChunkPlugin('vendor'),
  21. new webpack.optimize.UglifyJsPlugin(),
  22. new webpack.ProvidePlugin({
  23. $: "jquery",
  24. jQuery: "jquery",
  25. "window.jQuery": "jquery",
  26. _: "underscore"
  27. }),
  28. new webpack.DefinePlugin({
  29. 'process.env': {
  30. 'NODE_ENV': JSON.stringify('production')
  31. }
  32. }),
  33. new ExtractTextPlugin({
  34. filename: "css/[name]_[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: 'body',
  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\/(?!(dom7|swiper)\/).*/
  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: /\.html$/,
  116. use: [{
  117. loader: 'html-loader',
  118. options: {
  119. attrs: ['img:src', 'img:data-src'],
  120. minimize: false
  121. }
  122. }]
  123. },
  124. {
  125. test: /\.(gif|jpg|png|woff|svg|eot|ttf)\??.*$/,
  126. loader: 'file-loader',
  127. options: {
  128. publicPath: cdn,
  129. outputPath: '/img/',
  130. name(file) {
  131. if (file.indexOf('sprite') === -1)
  132. return '[name]_[hash:6].[ext]'
  133. else
  134. return 'sprite/[name]_[hash:6].[ext]'
  135. },
  136. }
  137. },
  138. {
  139. test: /\.tmpl$/,
  140. use: 'raw-loader'
  141. }
  142. ]
  143. },
  144. // devtool: 'source-map',
  145. plugins: plugins,
  146. resolve: {
  147. }
  148. }