utils.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. })
  37. } else {
  38. return ['vue-style-loader'].concat(loaders)
  39. }
  40. }
  41. // https://vue-loader.vuejs.org/en/configurations/extract-css.html
  42. return {
  43. css: generateLoaders(),
  44. postcss: generateLoaders(),
  45. less: generateLoaders('less'),
  46. sass: generateLoaders('sass', { indentedSyntax: true }),
  47. scss: generateLoaders('sass'),
  48. stylus: generateLoaders('stylus'),
  49. styl: generateLoaders('stylus')
  50. }
  51. }
  52. // Generate loaders for standalone style files (outside of .vue)
  53. exports.styleLoaders = function (options) {
  54. var output = []
  55. var loaders = exports.cssLoaders(options)
  56. for (var extension in loaders) {
  57. var loader = loaders[extension]
  58. output.push({
  59. test: new RegExp('\\.' + extension + '$'),
  60. use: loader
  61. })
  62. }
  63. return output
  64. }