marko-widgets
Version:
<h1 align="center"> <!-- Logo --> <br/> marko-widgets@8 <br/>
347 lines (346 loc) • 14 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var translate_exports = {};
__export(translate_exports, {
default: () => translate_default
});
module.exports = __toCommonJS(translate_exports);
var import_path = __toESM(require("path"));
var import_babel_utils = require("@marko/babel-utils");
var import_compiler = require("@marko/compiler");
var import_package = require("marko/package.json");
var import_compat_utils = require("@marko/compat-utils");
var import_optmize_html_writes = require("./optmize-html-writes");
var translate_default = {
MarkoTag(tag) {
const {
hub: { file }
} = tag;
const meta = file.metadata.marko;
const { widgetBind } = meta;
if (!widgetBind) return;
const wBindAttr = (0, import_compat_utils.getAttribute)(tag, "w-bind");
if (!wBindAttr) return;
const componentDefIdentifier = file._componentDefIdentifier;
if (typeof widgetBind === "object") {
const componentInstanceIdentifier = file._componentInstanceIdentifier;
const widgetTypesIdentifier = file._widgetTypesIdentifier;
const wTypeIdentifier = tag.scope.generateUidIdentifier("wtype");
const wBindIdentifier = tag.scope.generateUidIdentifier("wbind");
tag.insertBefore(
import_compiler.types.markoScriptlet([
import_compiler.types.variableDeclaration("const", [
import_compiler.types.variableDeclarator(
wTypeIdentifier,
import_compiler.types.memberExpression(
widgetTypesIdentifier,
wBindAttr.node.value,
true
)
),
import_compiler.types.variableDeclarator(
wBindIdentifier,
import_compiler.types.logicalExpression(
"&&",
wTypeIdentifier,
import_compiler.types.stringLiteral("_wbind")
)
)
]),
import_compiler.types.expressionStatement(
import_compiler.types.assignmentExpression(
"=",
componentInstanceIdentifier,
import_compiler.types.callExpression(
import_compiler.types.memberExpression(componentDefIdentifier, import_compiler.types.identifier("t")),
[wTypeIdentifier]
)
)
)
])
);
if ((0, import_compat_utils.hasAttribute)(tag, "id")) {
wBindAttr.replaceWith(import_compiler.types.markoAttribute("key", wBindIdentifier));
} else {
wBindAttr.replaceWithMultiple([
import_compiler.types.markoAttribute("key", wBindIdentifier),
import_compiler.types.markoAttribute(
"id",
import_compiler.types.logicalExpression(
"&&",
wBindIdentifier,
import_compiler.types.callExpression(
import_compiler.types.memberExpression(
componentInstanceIdentifier,
import_compiler.types.identifier("elId")
),
[wBindIdentifier]
)
)
)
]);
}
} else if ((0, import_compat_utils.hasAttribute)(tag, "id")) {
wBindAttr.replaceWith(import_compiler.types.markoAttribute("key", import_compiler.types.stringLiteral("_wbind")));
} else {
wBindAttr.replaceWithMultiple([
import_compiler.types.markoAttribute("key", import_compiler.types.stringLiteral("_wbind")),
import_compiler.types.markoAttribute(
"id",
import_compiler.types.memberExpression(componentDefIdentifier, import_compiler.types.identifier("id"))
)
]);
}
},
Program: {
enter(program) {
const {
hub: { file }
} = program;
const meta = file.metadata.marko;
const { widgetBind } = meta;
if (!widgetBind) return;
if (typeof widgetBind === "object") {
program.unshiftContainer(
"body",
import_compiler.types.variableDeclaration("const", [
import_compiler.types.variableDeclarator(
file._widgetTypesIdentifier = program.scope.generateUidIdentifier("widget_types"),
import_compiler.types.objectExpression(
Object.entries(widgetBind).map(([name, id]) => {
return import_compiler.types.objectProperty(
import_compiler.types.stringLiteral(name),
import_compiler.types.stringLiteral(id)
);
})
)
)
])
);
}
},
exit(program) {
var _a;
const {
hub: { file }
} = program;
const meta = file.metadata.marko;
const { widgetBind } = meta;
if (!widgetBind) return;
program.skip();
const { deps } = meta;
const isImplicit = widgetBind === true;
const isDynamic = typeof widgetBind === "object";
const isSplit = isImplicit || isDynamic ? true : !/^\.(?:\/(?:index(?:\.js)?)?)?$/.test(widgetBind);
const { markoOpts } = file;
const isCJS = markoOpts.modules === "cjs";
const templateFileName = file.opts.filename;
const includeMetaInSource = markoOpts.meta !== false;
const isHTML = markoOpts.output === "html";
const renderBlock = file._renderBlock;
const componentTypeIdentifier = program.scope.generateUidIdentifier(
"marko_componentType"
);
const templateIdentifier = program.scope.generateUidIdentifier("marko_template");
const rendererIdentifier = (0, import_babel_utils.importDefault)(
file,
"marko/src/runtime/components/legacy/renderer-legacy.js",
"marko_renderer"
);
const templateRendererMember = import_compiler.types.memberExpression(
templateIdentifier,
import_compiler.types.identifier("_")
);
const templateMetaMember = import_compiler.types.memberExpression(
templateIdentifier,
import_compiler.types.identifier("meta")
);
if (markoOpts.writeVersionComment) {
program.addComment(
"leading",
` Compiled using marko@${import_package.version} - DO NOT EDIT`,
true
);
}
const runtimeTemplateIdentifier = program.scope.generateUidIdentifier("t");
const prependNodes = [
import_compiler.types.importDeclaration(
[import_compiler.types.importSpecifier(runtimeTemplateIdentifier, import_compiler.types.identifier("t"))],
import_compiler.types.stringLiteral(
`marko/${markoOpts.optimize ? "dist" : "src"}/runtime/${isHTML ? "html" : "vdom"}/${markoOpts.hot ? "hot-reload.js" : "index.js"}`
)
),
import_compiler.types.variableDeclaration("const", [
import_compiler.types.variableDeclarator(
componentTypeIdentifier,
import_compiler.types.stringLiteral(meta.id)
),
import_compiler.types.variableDeclarator(
templateIdentifier,
import_compiler.types.callExpression(runtimeTemplateIdentifier, [
componentTypeIdentifier
])
)
])
];
if (includeMetaInSource) {
prependNodes.push(
import_compiler.types.expressionStatement(
import_compiler.types.assignmentExpression(
"=",
import_compiler.types.memberExpression(templateIdentifier, import_compiler.types.identifier("path")),
import_compiler.types.identifier("__filename")
)
)
);
}
prependNodes.push(import_compiler.types.exportDefaultDeclaration(templateIdentifier));
program.unshiftContainer("body", prependNodes);
const templateRenderOptionsProps = [];
if (!isDynamic) {
templateRenderOptionsProps.push(
import_compiler.types.objectProperty(import_compiler.types.identifier("t"), componentTypeIdentifier)
);
}
if (isImplicit || isDynamic) {
templateRenderOptionsProps.push(
import_compiler.types.objectProperty(import_compiler.types.identifier("i"), import_compiler.types.booleanLiteral(true))
);
}
if (isSplit) {
templateRenderOptionsProps.push(
import_compiler.types.objectProperty(import_compiler.types.identifier("s"), import_compiler.types.booleanLiteral(true))
);
}
if (!markoOpts.optimize) {
templateRenderOptionsProps.push(
import_compiler.types.objectProperty(import_compiler.types.identifier("d"), import_compiler.types.booleanLiteral(true))
);
}
const componentDefIdentifier = file._componentDefIdentifier;
const componentInstanceIdentifier = file._componentInstanceIdentifier;
program.pushContainer(
"body",
import_compiler.types.expressionStatement(
import_compiler.types.assignmentExpression(
"=",
templateRendererMember,
import_compiler.types.callExpression(rendererIdentifier, [
import_compiler.types.functionExpression(
null,
[
import_compiler.types.identifier("input"),
import_compiler.types.identifier("out"),
componentDefIdentifier,
componentInstanceIdentifier,
import_compiler.types.identifier("state"),
import_compiler.types.identifier("$global")
],
renderBlock.node
),
import_compiler.types.objectExpression(templateRenderOptionsProps)
])
)
)
);
renderBlock.remove();
if (includeMetaInSource) {
const metaObject = import_compiler.types.objectExpression([
import_compiler.types.objectProperty(import_compiler.types.identifier("legacy"), import_compiler.types.booleanLiteral(true)),
import_compiler.types.objectProperty(import_compiler.types.identifier("id"), componentTypeIdentifier)
]);
if (isImplicit) {
metaObject.properties.push(
import_compiler.types.objectProperty(
import_compiler.types.identifier("component"),
import_compiler.types.stringLiteral(import_path.default.basename(templateFileName))
)
);
} else if (!isDynamic) {
metaObject.properties.push(
import_compiler.types.objectProperty(
import_compiler.types.identifier("component"),
import_compiler.types.stringLiteral(widgetBind)
)
);
}
if (deps.length) {
metaObject.properties.push(
import_compiler.types.objectProperty(
import_compiler.types.identifier("deps"),
(0, import_babel_utils.parseExpression)(file, JSON.stringify(deps))
)
);
}
if ((_a = meta.tags) == null ? void 0 : _a.length) {
metaObject.properties.push(
import_compiler.types.objectProperty(
import_compiler.types.identifier("tags"),
import_compiler.types.arrayExpression(meta.tags.map((tag) => import_compiler.types.stringLiteral(tag)))
)
);
}
program.pushContainer(
"body",
import_compiler.types.expressionStatement(
import_compiler.types.assignmentExpression("=", templateMetaMember, metaObject)
)
);
}
if (isCJS) {
program.pushContainer(
"body",
import_compiler.types.expressionStatement(
import_compiler.types.assignmentExpression(
"=",
import_compiler.types.memberExpression(
import_compiler.types.identifier("module"),
import_compiler.types.identifier("exports")
),
import_compiler.types.callExpression(
import_compiler.types.memberExpression(
import_compiler.types.identifier("Object"),
import_compiler.types.identifier("assign")
),
[
import_compiler.types.memberExpression(
import_compiler.types.identifier("exports"),
import_compiler.types.identifier("default")
),
import_compiler.types.identifier("exports")
]
)
)
)
);
}
(0, import_optmize_html_writes.optimizeHTMLWrites)(program);
}
}
};