plugin-light
Version:
Webpack 相关插件、Loader、基础配置及 CLI 命令
213 lines (203 loc) • 9.9 kB
JavaScript
;
function _arrayLikeToArray$1(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$1(arr);
}
function _iterableToArray(iter) {
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _unsupportedIterableToArray$1(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray$1(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$1(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$1(arr) || _nonIterableSpread();
}
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
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 _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; }
/* eslint-disable @typescript-eslint/no-require-imports */
var parser = require('@babel/parser');
var traverse = require('@babel/traverse');
var generator = require('@babel/generator');
var htmlparser2 = require('htmlparser2');
var render = require('dom-serializer')["default"];
function swipeComponent(source) {
if (process.env.VUE_APP_PLATFORM === 'h5') {
return source;
}
var templateResult = handleTemplate(source);
var scriptResult = handleScript(source);
var styleResult = handleStyle(source);
var outputResult = "".concat(templateResult, "\n").concat(scriptResult, "\n").concat(styleResult, "\n");
return outputResult;
}
function handleTemplate(fileContent) {
var firstIndex = fileContent.indexOf('<template');
var lastIndex = fileContent.lastIndexOf('</template>');
var content = fileContent.slice(firstIndex + '<template'.length + 1, lastIndex);
var dom = htmlparser2.parseDocument(content, {
lowerCaseTags: false,
lowerCaseAttributeNames: false
});
function getElementsByTagName(dom, name) {
var result = [];
var _iterator = _createForOfIteratorHelper(dom.children),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var child = _step.value;
if (child.name == name) {
result.push(child);
}
if (child.children) {
result.push.apply(result, _toConsumableArray(getElementsByTagName(child, name)));
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return result;
}
function replaceAttr(attrs, searchName, replaceName) {
var expand = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
if (Object.keys(attrs).includes(searchName)) {
attrs[replaceName] = attrs[searchName];
}
if (expand && Object.keys(attrs).includes(":".concat(searchName))) {
replaceAttr(attrs, ":".concat(searchName), ":".concat(replaceName), false);
}
}
var elements = getElementsByTagName(dom, 'Swipe');
elements.push.apply(elements, _toConsumableArray(getElementsByTagName(dom, 'van-swipe')));
if (elements === null || elements.length === 0) {
return "<template>\n".concat(content, "\n</template>\n");
}
var vantSwipeItemCompNames = ['SwipeItem', 'van-swipe-item'];
var _iterator2 = _createForOfIteratorHelper(elements),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var element = _step2.value;
element.name = 'swiper';
if (element.attribs[':indicator-dots'] == null) {
element.attribs['indicator-dots'] = element.attribs['indicator-dots'] || 'true'; // 加个默认为true
}
if (element.attribs[':indicator-active-color'] == null) {
element.attribs['indicator-active-color'] = element.attribs['indicator-color'] || '#1989fa';
}
if (element.attribs[':indicator-color'] == null) {
element.attribs['indicator-color'] = element.attribs['indicator-inactive-color'] || 'rgba(0, 0, 0, .3)';
}
var _iterator3 = _createForOfIteratorHelper(element.children),
_step3;
try {
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
var child = _step3.value;
if (vantSwipeItemCompNames.includes(child.name)) {
child.name = 'swiper-item';
}
}
} catch (err) {
_iterator3.e(err);
} finally {
_iterator3.f();
}
var autoplayKeys = ['autoplay', ':autoplay'];
for (var _i = 0, _autoplayKeys = autoplayKeys; _i < _autoplayKeys.length; _i++) {
var autoplayKey = _autoplayKeys[_i];
if (Object.keys(element.attribs).includes(autoplayKey)) {
element.attribs[autoplayKey] = String(parseInt(element.attribs[autoplayKey]) > 0);
}
}
replaceAttr(element.attribs, 'loop', 'circular');
replaceAttr(element.attribs, 'show-indicators', 'indicator-dots');
// replaceAttr(element.attribs, 'indicator-color', 'indicator-active-color');
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
var output = render(dom, {
decodeEntities: false
});
return "<template>\n".concat(output, "\n</template>\n");
}
function handleScript(fileContent) {
var regex = /<script(.)*?>/;
var match = fileContent.match(regex);
if (!match) {
return '<script></script>';
}
var firstIndex = match.index + match[0].length;
var lastIndex = fileContent.lastIndexOf('</script>');
var content = fileContent.slice(firstIndex, lastIndex);
var ast = parser.parse(content, {
sourceType: 'module'
});
var visitor = {
ImportSpecifier: function ImportSpecifier(path) {
// import { Swipe, SwipeItem, Tab, Toast, Tabs } from 'vant';
if (path.node.imported.name === 'Swipe' || path.node.imported.name === 'SwipeItem') {
path.remove();
}
},
ImportDefaultSpecifier: function ImportDefaultSpecifier(path) {
// import Swipe from 'vant/lib/swipe';
// import SwipeItem from 'vant/lib/swipe-item';
if (path.node.local.name === 'Swipe' || path.node.local.name === 'SwipeItem') {
path.remove();
}
},
ImportDeclaration: function ImportDeclaration(path) {
if (path.node.specifiers.length === 0 && (path.node.source.value === 'vant/lib/swipe/index.css' || path.node.source.value === 'vant/lib/swipe-item/index.css')) {
// import 'vant/lib/swipe/index.css';
// import 'vant/lib/swipe-item/index.css';
path.remove();
}
},
ExpressionStatement: function ExpressionStatement(path) {
var _path$node$expression, _path$node$expression2, _path$node$expression3;
// Vue.use(Swipe);
// Vue.use(SwipeItem);
// Vue.use(Swipe).use(SwipeItem);
if (((_path$node$expression = path.node.expression.arguments) === null || _path$node$expression === void 0 ? void 0 : _path$node$expression.length) > 0 && (((_path$node$expression2 = path.node.expression.arguments[0]) === null || _path$node$expression2 === void 0 ? void 0 : _path$node$expression2.name) === 'Swipe' || ((_path$node$expression3 = path.node.expression.arguments[0]) === null || _path$node$expression3 === void 0 ? void 0 : _path$node$expression3.name) === 'SwipeItem')) {
path.remove();
}
},
Property: function Property(path) {
/*
components: {
LgameMatch, Merchant, Swipe, SwipeItem, OfflineWatchSchedule, PrivilegeBrief, Ticket,
TaskDialog, ScrollView, VanLoading, Error,
},
*/
if (path.node.key.name === 'Swipe' || path.node.key.name === 'SwipeItem') {
path.remove();
}
}
};
traverse["default"](ast, visitor);
var output = generator["default"](ast, {});
return "".concat(match[0], "\n").concat(output.code, "\n</script>\n");
}
function handleStyle(fileContent) {
var index = fileContent.indexOf('<style');
var output = fileContent.slice(index);
return output;
}
module.exports = swipeComponent;