utils.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. var path = require('path')
  2. var fs = require('fs')
  3. var config = require('../config')
  4. var ExtractTextPlugin = require('extract-text-webpack-plugin')
  5. var mpvueInfo = require('../node_modules/mpvue/package.json')
  6. var packageInfo = require('../package.json')
  7. var mkdirp = require('mkdirp')
  8. exports.assetsPath = function (_path) {
  9. var assetsSubDirectory = process.env.NODE_ENV === 'production'
  10. ? config.build.assetsSubDirectory
  11. : config.dev.assetsSubDirectory
  12. return path.posix.join(assetsSubDirectory, _path)
  13. }
  14. exports.cssLoaders = function (options) {
  15. options = options || {}
  16. var cssLoader = {
  17. loader: 'css-loader',
  18. options: {
  19. minimize: process.env.NODE_ENV === 'production',
  20. sourceMap: options.sourceMap
  21. }
  22. }
  23. var postcssLoader = {
  24. loader: 'postcss-loader',
  25. options: {
  26. sourceMap: true
  27. }
  28. }
  29. var px2rpxLoader = {
  30. loader: 'px2rpx-loader',
  31. options: {
  32. baseDpr: 1,
  33. rpxUnit: 0.5
  34. }
  35. }
  36. // generate loader string to be used with extract text plugin
  37. function generateLoaders (loader, loaderOptions) {
  38. var loaders = [cssLoader, px2rpxLoader, postcssLoader]
  39. if (loader) {
  40. loaders.push({
  41. loader: loader + '-loader',
  42. options: Object.assign({}, loaderOptions, {
  43. sourceMap: options.sourceMap
  44. })
  45. })
  46. }
  47. // Extract CSS when that option is specified
  48. // (which is the case during production build)
  49. if (options.extract) {
  50. return ExtractTextPlugin.extract({
  51. use: loaders,
  52. fallback: 'vue-style-loader'
  53. })
  54. } else {
  55. return ['vue-style-loader'].concat(loaders)
  56. }
  57. }
  58. // https://vue-loader.vuejs.org/en/configurations/extract-css.html
  59. return {
  60. css: generateLoaders(),
  61. wxss: generateLoaders(),
  62. postcss: generateLoaders(),
  63. less: generateLoaders('less'),
  64. sass: generateLoaders('sass', { indentedSyntax: true }),
  65. scss: generateLoaders('sass'),
  66. stylus: generateLoaders('stylus'),
  67. styl: generateLoaders('stylus')
  68. }
  69. }
  70. // Generate loaders for standalone style files (outside of .vue)
  71. exports.styleLoaders = function (options) {
  72. var output = []
  73. var loaders = exports.cssLoaders(options)
  74. for (var extension in loaders) {
  75. var loader = loaders[extension]
  76. output.push({
  77. test: new RegExp('\\.' + extension + '$'),
  78. use: loader
  79. })
  80. }
  81. return output
  82. }
  83. const writeFile = async (filePath, content) => {
  84. let dir = path.dirname(filePath)
  85. let exist = fs.existsSync(dir)
  86. if (!exist) {
  87. await mkdirp(dir)
  88. }
  89. await fs.writeFileSync(filePath, content, 'utf8')
  90. }
  91. exports.writeFrameworkinfo = function () {
  92. var buildInfo = {
  93. 'toolName': mpvueInfo.name,
  94. 'toolFrameWorkVersion': mpvueInfo.version,
  95. 'toolCliVersion': packageInfo.mpvueTemplateProjectVersion || '',
  96. 'createTime': Date.now()
  97. }
  98. var content = JSON.stringify(buildInfo)
  99. var fileName = '.frameworkinfo'
  100. var rootDir = path.resolve(__dirname, `../${fileName}`)
  101. var distDir = path.resolve(config.build.assetsRoot, `./${fileName}`)
  102. writeFile(rootDir, content)
  103. writeFile(distDir, content)
  104. }