utils.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. var path = require('path')
  2. var config = require('../config')
  3. var ExtractTextPlugin = require('extract-text-webpack-plugin')
  4. exports.assetsPath = function (_path) {
  5. var assetsSubDirectory = process.env.NODE_ENV === 'production'
  6. ? config.build.assetsSubDirectory
  7. : config.dev.assetsSubDirectory
  8. return path.posix.join(assetsSubDirectory, _path)
  9. }
  10. exports.cssLoaders = function (options) {
  11. options = options || {}
  12. // generate loader string to be used with extract text plugin
  13. function generateLoaders (loaders) {
  14. var sourceLoader = loaders.map(function (loader) {
  15. var extraParamChar
  16. if (/\?/.test(loader)) {
  17. loader = loader.replace(/\?/, '-loader?')
  18. extraParamChar = '&'
  19. } else {
  20. loader = loader + '-loader'
  21. extraParamChar = '?'
  22. }
  23. return loader + (options.sourceMap ? extraParamChar + 'sourceMap' : '')
  24. }).join('!')
  25. // Extract CSS when that option is specified
  26. // (which is the case during production build)
  27. if (options.extract) {
  28. return ExtractTextPlugin.extract('vue-style-loader', sourceLoader)
  29. } else {
  30. return ['vue-style-loader', sourceLoader].join('!')
  31. }
  32. }
  33. // http://vuejs.github.io/vue-loader/configurations/extract-css.html
  34. return {
  35. css: generateLoaders(['css']),
  36. postcss: generateLoaders(['css']),
  37. less: generateLoaders(['css', 'less']),
  38. sass: generateLoaders(['css', 'sass?indentedSyntax']),
  39. scss: generateLoaders(['css', 'sass']),
  40. stylus: generateLoaders(['css', 'stylus']),
  41. styl: generateLoaders(['css', 'stylus'])
  42. }
  43. }
  44. // Generate loaders for standalone style files (outside of .vue)
  45. exports.styleLoaders = function (options) {
  46. var output = []
  47. var loaders = exports.cssLoaders(options)
  48. for (var extension in loaders) {
  49. var loader = loaders[extension]
  50. output.push({
  51. test: new RegExp('\\.' + extension + '$'),
  52. loader: loader
  53. })
  54. }
  55. return output
  56. }