vite-plugin-vue-wizard
Version:
Wrapper for automatic add toNative() for vue-facing-decorator
93 lines • 3.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@babel/core");
var myBabelPlugin = function () {
return {
visitor: {
ImportDeclaration: function (a) {
if (a.node.source.value === 'vue-facing-decorator') {
var specifiers = a.node.specifiers;
var hasToNative = specifiers.map(function (v) { return v.imported.name; }).includes('toNative');
if (!hasToNative) {
specifiers.push({
type: 'ImportSpecifier',
importKind: 'value',
imported: {
type: 'Identifier',
name: 'toNative'
},
local: {
type: 'Identifier',
name: 'toNative'
}
});
}
}
},
ExportDefaultDeclaration: function (a) {
var declaration = a.node.declaration;
if (declaration.type !== 'ClassDeclaration') {
console.log('Skip class transform!', declaration.type);
return;
}
a.node.declaration = {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'toNative',
},
arguments: [
{
type: 'Identifier',
name: declaration.id.name,
}
]
};
var parent = a.parent;
var index = parent.body.map(function (v) { return v.type; }).indexOf('ExportDefaultDeclaration');
parent.body.splice(index, 0, declaration);
}
}
};
};
function convertTsCode(oldCode) {
if (!oldCode.includes('vue-facing-decorator')) {
return '';
}
var transformResult = (0, core_1.transformSync)(oldCode, {
parserOpts: {
sourceType: 'module',
plugins: ['typescript', 'decorators-legacy']
},
plugins: [myBabelPlugin]
});
if (transformResult) {
return transformResult.code;
}
}
var vueWizard = function () { return ({
name: 'vite-plugin-vue-wizard',
transform: function (content, path) {
if (path.includes('node_modules')) {
return;
}
var flag = false;
function replacer() {
var matches = [];
for (var _i = 0; _i < arguments.length; _i++) {
matches[_i] = arguments[_i];
}
var result = convertTsCode(matches[2]);
if (result) {
flag = true;
}
return matches[1] + result + matches[3];
}
var replaced = content.replace(/(<script[^>]*>\s*)([^]+?)(\s*<\/script>)/g, replacer);
if (flag) {
return { code: replaced };
}
}
}); };
exports.default = vueWizard;
//# sourceMappingURL=index.js.map