utils.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. var cssLoader = {
  13. loader: 'css-loader',
  14. options: {
  15. minimize: process.env.NODE_ENV === 'production',
  16. sourceMap: options.sourceMap
  17. }
  18. }
  19. // generate loader string to be used with extract text plugin
  20. function generateLoaders (loader, loaderOptions) {
  21. var loaders = [cssLoader]
  22. if (loader) {
  23. loaders.push({
  24. loader: loader + '-loader',
  25. options: Object.assign({}, loaderOptions, {
  26. sourceMap: options.sourceMap
  27. })
  28. })
  29. }
  30. // Extract CSS when that option is specified
  31. // (which is the case during production build)
  32. if (options.extract) {
  33. return ExtractTextPlugin.extract({
  34. use: loaders,
  35. fallback: 'vue-style-loader',
  36. publicPath: '../../'
  37. })
  38. } else {
  39. return ['vue-style-loader'].concat(loaders)
  40. }
  41. }
  42. // https://vue-loader.vuejs.org/en/configurations/extract-css.html
  43. return {
  44. css: generateLoaders(),
  45. postcss: generateLoaders(),
  46. less: generateLoaders('less'),
  47. sass: generateLoaders('sass', { indentedSyntax: true }),
  48. scss: generateLoaders('sass'),
  49. stylus: generateLoaders('stylus'),
  50. styl: generateLoaders('stylus')
  51. }
  52. }
  53. // Generate loaders for standalone style files (outside of .vue)
  54. exports.styleLoaders = function (options) {
  55. var output = []
  56. var loaders = exports.cssLoaders(options)
  57. for (var extension in loaders) {
  58. var loader = loaders[extension]
  59. output.push({
  60. test: new RegExp('\\.' + extension + '$'),
  61. use: loader
  62. })
  63. }
  64. return output
  65. }