uni-plugin-light
Version:
uni-app相关插件、loader及webpack基础配置
129 lines (119 loc) • 4.34 kB
JavaScript
;
var tComm = require('t-comm');
var loaderUtils = require('loader-utils');
require('fs');
var path = require('path');
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 path__namespace = /*#__PURE__*/_interopNamespace(path);
/* eslint-disable @typescript-eslint/no-require-imports */
function getRelativePath(filePath) {
return path__namespace.relative(process.cwd(), path__namespace.resolve(filePath));
}
var LOG_KEY = 'LOADER_LOGS';
function recordLoaderLog(file, content) {
if (!global[LOG_KEY]) {
global[LOG_KEY] = {};
}
if (!global[LOG_KEY][file]) {
global[LOG_KEY][file] = [];
}
global[LOG_KEY][file].push(content);
}
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);
}
var PLATFORMS_MP = [PLATFORM_MAP.MP_WX, PLATFORM_MAP.MP_QQ];
/**
* 转换vant等组件,比如
* import { Tab, Tabs } from 'vant' 转为:import { Tab, Tabs } from 'test';
* import List from 'vant/lib/list' 转为:import List from 'test';
* import 'vant/lib/list/index.css' 转为:import 'test'
*/
function replaceLibrary(source) {
tComm.replaceAllPolyfill();
if (!shouldUseLoader.call(this, PLATFORMS_MP)) return source;
var options = loaderUtils.getOptions(this) || {};
var _options$replaceLibra = options.replaceLibraryList,
replaceLibraryList = _options$replaceLibra === void 0 ? [] : _options$replaceLibra,
_options$replaceConte = options.replaceContentList,
replaceContentList = _options$replaceConte === void 0 ? [] : _options$replaceConte;
if (!(replaceLibraryList !== null && replaceLibraryList !== void 0 && replaceLibraryList.length) && !replaceContentList.length) {
return source;
}
var resourcePath = this.resourcePath;
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (var i = 0; i < replaceContentList.length; i++) {
var _replaceContentList$i = replaceContentList[i],
path = _replaceContentList$i.path,
_replaceContentList$i2 = _replaceContentList$i.content,
content = _replaceContentList$i2 === void 0 ? '' : _replaceContentList$i2;
var tContent = typeof content === 'function' ? content() : content;
if (resourcePath.match(new RegExp(path))) {
recordLoaderLog('replace-library.json', {
file: getRelativePath(resourcePath),
type: 'CONTENT',
path: path
});
return tContent;
}
}
var res = source;
// eslint-disable-next-line @typescript-eslint/prefer-for-of
var _loop = function _loop() {
var _replaceLibraryList$_ = replaceLibraryList[_i],
from = _replaceLibraryList$_.from,
to = _replaceLibraryList$_.to,
exact = _replaceLibraryList$_.exact;
if (exact) {
res = res.replaceAll("'".concat(from, "'"), "'".concat(to, "'"));
} else {
var importRe = new RegExp("(?<=import(?:[\\s\\S]+from)?\\s+)(?:'|\")(".concat(from, "[\\w\\/\\-\\.]*)(?:'|\")"));
if (importRe.test(res)) {
res = res.replaceAll(importRe, function () {
return "'".concat(to, "'");
});
recordLoaderLog('replace-library.json', {
file: getRelativePath(resourcePath),
type: 'LIBRARY',
from: from,
to: to
});
}
}
};
for (var _i = 0; _i < replaceLibraryList.length; _i++) {
_loop();
}
return res;
}
module.exports = replaceLibrary;