webpack.prod.config.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 = '/zt2018/tankehezih5/'
  12. const staticPath = /:\/\//.test(svnpath) ? path.joiwn(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. }
  19. var plugins = [
  20. new webpack.optimize.CommonsChunkPlugin('vendor'),
  21. new webpack.optimize.UglifyJsPlugin(),
  22. new webpack.ProvidePlugin({
  23. _: "underscore"
  24. }),
  25. new webpack.DefinePlugin({
  26. 'process.env': {
  27. 'NODE_ENV': JSON.stringify('production')
  28. }
  29. }),
  30. new ExtractTextPlugin({
  31. filename: "css/[name].css",
  32. allChunks: true
  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: '/',
  49. filename: 'js/[name].js',
  50. path: path.resolve(staticPath, '')
  51. },
  52. module: {
  53. rules: [
  54. {
  55. test: /\.vue$/,
  56. loader: 'vue-loader',
  57. exclude: /node_modules/
  58. },
  59. {
  60. test: /\.js$/,
  61. use: [{
  62. loader: 'babel-loader'
  63. }],
  64. exclude: /node_modules/,
  65. },
  66. {
  67. test: /\.css$/,
  68. use: ExtractTextPlugin.extract({
  69. fallback: "style-loader",
  70. use: "css-loader",
  71. publicPath: path.resolve(cdn, './css')
  72. })
  73. },
  74. {
  75. test: /\.scss$/, use: ExtractTextPlugin.extract({
  76. fallback: 'style-loader',
  77. use: [{
  78. loader: "css-loader",
  79. }, {
  80. loader: "postcss-loader",
  81. options: {
  82. ident: 'postcss',
  83. plugins: () => [
  84. autoprefixer({
  85. browsers: ['last 2 versions', 'Firefox ESR', '> 1%', 'ie >= 8', 'iOS >= 8', 'Android >= 4'],
  86. }),
  87. postcssSprites({
  88. stylesheetPath: './src/img',
  89. spritePath: path.resolve(staticPath, './img/sprite'),
  90. filterBy: [
  91. function (image) {
  92. if (image.originalUrl.indexOf('?__sprite') === -1) {
  93. return Promise.reject()
  94. }
  95. return Promise.resolve()
  96. }
  97. ]
  98. })
  99. ],
  100. },
  101. }, {
  102. loader: "sass-loader"
  103. }]
  104. })
  105. },
  106. {
  107. test: /\.html$/,
  108. use: [{
  109. loader: 'html-loader',
  110. options: {
  111. attrs: ['img:src', 'img:data-src'],
  112. minimize: false
  113. }
  114. }]
  115. },
  116. {
  117. test: /\.(gif|jpg|png|woff|svg|eot|ttf)\??.*$/,
  118. loader: 'file-loader',
  119. options: {
  120. useRelativePath: true,
  121. name(file) {
  122. if (file.indexOf('sprite') === -1)
  123. return '[name].[ext]'
  124. else
  125. return 'sprite/[name].[ext]'
  126. },
  127. }
  128. },
  129. {
  130. test: /\.tmpl$/,
  131. use: 'raw-loader'
  132. }
  133. ]
  134. },
  135. // devtool: 'source-map',
  136. plugins: plugins,
  137. resolve: {
  138. alias: {
  139. 'vue$': 'vue/dist/vue.esm.js',
  140. "jsonp": path.resolve(__dirname, './src/lib/jsonp.js'),
  141. "lib": path.resolve(__dirname, './src/lib/library.js')
  142. }
  143. }
  144. }