utils.js 2.0 KB

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