webpack.prod.config.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 = ''
  11. const filepath = '/zt2017/fnExama/'
  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: ['./src/lib/rem', 'vue']
  19. }
  20. var plugins = [
  21. new webpack.optimize.CommonsChunkPlugin('vendor'),
  22. new webpack.optimize.UglifyJsPlugin(),
  23. new webpack.ProvidePlugin({
  24. _: "underscore"
  25. }),
  26. new webpack.DefinePlugin({
  27. 'process.env': {
  28. 'NODE_ENV': JSON.stringify('production')
  29. }
  30. }),
  31. new ExtractTextPlugin({
  32. filename: "css/global_[contenthash:6].css"
  33. }),
  34. ]
  35. readEntryFile().forEach(function (file) {
  36. plugins.push(new HtmlWebpackPlugin({
  37. filename: file.name + '.html',
  38. template: file.htmlentry,
  39. inject: 'body',
  40. chunksSortMode: 'manual',
  41. chunks: ['vendor', file.name],
  42. }))
  43. entry[file.name] = file.jsentry
  44. }, this);
  45. module.exports = {
  46. entry: entry,
  47. output: {
  48. publicPath: cdn,
  49. filename: 'js/[name]_[chunkhash:6].js',
  50. path: path.resolve(staticPath, '')
  51. },
  52. module: {
  53. rules: [
  54. {
  55. test: /\.js$/,
  56. use: [{
  57. loader: 'babel-loader'
  58. }],
  59. exclude: /node_modules/,
  60. },
  61. {
  62. test: /\.css$/,
  63. use: ExtractTextPlugin.extract({
  64. fallback: "style-loader",
  65. use: "css-loader",
  66. })
  67. },
  68. {
  69. test: /\.scss$/, use: ExtractTextPlugin.extract({
  70. fallback: 'style-loader',
  71. use: [{
  72. loader: "css-loader",
  73. }, {
  74. loader: "postcss-loader",
  75. options: {
  76. ident: 'postcss',
  77. plugins: () => [
  78. autoprefixer({
  79. browsers: ['last 2 versions', 'Firefox ESR', '> 1%', 'ie >= 8', 'iOS >= 8', 'Android >= 4'],
  80. }),
  81. postcssSprites({
  82. stylesheetPath: './src/img',
  83. spritePath: path.resolve(staticPath, './img/sprite'),
  84. filterBy: [
  85. function (image) {
  86. if (image.originalUrl.indexOf('?__sprite') === -1) {
  87. return Promise.reject()
  88. }
  89. return Promise.resolve()
  90. }
  91. ]
  92. })
  93. ],
  94. },
  95. }, {
  96. loader: "sass-loader"
  97. }]
  98. })
  99. },
  100. {
  101. test: /\.html$/,
  102. use: [{
  103. loader: 'html-loader',
  104. options: {
  105. attrs: ['img:src', 'img:data-src'],
  106. minimize: false
  107. }
  108. }]
  109. },
  110. {
  111. test: /\.(gif|jpg|png|woff|svg|eot|ttf)\??.*$/,
  112. loader: 'file-loader',
  113. options: {
  114. useRelativePath: true,
  115. name(file) {
  116. if (file.indexOf('sprite') === -1)
  117. return '[name]_[hash:6].[ext]'
  118. else
  119. return 'sprite/[name]_[hash:6].[ext]'
  120. },
  121. }
  122. },
  123. {
  124. test: /\.tmpl$/,
  125. use: 'raw-loader'
  126. }
  127. ]
  128. },
  129. // devtool: 'source-map',
  130. plugins: plugins,
  131. resolve: {
  132. alias: {
  133. 'vue$': 'vue/dist/vue.esm.js',
  134. "jsonp": path.resolve(__dirname, './src/lib/jsonp.js')
  135. }
  136. }
  137. }