utils.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. "use strict";
  2. const path = require("path");
  3. const config = require("../config");
  4. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  5. const packageConfig = require("../package.json");
  6. const chalk = require("chalk");
  7. const defaultTheme = "default"; // 默认主题
  8. exports.assetsPath = function(_path) {
  9. const assetsSubDirectory =
  10. process.env.NODE_ENV === "production" ? config.build.assetsSubDirectory : config.dev.assetsSubDirectory;
  11. return path.posix.join(assetsSubDirectory, _path);
  12. };
  13. exports.cssLoaders = function(options) {
  14. options = options || {};
  15. const cssLoader = {
  16. loader: "css-loader",
  17. options: {
  18. sourceMap: options.sourceMap
  19. }
  20. };
  21. const postcssLoader = {
  22. loader: "postcss-loader",
  23. options: {
  24. sourceMap: options.sourceMap
  25. }
  26. };
  27. // generate loader string to be used with extract text plugin
  28. function generateLoaders(loader, loaderOptions) {
  29. const loaders = [];
  30. // Extract CSS when that option is specified
  31. // (which is the case during production build)
  32. if (options.extract) {
  33. // loaders.push(MiniCssExtractPlugin.loader);
  34. // Tips: repaired css files contain image's url address, TangDM.2019.04.29
  35. loaders.push({
  36. loader: MiniCssExtractPlugin.loader,
  37. options: {
  38. publicPath: "../../"
  39. }
  40. });
  41. } else {
  42. loaders.push("vue-style-loader");
  43. }
  44. loaders.push(cssLoader);
  45. if (options.usePostCSS) {
  46. loaders.push(postcssLoader);
  47. }
  48. if (loader) {
  49. // 修改这一块,当使用sass-resources时也需要引入'sass-loader'
  50. if (loader === "sass-resources") {
  51. loaders.push({
  52. loader: "sass-loader"
  53. });
  54. }
  55. loaders.push({
  56. loader: loader + "-loader",
  57. options: Object.assign({}, loaderOptions, {
  58. sourceMap: options.sourceMap
  59. })
  60. });
  61. }
  62. return loaders;
  63. }
  64. function resolveResource() {
  65. // config arr props
  66. let _arr = [];
  67. // default other variables
  68. let otherVariables = ["common.scss"];
  69. // construct skin theme styles
  70. let name = process.env.NODE_ENV === "theme" ? process.argv[2] : defaultTheme;
  71. otherVariables.push("variable-theme-" + name + ".scss");
  72. // other variable scss files
  73. otherVariables.forEach(item => {
  74. _arr.push(path.resolve(__dirname, "../src/assets/style/" + item));
  75. });
  76. console.table(_arr);
  77. return _arr;
  78. }
  79. // https://vue-loader.vuejs.org/en/configurations/extract-css.html
  80. return {
  81. css: generateLoaders(),
  82. postcss: generateLoaders(),
  83. less: generateLoaders("less"),
  84. sass: generateLoaders("sass", { indentedSyntax: true }),
  85. scss: generateLoaders("sass"),
  86. // sass: generateLoaders("sass-resources", {
  87. // resources: resolveResource()
  88. // }),
  89. // scss: generateLoaders("sass-resources", {
  90. // resources: resolveResource()
  91. // }),
  92. stylus: generateLoaders("stylus"),
  93. styl: generateLoaders("stylus")
  94. };
  95. };
  96. // Generate loaders for standalone style files (outside of .vue)
  97. exports.styleLoaders = function(options) {
  98. const output = [];
  99. const loaders = exports.cssLoaders(options);
  100. for (const extension in loaders) {
  101. const loader = loaders[extension];
  102. output.push({
  103. test: new RegExp("\\." + extension + "$"),
  104. use: loader
  105. });
  106. }
  107. return output;
  108. };
  109. exports.createNotifierCallback = () => {
  110. const notifier = require("node-notifier");
  111. return (severity, errors) => {
  112. if (severity !== "error") return;
  113. const error = errors[0];
  114. const filename = error.file && error.file.split("!").pop();
  115. notifier.notify({
  116. title: packageConfig.name,
  117. message: severity + ": " + error.name,
  118. subtitle: filename || "",
  119. icon: path.join(__dirname, "logo.png")
  120. });
  121. };
  122. };