@teleporthq/teleport-component-generator
Advanced tools
Comparing version
@@ -1,3 +0,3 @@ | ||
import { Node } from '@babel/types'; | ||
import * as types from '@babel/types'; | ||
import { CodeGeneratorFunction } from '@teleporthq/teleport-types'; | ||
export declare const generator: CodeGeneratorFunction<Node>; | ||
export declare const generator: CodeGeneratorFunction<types.Node>; |
import { ComponentGenerator, GeneratorFactoryParams } from '@teleporthq/teleport-types'; | ||
declare const createComponentGenerator: ({ mappings, plugins, postprocessors, }?: Omit<GeneratorFactoryParams, 'variation'>) => ComponentGenerator; | ||
declare const createComponentGenerator: ({ mappings, plugins, postprocessors, }?: Pick<GeneratorFactoryParams, "mappings" | "plugins" | "postprocessors">) => ComponentGenerator; | ||
export { createComponentGenerator }; |
@@ -1,280 +0,162 @@ | ||
var __create = Object.create; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __getProtoOf = Object.getPrototypeOf; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __spreadValues = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
if (__getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(b)) { | ||
if (__propIsEnum.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
return a; | ||
}; | ||
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true }); | ||
var __export = (target, all) => { | ||
__markAsModule(target); | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
var __reExport = (target, module2, desc) => { | ||
if (module2 && typeof module2 === "object" || typeof module2 === "function") { | ||
for (let key of __getOwnPropNames(module2)) | ||
if (!__hasOwnProp.call(target, key) && key !== "default") | ||
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable }); | ||
} | ||
return target; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
var __toModule = (module2) => { | ||
return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2); | ||
}; | ||
var __async = (__this, __arguments, generator3) => { | ||
return new Promise((resolve, reject) => { | ||
var fulfilled = (value) => { | ||
try { | ||
step(generator3.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var teleport_uidl_validator_1 = require("@teleporthq/teleport-uidl-validator"); | ||
var teleport_uidl_resolver_1 = require("@teleporthq/teleport-uidl-resolver"); | ||
var teleport_shared_1 = require("@teleporthq/teleport-shared"); | ||
var teleport_types_1 = require("@teleporthq/teleport-types"); | ||
var assembly_line_1 = __importDefault(require("./assembly-line")); | ||
var builder_1 = __importDefault(require("./builder")); | ||
var createComponentGenerator = function (_a) { | ||
var _b = _a === void 0 ? {} : _a, _c = _b.mappings, mappings = _c === void 0 ? [] : _c, _d = _b.plugins, plugins = _d === void 0 ? [] : _d, _e = _b.postprocessors, postprocessors = _e === void 0 ? [] : _e; | ||
var validator = new teleport_uidl_validator_1.Validator(); | ||
var resolver = new teleport_uidl_resolver_1.Resolver(__spreadArrays([teleport_uidl_resolver_1.HTMLMapping], mappings)); | ||
var assemblyLine = new assembly_line_1.default(plugins); | ||
var chunksLinker = new builder_1.default(); | ||
var processors = postprocessors; | ||
var generateComponent = function (input, options) { | ||
if (options === void 0) { options = {}; } | ||
return __awaiter(void 0, void 0, void 0, function () { | ||
var cleanedUIDL, schemaValidator, schemaValidationResult, componentUIDL, valid, uidl, contentValidationResult, resolvedUIDL, _a, chunks, externalDependencies, codeChunks, fileName, styleFileName, templateFileName, files; | ||
var _b; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
cleanedUIDL = input; | ||
if (!options.skipValidation) { | ||
schemaValidator = ((_b = options) === null || _b === void 0 ? void 0 : _b.isRootComponent) ? validator.validateRootComponentSchema | ||
: validator.validateComponentSchema; | ||
schemaValidationResult = schemaValidator(input); | ||
componentUIDL = schemaValidationResult.componentUIDL, valid = schemaValidationResult.valid; | ||
if (valid && componentUIDL) { | ||
cleanedUIDL = componentUIDL; | ||
} | ||
else { | ||
throw new Error(schemaValidationResult.errorMsg); | ||
} | ||
} | ||
uidl = teleport_uidl_validator_1.Parser.parseComponentJSON(cleanedUIDL); | ||
contentValidationResult = validator.validateComponentContent(uidl); | ||
if (!contentValidationResult.valid) { | ||
throw new Error(contentValidationResult.errorMsg); | ||
} | ||
resolvedUIDL = resolver.resolveUIDL(uidl, options); | ||
if (assemblyLine.getPlugins().length <= 0) { | ||
throw new Error('No plugins found. Component generation cannot work without any plugins!'); | ||
} | ||
return [4 /*yield*/, assemblyLine.run(resolvedUIDL, options)]; | ||
case 1: | ||
_a = _c.sent(), chunks = _a.chunks, externalDependencies = _a.externalDependencies; | ||
codeChunks = {}; | ||
Object.keys(chunks).forEach(function (fileType) { | ||
codeChunks[fileType] = chunksLinker.link(chunks[fileType]); | ||
}); | ||
processors.forEach(function (processor) { | ||
codeChunks = processor(codeChunks); | ||
}); | ||
fileName = teleport_shared_1.UIDLUtils.getComponentFileName(resolvedUIDL); | ||
styleFileName = teleport_shared_1.UIDLUtils.getStyleFileName(resolvedUIDL); | ||
templateFileName = teleport_shared_1.UIDLUtils.getTemplateFileName(resolvedUIDL); | ||
files = fileBundler(codeChunks, fileName, styleFileName, templateFileName); | ||
return [2 /*return*/, { | ||
files: files, | ||
dependencies: externalDependencies, | ||
}]; | ||
} | ||
}); | ||
}); | ||
}; | ||
var rejected = (value) => { | ||
try { | ||
step(generator3.throw(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
/** | ||
* Builds each individual chunk and applies the post processors | ||
* @param chunks All the raw chunks (ASTs, HASTs or JSS/strings) | ||
* @param fileName The name of the output file | ||
*/ | ||
var linkCodeChunks = function (chunks, fileName) { | ||
var codeChunks = {}; | ||
Object.keys(chunks).forEach(function (fileType) { | ||
codeChunks[fileType] = chunksLinker.link(chunks[fileType]); | ||
}); | ||
processors.forEach(function (processor) { | ||
codeChunks = processor(codeChunks); | ||
}); | ||
return fileBundler(codeChunks, fileName); | ||
}; | ||
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); | ||
step((generator3 = generator3.apply(__this, __arguments)).next()); | ||
}); | ||
var addPostProcessor = function (fn) { | ||
processors.push(fn); | ||
}; | ||
return { | ||
generateComponent: generateComponent, | ||
linkCodeChunks: linkCodeChunks, | ||
resolveElement: resolver.resolveElement.bind(resolver), | ||
addMapping: resolver.addMapping.bind(resolver), | ||
addPlugin: assemblyLine.addPlugin.bind(assemblyLine), | ||
addPostProcessor: addPostProcessor, | ||
}; | ||
}; | ||
// packages/teleport-component-generator/src/index.ts | ||
__export(exports, { | ||
createComponentGenerator: () => createComponentGenerator | ||
}); | ||
var import_teleport_uidl_validator = __toModule(require("@teleporthq/teleport-uidl-validator")); | ||
var import_teleport_uidl_resolver = __toModule(require("@teleporthq/teleport-uidl-resolver")); | ||
var import_teleport_shared2 = __toModule(require("@teleporthq/teleport-shared")); | ||
var import_teleport_types2 = __toModule(require("@teleporthq/teleport-types")); | ||
// packages/teleport-component-generator/src/assembly-line/utils.ts | ||
var groupChunksByFileType = (chunks) => { | ||
return chunks.reduce((chunksByFileType, chunk) => { | ||
const fileType = chunk.fileType; | ||
if (!chunksByFileType[fileType]) { | ||
chunksByFileType[fileType] = []; | ||
} | ||
chunksByFileType[fileType].push(chunk); | ||
return chunksByFileType; | ||
}, {}); | ||
}; | ||
// packages/teleport-component-generator/src/assembly-line/index.ts | ||
var import_teleport_shared = __toModule(require("@teleporthq/teleport-shared")); | ||
var AssemblyLine = class { | ||
constructor(plugins = []) { | ||
this.plugins = plugins; | ||
} | ||
run(_0, _1) { | ||
return __async(this, arguments, function* (uidl, options, initialStructure = { | ||
uidl, | ||
options, | ||
chunks: [], | ||
dependencies: {} | ||
}) { | ||
var _a; | ||
const structure = initialStructure; | ||
const finalStructure = yield this.plugins.reduce((previousPluginOperation, plugin) => __async(this, null, function* () { | ||
const modifiedStructure = yield previousPluginOperation; | ||
return plugin(modifiedStructure); | ||
}), Promise.resolve(structure)); | ||
const externalDependencies = __spreadValues(__spreadValues({}, import_teleport_shared.UIDLUtils.extractExternalDependencies((finalStructure == null ? void 0 : finalStructure.dependencies) || {})), import_teleport_shared.UIDLUtils.extractExternalDependencies(((_a = finalStructure.uidl) == null ? void 0 : _a.peerDefinitions) || {})); | ||
const chunks = groupChunksByFileType(finalStructure.chunks); | ||
return { | ||
chunks, | ||
externalDependencies | ||
}; | ||
exports.createComponentGenerator = createComponentGenerator; | ||
var fileBundler = function (codeChunks, fileName, styleFileName, templateFileName) { | ||
return Object.keys(codeChunks).map(function (fileType) { | ||
return { | ||
name: getFileName(fileType, fileName, styleFileName, templateFileName), | ||
fileType: fileType, | ||
content: codeChunks[fileType], | ||
}; | ||
}); | ||
} | ||
getPlugins() { | ||
return this.plugins; | ||
} | ||
addPlugin(plugin) { | ||
this.plugins.push(plugin); | ||
} | ||
}; | ||
// packages/teleport-component-generator/src/builder/generators/js-ast-to-code.ts | ||
var import_generator = __toModule(require("@babel/generator")); | ||
var generator = (ast) => { | ||
return (0, import_generator.default)(ast, { jsescOption: { minimal: true } }).code; | ||
}; | ||
// packages/teleport-component-generator/src/builder/generators/html-to-string.ts | ||
var import_prettyhtml_hast_to_html = __toModule(require("@starptech/prettyhtml-hast-to-html")); | ||
var generator2 = (htmlObject) => { | ||
return (0, import_prettyhtml_hast_to_html.default)(htmlObject); | ||
}; | ||
// packages/teleport-component-generator/src/builder/index.ts | ||
var import_teleport_types = __toModule(require("@teleporthq/teleport-types")); | ||
var Builder = class { | ||
constructor(chunkDefinitions = []) { | ||
this.chunkDefinitions = []; | ||
this.generators = { | ||
[import_teleport_types.ChunkType.AST]: generator, | ||
[import_teleport_types.ChunkType.HAST]: generator2, | ||
[import_teleport_types.ChunkType.STRING]: (str) => str | ||
}; | ||
this.chunkDefinitions = chunkDefinitions; | ||
} | ||
link(chunkDefinitions = []) { | ||
const chunks = chunkDefinitions || this.chunkDefinitions; | ||
if (chunks.length <= 0) { | ||
return ""; | ||
// Based on the file type we selected the file name associated. | ||
// This is mostly used by project generators when a component is exported in its own folder | ||
var getFileName = function (fileType, fileName, styleFileName, templateFileName) { | ||
if (fileType === teleport_types_1.FileType.CSS) { | ||
return styleFileName || fileName; | ||
} | ||
const unprocessedChunks = chunks.map((chunk) => { | ||
return { | ||
name: chunk.name, | ||
type: chunk.type, | ||
content: chunk.content, | ||
linkAfter: this.cleanupInvalidChunks(chunk.linkAfter, chunks) | ||
}; | ||
}); | ||
const resultingString = []; | ||
while (unprocessedChunks.length > 0) { | ||
let indexToRemove = 0; | ||
for (let index = 0; index < unprocessedChunks.length; index++) { | ||
if (unprocessedChunks[index].linkAfter.length <= 0) { | ||
indexToRemove = index; | ||
break; | ||
} | ||
} | ||
if (unprocessedChunks[indexToRemove].linkAfter.length > 0) { | ||
console.info("Operation aborted. Reason: cyclic dependency between chunks."); | ||
return ""; | ||
} | ||
const { type, content, name } = unprocessedChunks[indexToRemove]; | ||
const compiledContent = this.generateByType(type, content); | ||
if (compiledContent) { | ||
resultingString.push(compiledContent + "\n"); | ||
} | ||
unprocessedChunks.splice(indexToRemove, 1); | ||
unprocessedChunks.forEach((ch) => ch.linkAfter = ch.linkAfter.filter((after) => after !== name)); | ||
if (fileType === teleport_types_1.FileType.HTML) { | ||
return templateFileName || fileName; | ||
} | ||
return resultingString.join("\n"); | ||
} | ||
generateByType(type, content) { | ||
if (!content) { | ||
return ""; | ||
} | ||
if (Array.isArray(content)) { | ||
return content.map((contentItem) => this.generateByType(type, contentItem)).join("\n"); | ||
} | ||
if (!this.generators[type]) { | ||
throw new Error(`Attempted to generate unknown type ${type}. Please register a generator for this type in builder/index.ts`); | ||
} | ||
return this.generators[type](content); | ||
} | ||
cleanupInvalidChunks(linkAfter, chunks) { | ||
return linkAfter.filter((chunkName) => chunks.some((chunk) => chunk.name === chunkName)); | ||
} | ||
return fileName; | ||
}; | ||
// packages/teleport-component-generator/src/index.ts | ||
var createComponentGenerator = ({ | ||
mappings = [], | ||
plugins = [], | ||
postprocessors = [] | ||
} = {}) => { | ||
const validator = new import_teleport_uidl_validator.Validator(); | ||
const resolver = new import_teleport_uidl_resolver.Resolver([import_teleport_uidl_resolver.HTMLMapping, ...mappings]); | ||
const assemblyLine = new AssemblyLine(plugins); | ||
const chunksLinker = new Builder(); | ||
const processors = postprocessors; | ||
const generateComponent = (_0, ..._1) => __async(void 0, [_0, ..._1], function* (input, options = {}) { | ||
let cleanedUIDL = input; | ||
if (!options.skipValidation) { | ||
const schemaValidator = (options == null ? void 0 : options.isRootComponent) ? validator.validateRootComponentSchema : validator.validateComponentSchema; | ||
const schemaValidationResult = schemaValidator(input); | ||
const { componentUIDL, valid } = schemaValidationResult; | ||
if (valid && componentUIDL) { | ||
cleanedUIDL = componentUIDL; | ||
} else { | ||
throw new Error(schemaValidationResult.errorMsg); | ||
} | ||
} | ||
const uidl = import_teleport_uidl_validator.Parser.parseComponentJSON(cleanedUIDL); | ||
const contentValidationResult = validator.validateComponentContent(uidl); | ||
if (!contentValidationResult.valid) { | ||
throw new Error(contentValidationResult.errorMsg); | ||
} | ||
const resolvedUIDL = resolver.resolveUIDL(uidl, options); | ||
if (assemblyLine.getPlugins().length <= 0) { | ||
throw new Error("No plugins found. Component generation cannot work without any plugins!"); | ||
} | ||
const { chunks, externalDependencies } = yield assemblyLine.run(resolvedUIDL, options); | ||
let codeChunks = {}; | ||
Object.keys(chunks).forEach((fileType) => { | ||
codeChunks[fileType] = chunksLinker.link(chunks[fileType]); | ||
}); | ||
processors.forEach((processor) => { | ||
codeChunks = processor(codeChunks); | ||
}); | ||
const fileName = import_teleport_shared2.UIDLUtils.getComponentFileName(resolvedUIDL); | ||
const styleFileName = import_teleport_shared2.UIDLUtils.getStyleFileName(resolvedUIDL); | ||
const templateFileName = import_teleport_shared2.UIDLUtils.getTemplateFileName(resolvedUIDL); | ||
const files = fileBundler(codeChunks, fileName, styleFileName, templateFileName); | ||
return { | ||
files, | ||
dependencies: externalDependencies | ||
}; | ||
}); | ||
const linkCodeChunks = (chunks, fileName) => { | ||
let codeChunks = {}; | ||
Object.keys(chunks).forEach((fileType) => { | ||
codeChunks[fileType] = chunksLinker.link(chunks[fileType]); | ||
}); | ||
processors.forEach((processor) => { | ||
codeChunks = processor(codeChunks); | ||
}); | ||
return fileBundler(codeChunks, fileName); | ||
}; | ||
const addPostProcessor = (fn) => { | ||
processors.push(fn); | ||
}; | ||
return { | ||
generateComponent, | ||
linkCodeChunks, | ||
resolveElement: resolver.resolveElement.bind(resolver), | ||
addMapping: resolver.addMapping.bind(resolver), | ||
addPlugin: assemblyLine.addPlugin.bind(assemblyLine), | ||
addPostProcessor | ||
}; | ||
}; | ||
var fileBundler = (codeChunks, fileName, styleFileName, templateFileName) => { | ||
return Object.keys(codeChunks).map((fileType) => { | ||
return { | ||
name: getFileName(fileType, fileName, styleFileName, templateFileName), | ||
fileType, | ||
content: codeChunks[fileType] | ||
}; | ||
}); | ||
}; | ||
var getFileName = (fileType, fileName, styleFileName, templateFileName) => { | ||
if (fileType === import_teleport_types2.FileType.CSS) { | ||
return styleFileName || fileName; | ||
} | ||
if (fileType === import_teleport_types2.FileType.HTML) { | ||
return templateFileName || fileName; | ||
} | ||
return fileName; | ||
}; | ||
//# sourceMappingURL=index.js.map |
@@ -1,263 +0,157 @@ | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __spreadValues = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
if (__getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(b)) { | ||
if (__propIsEnum.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
} | ||
return a; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __async = (__this, __arguments, generator3) => { | ||
return new Promise((resolve, reject) => { | ||
var fulfilled = (value) => { | ||
try { | ||
step(generator3.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var rejected = (value) => { | ||
try { | ||
step(generator3.throw(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); | ||
step((generator3 = generator3.apply(__this, __arguments)).next()); | ||
}); | ||
}; | ||
// packages/teleport-component-generator/src/index.ts | ||
import { Validator, Parser } from "@teleporthq/teleport-uidl-validator"; | ||
import { Resolver, HTMLMapping } from "@teleporthq/teleport-uidl-resolver"; | ||
import { UIDLUtils as UIDLUtils2 } from "@teleporthq/teleport-shared"; | ||
import { | ||
FileType | ||
} from "@teleporthq/teleport-types"; | ||
// packages/teleport-component-generator/src/assembly-line/utils.ts | ||
var groupChunksByFileType = (chunks) => { | ||
return chunks.reduce((chunksByFileType, chunk) => { | ||
const fileType = chunk.fileType; | ||
if (!chunksByFileType[fileType]) { | ||
chunksByFileType[fileType] = []; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
chunksByFileType[fileType].push(chunk); | ||
return chunksByFileType; | ||
}, {}); | ||
}; | ||
// packages/teleport-component-generator/src/assembly-line/index.ts | ||
import { UIDLUtils } from "@teleporthq/teleport-shared"; | ||
var AssemblyLine = class { | ||
constructor(plugins = []) { | ||
this.plugins = plugins; | ||
} | ||
run(_0, _1) { | ||
return __async(this, arguments, function* (uidl, options, initialStructure = { | ||
uidl, | ||
options, | ||
chunks: [], | ||
dependencies: {} | ||
}) { | ||
var _a; | ||
const structure = initialStructure; | ||
const finalStructure = yield this.plugins.reduce((previousPluginOperation, plugin) => __async(this, null, function* () { | ||
const modifiedStructure = yield previousPluginOperation; | ||
return plugin(modifiedStructure); | ||
}), Promise.resolve(structure)); | ||
const externalDependencies = __spreadValues(__spreadValues({}, UIDLUtils.extractExternalDependencies((finalStructure == null ? void 0 : finalStructure.dependencies) || {})), UIDLUtils.extractExternalDependencies(((_a = finalStructure.uidl) == null ? void 0 : _a.peerDefinitions) || {})); | ||
const chunks = groupChunksByFileType(finalStructure.chunks); | ||
return { | ||
chunks, | ||
externalDependencies | ||
}; | ||
}); | ||
} | ||
getPlugins() { | ||
return this.plugins; | ||
} | ||
addPlugin(plugin) { | ||
this.plugins.push(plugin); | ||
} | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
// packages/teleport-component-generator/src/builder/generators/js-ast-to-code.ts | ||
import babelGenerator from "@babel/generator"; | ||
var generator = (ast) => { | ||
return babelGenerator(ast, { jsescOption: { minimal: true } }).code; | ||
}; | ||
// packages/teleport-component-generator/src/builder/generators/html-to-string.ts | ||
import toHTML from "@starptech/prettyhtml-hast-to-html"; | ||
var generator2 = (htmlObject) => { | ||
return toHTML(htmlObject); | ||
}; | ||
// packages/teleport-component-generator/src/builder/index.ts | ||
import { | ||
ChunkType | ||
} from "@teleporthq/teleport-types"; | ||
var Builder = class { | ||
constructor(chunkDefinitions = []) { | ||
this.chunkDefinitions = []; | ||
this.generators = { | ||
[ChunkType.AST]: generator, | ||
[ChunkType.HAST]: generator2, | ||
[ChunkType.STRING]: (str) => str | ||
import { Validator, Parser } from '@teleporthq/teleport-uidl-validator'; | ||
import { Resolver, HTMLMapping } from '@teleporthq/teleport-uidl-resolver'; | ||
import { UIDLUtils } from '@teleporthq/teleport-shared'; | ||
import { FileType, } from '@teleporthq/teleport-types'; | ||
import AssemblyLine from './assembly-line'; | ||
import Builder from './builder'; | ||
var createComponentGenerator = function (_a) { | ||
var _b = _a === void 0 ? {} : _a, _c = _b.mappings, mappings = _c === void 0 ? [] : _c, _d = _b.plugins, plugins = _d === void 0 ? [] : _d, _e = _b.postprocessors, postprocessors = _e === void 0 ? [] : _e; | ||
var validator = new Validator(); | ||
var resolver = new Resolver(__spreadArrays([HTMLMapping], mappings)); | ||
var assemblyLine = new AssemblyLine(plugins); | ||
var chunksLinker = new Builder(); | ||
var processors = postprocessors; | ||
var generateComponent = function (input, options) { | ||
if (options === void 0) { options = {}; } | ||
return __awaiter(void 0, void 0, void 0, function () { | ||
var cleanedUIDL, schemaValidator, schemaValidationResult, componentUIDL, valid, uidl, contentValidationResult, resolvedUIDL, _a, chunks, externalDependencies, codeChunks, fileName, styleFileName, templateFileName, files; | ||
var _b; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
cleanedUIDL = input; | ||
if (!options.skipValidation) { | ||
schemaValidator = ((_b = options) === null || _b === void 0 ? void 0 : _b.isRootComponent) ? validator.validateRootComponentSchema | ||
: validator.validateComponentSchema; | ||
schemaValidationResult = schemaValidator(input); | ||
componentUIDL = schemaValidationResult.componentUIDL, valid = schemaValidationResult.valid; | ||
if (valid && componentUIDL) { | ||
cleanedUIDL = componentUIDL; | ||
} | ||
else { | ||
throw new Error(schemaValidationResult.errorMsg); | ||
} | ||
} | ||
uidl = Parser.parseComponentJSON(cleanedUIDL); | ||
contentValidationResult = validator.validateComponentContent(uidl); | ||
if (!contentValidationResult.valid) { | ||
throw new Error(contentValidationResult.errorMsg); | ||
} | ||
resolvedUIDL = resolver.resolveUIDL(uidl, options); | ||
if (assemblyLine.getPlugins().length <= 0) { | ||
throw new Error('No plugins found. Component generation cannot work without any plugins!'); | ||
} | ||
return [4 /*yield*/, assemblyLine.run(resolvedUIDL, options)]; | ||
case 1: | ||
_a = _c.sent(), chunks = _a.chunks, externalDependencies = _a.externalDependencies; | ||
codeChunks = {}; | ||
Object.keys(chunks).forEach(function (fileType) { | ||
codeChunks[fileType] = chunksLinker.link(chunks[fileType]); | ||
}); | ||
processors.forEach(function (processor) { | ||
codeChunks = processor(codeChunks); | ||
}); | ||
fileName = UIDLUtils.getComponentFileName(resolvedUIDL); | ||
styleFileName = UIDLUtils.getStyleFileName(resolvedUIDL); | ||
templateFileName = UIDLUtils.getTemplateFileName(resolvedUIDL); | ||
files = fileBundler(codeChunks, fileName, styleFileName, templateFileName); | ||
return [2 /*return*/, { | ||
files: files, | ||
dependencies: externalDependencies, | ||
}]; | ||
} | ||
}); | ||
}); | ||
}; | ||
this.chunkDefinitions = chunkDefinitions; | ||
} | ||
link(chunkDefinitions = []) { | ||
const chunks = chunkDefinitions || this.chunkDefinitions; | ||
if (chunks.length <= 0) { | ||
return ""; | ||
} | ||
const unprocessedChunks = chunks.map((chunk) => { | ||
return { | ||
name: chunk.name, | ||
type: chunk.type, | ||
content: chunk.content, | ||
linkAfter: this.cleanupInvalidChunks(chunk.linkAfter, chunks) | ||
}; | ||
}); | ||
const resultingString = []; | ||
while (unprocessedChunks.length > 0) { | ||
let indexToRemove = 0; | ||
for (let index = 0; index < unprocessedChunks.length; index++) { | ||
if (unprocessedChunks[index].linkAfter.length <= 0) { | ||
indexToRemove = index; | ||
break; | ||
} | ||
} | ||
if (unprocessedChunks[indexToRemove].linkAfter.length > 0) { | ||
console.info("Operation aborted. Reason: cyclic dependency between chunks."); | ||
return ""; | ||
} | ||
const { type, content, name } = unprocessedChunks[indexToRemove]; | ||
const compiledContent = this.generateByType(type, content); | ||
if (compiledContent) { | ||
resultingString.push(compiledContent + "\n"); | ||
} | ||
unprocessedChunks.splice(indexToRemove, 1); | ||
unprocessedChunks.forEach((ch) => ch.linkAfter = ch.linkAfter.filter((after) => after !== name)); | ||
} | ||
return resultingString.join("\n"); | ||
} | ||
generateByType(type, content) { | ||
if (!content) { | ||
return ""; | ||
} | ||
if (Array.isArray(content)) { | ||
return content.map((contentItem) => this.generateByType(type, contentItem)).join("\n"); | ||
} | ||
if (!this.generators[type]) { | ||
throw new Error(`Attempted to generate unknown type ${type}. Please register a generator for this type in builder/index.ts`); | ||
} | ||
return this.generators[type](content); | ||
} | ||
cleanupInvalidChunks(linkAfter, chunks) { | ||
return linkAfter.filter((chunkName) => chunks.some((chunk) => chunk.name === chunkName)); | ||
} | ||
}; | ||
// packages/teleport-component-generator/src/index.ts | ||
var createComponentGenerator = ({ | ||
mappings = [], | ||
plugins = [], | ||
postprocessors = [] | ||
} = {}) => { | ||
const validator = new Validator(); | ||
const resolver = new Resolver([HTMLMapping, ...mappings]); | ||
const assemblyLine = new AssemblyLine(plugins); | ||
const chunksLinker = new Builder(); | ||
const processors = postprocessors; | ||
const generateComponent = (_0, ..._1) => __async(void 0, [_0, ..._1], function* (input, options = {}) { | ||
let cleanedUIDL = input; | ||
if (!options.skipValidation) { | ||
const schemaValidator = (options == null ? void 0 : options.isRootComponent) ? validator.validateRootComponentSchema : validator.validateComponentSchema; | ||
const schemaValidationResult = schemaValidator(input); | ||
const { componentUIDL, valid } = schemaValidationResult; | ||
if (valid && componentUIDL) { | ||
cleanedUIDL = componentUIDL; | ||
} else { | ||
throw new Error(schemaValidationResult.errorMsg); | ||
} | ||
} | ||
const uidl = Parser.parseComponentJSON(cleanedUIDL); | ||
const contentValidationResult = validator.validateComponentContent(uidl); | ||
if (!contentValidationResult.valid) { | ||
throw new Error(contentValidationResult.errorMsg); | ||
} | ||
const resolvedUIDL = resolver.resolveUIDL(uidl, options); | ||
if (assemblyLine.getPlugins().length <= 0) { | ||
throw new Error("No plugins found. Component generation cannot work without any plugins!"); | ||
} | ||
const { chunks, externalDependencies } = yield assemblyLine.run(resolvedUIDL, options); | ||
let codeChunks = {}; | ||
Object.keys(chunks).forEach((fileType) => { | ||
codeChunks[fileType] = chunksLinker.link(chunks[fileType]); | ||
}); | ||
processors.forEach((processor) => { | ||
codeChunks = processor(codeChunks); | ||
}); | ||
const fileName = UIDLUtils2.getComponentFileName(resolvedUIDL); | ||
const styleFileName = UIDLUtils2.getStyleFileName(resolvedUIDL); | ||
const templateFileName = UIDLUtils2.getTemplateFileName(resolvedUIDL); | ||
const files = fileBundler(codeChunks, fileName, styleFileName, templateFileName); | ||
return { | ||
files, | ||
dependencies: externalDependencies | ||
/** | ||
* Builds each individual chunk and applies the post processors | ||
* @param chunks All the raw chunks (ASTs, HASTs or JSS/strings) | ||
* @param fileName The name of the output file | ||
*/ | ||
var linkCodeChunks = function (chunks, fileName) { | ||
var codeChunks = {}; | ||
Object.keys(chunks).forEach(function (fileType) { | ||
codeChunks[fileType] = chunksLinker.link(chunks[fileType]); | ||
}); | ||
processors.forEach(function (processor) { | ||
codeChunks = processor(codeChunks); | ||
}); | ||
return fileBundler(codeChunks, fileName); | ||
}; | ||
}); | ||
const linkCodeChunks = (chunks, fileName) => { | ||
let codeChunks = {}; | ||
Object.keys(chunks).forEach((fileType) => { | ||
codeChunks[fileType] = chunksLinker.link(chunks[fileType]); | ||
}); | ||
processors.forEach((processor) => { | ||
codeChunks = processor(codeChunks); | ||
}); | ||
return fileBundler(codeChunks, fileName); | ||
}; | ||
const addPostProcessor = (fn) => { | ||
processors.push(fn); | ||
}; | ||
return { | ||
generateComponent, | ||
linkCodeChunks, | ||
resolveElement: resolver.resolveElement.bind(resolver), | ||
addMapping: resolver.addMapping.bind(resolver), | ||
addPlugin: assemblyLine.addPlugin.bind(assemblyLine), | ||
addPostProcessor | ||
}; | ||
}; | ||
var fileBundler = (codeChunks, fileName, styleFileName, templateFileName) => { | ||
return Object.keys(codeChunks).map((fileType) => { | ||
var addPostProcessor = function (fn) { | ||
processors.push(fn); | ||
}; | ||
return { | ||
name: getFileName(fileType, fileName, styleFileName, templateFileName), | ||
fileType, | ||
content: codeChunks[fileType] | ||
generateComponent: generateComponent, | ||
linkCodeChunks: linkCodeChunks, | ||
resolveElement: resolver.resolveElement.bind(resolver), | ||
addMapping: resolver.addMapping.bind(resolver), | ||
addPlugin: assemblyLine.addPlugin.bind(assemblyLine), | ||
addPostProcessor: addPostProcessor, | ||
}; | ||
}); | ||
}; | ||
var getFileName = (fileType, fileName, styleFileName, templateFileName) => { | ||
if (fileType === FileType.CSS) { | ||
return styleFileName || fileName; | ||
} | ||
if (fileType === FileType.HTML) { | ||
return templateFileName || fileName; | ||
} | ||
return fileName; | ||
export { createComponentGenerator }; | ||
var fileBundler = function (codeChunks, fileName, styleFileName, templateFileName) { | ||
return Object.keys(codeChunks).map(function (fileType) { | ||
return { | ||
name: getFileName(fileType, fileName, styleFileName, templateFileName), | ||
fileType: fileType, | ||
content: codeChunks[fileType], | ||
}; | ||
}); | ||
}; | ||
export { | ||
createComponentGenerator | ||
// Based on the file type we selected the file name associated. | ||
// This is mostly used by project generators when a component is exported in its own folder | ||
var getFileName = function (fileType, fileName, styleFileName, templateFileName) { | ||
if (fileType === FileType.CSS) { | ||
return styleFileName || fileName; | ||
} | ||
if (fileType === FileType.HTML) { | ||
return templateFileName || fileName; | ||
} | ||
return fileName; | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@teleporthq/teleport-component-generator", | ||
"version": "0.17.2", | ||
"version": "0.17.3", | ||
"description": "Bare-bone component generator on top of which you can add plugins, mappings and post processing functions", | ||
@@ -9,9 +9,4 @@ "author": "teleportHQ", | ||
"main": "dist/cjs/index.js", | ||
"module": "dist/esm/index.js", | ||
"types": "dist/cjs/index.d.ts", | ||
"module": "dist/esm/index.js", | ||
"exports": { | ||
"module": "./dist/esm/index.js", | ||
"require": "./dist/cjs/index.js", | ||
"import": "./dist/esm/index.js" | ||
}, | ||
"sideEffects": false, | ||
@@ -30,3 +25,3 @@ "repository": { | ||
"clean": "rimraf dist", | ||
"types": "tsc -p tsconfig.json" | ||
"build": "tsc -p tsconfig.json && tsc -p tsconfig.json --module commonjs --outDir dist/cjs" | ||
}, | ||
@@ -37,8 +32,8 @@ "dependencies": { | ||
"@starptech/prettyhtml-hast-to-html": "^0.10.0", | ||
"@teleporthq/teleport-shared": "^0.17.0", | ||
"@teleporthq/teleport-types": "^0.17.0", | ||
"@teleporthq/teleport-uidl-resolver": "^0.17.1", | ||
"@teleporthq/teleport-uidl-validator": "^0.17.0" | ||
"@teleporthq/teleport-shared": "^0.17.3", | ||
"@teleporthq/teleport-types": "^0.17.3", | ||
"@teleporthq/teleport-uidl-resolver": "^0.17.3", | ||
"@teleporthq/teleport-uidl-validator": "^0.17.3" | ||
}, | ||
"gitHead": "3835076622e271c7a99dfcde7395d923650b428f" | ||
"gitHead": "1a673d148f2626c5488092f3e88b3a9d7c9725b4" | ||
} |
@@ -1,8 +0,9 @@ | ||
import babelGenerator from '@babel/generator' | ||
import { Node } from '@babel/types' | ||
import { CodeGenerator } from '@babel/generator' | ||
import * as types from '@babel/types' | ||
import { CodeGeneratorFunction } from '@teleporthq/teleport-types' | ||
export const generator: CodeGeneratorFunction<Node> = (ast) => { | ||
return babelGenerator(ast, { jsescOption: { minimal: true } }).code | ||
export const generator: CodeGeneratorFunction<types.Node> = (ast) => { | ||
const babelGenerator = new CodeGenerator(ast, { jsescOption: { minimal: true } }) | ||
const { code } = babelGenerator.generate() | ||
return code | ||
} |
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"emitDeclarationOnly": true, | ||
"outDir": "dist/cjs" | ||
"outDir": "dist/esm" | ||
}, | ||
@@ -7,0 +6,0 @@ "include": [ |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
223314
288.29%49
145%1186
27.25%1
Infinity%