UNPKG

uni-plugin-light

Version:

uni-app相关插件、loader及webpack基础配置

151 lines (135 loc) 5.02 kB
'use strict'; var fs = require('fs'); var path = require('path'); var tComm = require('t-comm'); var loaderUtils = require('loader-utils'); function _interopNamespace(e) { if (e && e.__esModule) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n["default"] = e; return Object.freeze(n); } var fs__namespace = /*#__PURE__*/_interopNamespace(fs); var path__namespace = /*#__PURE__*/_interopNamespace(path); function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function getRootDir() { return process.cwd(); } function getAppDir() { if (process.env.VUE_APP_DIR) { return process.env.VUE_APP_DIR; } if (process.env.UNI_INPUT_DIR) { return process.env.UNI_INPUT_DIR; } var dir = tComm.readEnvVariable('VUE_APP_DIR', path__namespace.join(getRootDir(), '.env.local')); if (dir) { return dir; } return ''; } function getStyleName() { var configPath = path__namespace.resolve(getRootDir(), 'src', getAppDir(), 'config.js'); var config = { styleName: '' }; if (fs__namespace.existsSync(configPath)) { // eslint-disable-next-line @typescript-eslint/no-require-imports config = require(configPath); } var _config = config, styleName = _config.styleName; return styleName; } var TIP_STYLE_NAME = '@TIP_STYLE_NAME'; var ALL_PLATFORM = 'ALL'; function shouldUseLoader() { var defaultPlatforms = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var options = loaderUtils.getOptions(this) || {}; var _options$platforms = options.platforms, platforms = _options$platforms === void 0 ? defaultPlatforms : _options$platforms; var platform = process.env.UNI_PLATFORM || ''; if (platforms === ALL_PLATFORM || platforms.indexOf(ALL_PLATFORM) > -1) { return true; } return platforms.includes(platform); } function crossGameStyleLoader(source) { var _this = this; if (!shouldUseLoader.call(this, [ALL_PLATFORM])) return source; // 改为异步loader var callback = this.async(); var options = loaderUtils.getOptions(this); if (source.indexOf(TIP_STYLE_NAME) !== -1) { var styleName = ''; // 使用env.local的样式 VUE_APP_DIR = module/ingame-nba,即为nba if (options !== null && options !== void 0 && options.styleName) { // @ts-ignore styleName = options.styleName; } else if (getStyleName()) { styleName = getStyleName(); } if (Array.isArray(styleName)) { if (styleName.length > 1) { styleName = styleName.filter(function (item) { var cssAbsolutePath = "".concat(_this.context, "/css/").concat(item, ".scss"); return fs__namespace.existsSync(cssAbsolutePath); }); if (styleName.length > 1) { var styleTags = styleName.map(function (item) { return ".".concat(item, " {@import './css/").concat(item, ".scss';}"); }); callback(null, source.replace(TIP_STYLE_NAME, '').replace(/<\/style>/, "</style>".concat(['<style scoped lang="scss">'].concat(_toConsumableArray(styleTags), ['</style>']).join('')))); return; } } styleName = styleName[0] || ''; } var cssPath = "./css/".concat(styleName, ".scss"); var cssAbsolutePath = "".concat(this.context, "/css/").concat(styleName, ".scss"); var exist = fs__namespace.existsSync(cssAbsolutePath); if (exist) { callback(null, source.replace(TIP_STYLE_NAME, cssPath)); } else { callback(null, source.replace(TIP_STYLE_NAME, '')); } } else { callback(null, source); } } module.exports = crossGameStyleLoader;