utils.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict'
  2. const path = require('path')
  3. const config = require('../config')
  4. const ExtractTextPlugin = require('extract-text-webpack-plugin')
  5. exports.assetsPath = function (_path) {
  6. const assetsSubDirectory = process.env.NODE_ENV === 'production'
  7. ? config.build.assetsSubDirectory
  8. : config.dev.assetsSubDirectory
  9. return path.posix.join(assetsSubDirectory, _path)
  10. }
  11. exports.cssLoaders = function (options) {
  12. options = options || {}
  13. const cssLoader = {
  14. loader: 'css-loader',
  15. options: {
  16. minimize: process.env.NODE_ENV === 'production',
  17. sourceMap: options.sourceMap
  18. }
  19. }
  20. // generate loader string to be used with extract text plugin
  21. function generateLoaders (loader, loaderOptions) {
  22. const loaders = [cssLoader]
  23. if (loader) {
  24. loaders.push({
  25. loader: loader + '-loader',
  26. options: Object.assign({}, loaderOptions, {
  27. sourceMap: options.sourceMap
  28. })
  29. })
  30. }
  31. // Extract CSS when that option is specified
  32. // (which is the case during production build)
  33. if (options.extract) {
  34. return ExtractTextPlugin.extract({
  35. use: loaders,
  36. publicPath: '../../', // 注意配置这一部分,根据目录结构自由调整
  37. fallback: 'vue-style-loader'
  38. })
  39. } else {
  40. return ['vue-style-loader'].concat(loaders)
  41. }
  42. }
  43. function generateSassReourceLoader()
  44. {
  45. // console.log("===="+options.project,process.env.NODE_ENV);
  46. const loaders = [cssLoader,'sass-loader'];
  47. loaders.push( {
  48. loader:"sass-resources-loader",
  49. options:{
  50. resources:[
  51. path.resolve(__dirname,'../src/assets/css/login/'+options.project+".scss")
  52. ]
  53. }
  54. })
  55. // Extract CSS when that option is specified
  56. // (which is the case during production build)
  57. if (options.extract) {
  58. return ExtractTextPlugin.extract({
  59. use: loaders,
  60. publicPath: '../../', // 注意配置这一部分,根据目录结构自由调整
  61. fallback: 'vue-style-loader'
  62. })
  63. } else {
  64. return ['vue-style-loader'].concat(loaders)
  65. }
  66. }
  67. // https://vue-loader.vuejs.org/en/configurations/extract-css.html
  68. return {
  69. css: generateLoaders(),
  70. postcss: generateLoaders(),
  71. less: generateLoaders('less'),
  72. // sass: generateLoaders('sass', { indentedSyntax: true,data:"$project: '"+project+"';" }),
  73. // scss: generateLoaders('sass', { data:"$project: '"+project+"';" }),
  74. sass: generateSassReourceLoader(),
  75. scss: generateSassReourceLoader(),
  76. stylus: generateLoaders('stylus'),
  77. styl: generateLoaders('stylus')
  78. }
  79. }
  80. // Generate loaders for standalone style files (outside of .vue)
  81. exports.styleLoaders = function (options) {
  82. const output = []
  83. const loaders = exports.cssLoaders(options)
  84. for (const extension in loaders) {
  85. const loader = loaders[extension]
  86. // console.log(JSON.stringify(loader))
  87. output.push({
  88. test: new RegExp('\\.' + extension + '$'),
  89. use: loader
  90. })
  91. }
  92. return output
  93. }