UNPKG

uni-plugin-light

Version:

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

95 lines (85 loc) 2.93 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var fs = require('fs'); 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 BASE_SCSS = 'base.scss'; function getStyleList(dir) { var cssList = fs__namespace.readdirSync(dir); var filtered = cssList.filter(function (item) { return item.endsWith('scss') && !item.startsWith(BASE_SCSS); }).map(function (item) { return item.replace(/\.scss$/, ''); }); return filtered; } function genInjectContent(_ref) { var styleList = _ref.styleList, componentName = _ref.componentName, topElement = _ref.topElement, _ref$dir = _ref.dir, dir = _ref$dir === void 0 ? '' : _ref$dir; var styleStr = styleList.map(function (item) { return "\n &--type-".concat(item, " {\n @import './").concat(dir).concat(item, ".scss';\n }"); }).join('\n'); return "\n".concat(topElement, ".").concat(componentName, " {\n ").concat(styleStr, "\n}\n"); } function getComponentName(dir) { var match = dir.match(/\/([^/]+)\/css/); return (match === null || match === void 0 ? void 0 : match[1]) || ''; } var PLATFORM_MAP = { MP_WX: 'mp-weixin', MP_QQ: 'mp-qq', H5: 'h5' }; 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 injectDynamicStyleWeb(source) { if (!shouldUseLoader.call(this, [PLATFORM_MAP.H5])) return source; var options = loaderUtils.getOptions(this) || {}; var _options$topElement = options.topElement, topElement = _options$topElement === void 0 ? 'body' : _options$topElement; var dir = this.context; var componentName = getComponentName(dir); if (!componentName) { return source; } var styleList = getStyleList(dir); var injectContent = genInjectContent({ styleList: styleList, componentName: componentName, topElement: topElement, dir: '' }); return "".concat(source, "\n").concat(injectContent); } exports["default"] = injectDynamicStyleWeb; exports.getStyleList = getStyleList;