@teleporthq/teleport-plugin-common
Advanced tools
Comparing version 0.10.0-alpha.4 to 0.10.0-alpha.5
import * as types from '@babel/types'; | ||
import { ImportIdentifier, UIDLEventHandlerStatement } from '@teleporthq/teleport-types'; | ||
import { ImportIdentifier, UIDLEventHandlerStatement, EntryFileOptions, UIDLGlobalAsset } from '@teleporthq/teleport-types'; | ||
export declare const createConstAssignment: (constName: string, asignment?: any, t?: typeof types) => types.VariableDeclaration; | ||
@@ -11,3 +11,4 @@ export declare const createDefaultExport: (exportReference: string, t?: typeof types) => types.ExportDefaultDeclaration; | ||
export declare const createGenericImportStatement: (path: string, imports: ImportIdentifier[], t?: typeof types) => types.ImportDeclaration; | ||
export declare const createJSXTag: (tagName: string, children?: (types.JSXElement | types.JSXFragment | types.JSXExpressionContainer | types.JSXText | types.JSXSpreadChild)[], selfClosing?: boolean, t?: typeof types) => types.JSXElement; | ||
declare type JSXChild = types.JSXText | types.JSXExpressionContainer | types.JSXSpreadChild | types.JSXElement | types.JSXFragment; | ||
export declare const createJSXTag: (tagName: string, children?: JSXChild[], selfClosing?: boolean, t?: typeof types) => types.JSXElement; | ||
export declare const createSelfClosingJSXTag: (tagName: string) => types.JSXElement; | ||
@@ -20,1 +21,6 @@ export declare const createJSXExpresionContainer: (expression: types.Expression, t?: typeof types) => types.JSXExpressionContainer; | ||
export declare const createStateChangeStatement: (statement: UIDLEventHandlerStatement, t?: typeof types) => types.ExpressionStatement; | ||
export declare const appendAssetsAST: (assets: UIDLGlobalAsset[], options: EntryFileOptions, headNode: types.JSXElement, bodyNode: types.JSXElement) => { | ||
headNode: types.JSXElement; | ||
bodyNode: types.JSXElement; | ||
}; | ||
export {}; |
@@ -12,2 +12,3 @@ "use strict"; | ||
var ast_utils_1 = require("../utils/ast-utils"); | ||
var teleport_shared_1 = require("@teleporthq/teleport-shared"); | ||
exports.createConstAssignment = function (constName, asignment, t) { | ||
@@ -95,3 +96,3 @@ if (asignment === void 0) { asignment = null; } | ||
// TODO: extensive list | ||
return obj.type === 'JSXElement' || obj.type === 'CallExpression'; | ||
return obj.type === 'JSXElement' || obj.type === 'CallExpression' || obj.type === 'Identifier'; | ||
}; | ||
@@ -123,2 +124,66 @@ // equivalent to (props) => props.title | ||
}; | ||
exports.appendAssetsAST = function (assets, options, headNode, bodyNode) { | ||
assets.forEach(function (asset) { | ||
var assetPath = teleport_shared_1.UIDLUtils.prefixAssetsPath(options.assetsPrefix, asset.path); | ||
// link canonical for SEO | ||
if (asset.type === 'canonical' && assetPath) { | ||
var linkTag = exports.createJSXTag('link'); | ||
ast_utils_1.addAttributeToJSXTag(linkTag, 'rel', 'canonical'); | ||
ast_utils_1.addAttributeToJSXTag(linkTag, 'href', assetPath); | ||
ast_utils_1.addChildJSXTag(headNode, linkTag); | ||
} | ||
// link stylesheet (external css, font) | ||
if ((asset.type === 'style' || asset.type === 'font') && assetPath) { | ||
var linkTag = exports.createJSXTag('link'); | ||
ast_utils_1.addAttributeToJSXTag(linkTag, 'rel', 'stylesheet'); | ||
ast_utils_1.addAttributeToJSXTag(linkTag, 'href', assetPath); | ||
ast_utils_1.addChildJSXTag(headNode, linkTag); | ||
} | ||
// inline style | ||
if (asset.type === 'style' && asset.content) { | ||
var styleTag = exports.createJSXTag('style'); | ||
ast_utils_1.addAttributeToJSXTag(styleTag, 'dangerouslySetInnerHTML', { __html: asset.content }); | ||
ast_utils_1.addChildJSXTag(headNode, styleTag); | ||
} | ||
// script (external or inline) | ||
if (asset.type === 'script') { | ||
var scriptTag = exports.createJSXTag('script'); | ||
ast_utils_1.addAttributeToJSXTag(scriptTag, 'type', 'text/javascript'); | ||
if (assetPath) { | ||
ast_utils_1.addAttributeToJSXTag(scriptTag, 'src', assetPath); | ||
if (asset.options && asset.options.defer) { | ||
ast_utils_1.addAttributeToJSXTag(scriptTag, 'defer', true); | ||
} | ||
if (asset.options && asset.options.async) { | ||
ast_utils_1.addAttributeToJSXTag(scriptTag, 'async', true); | ||
} | ||
} | ||
else if (asset.content) { | ||
ast_utils_1.addAttributeToJSXTag(scriptTag, 'dangerouslySetInnerHTML', { | ||
__html: asset.content, | ||
}); | ||
} | ||
if (asset.options && asset.options.target === 'body') { | ||
ast_utils_1.addChildJSXTag(bodyNode, scriptTag); | ||
} | ||
else { | ||
ast_utils_1.addChildJSXTag(headNode, scriptTag); | ||
} | ||
} | ||
// icon | ||
if (asset.type === 'icon' && assetPath) { | ||
var iconTag = exports.createJSXTag('link'); | ||
ast_utils_1.addAttributeToJSXTag(iconTag, 'rel', 'shortcut icon'); | ||
ast_utils_1.addAttributeToJSXTag(iconTag, 'href', assetPath); | ||
if (asset.options && asset.options.iconType) { | ||
ast_utils_1.addAttributeToJSXTag(iconTag, 'type', asset.options.iconType); | ||
} | ||
if (asset.options && asset.options.iconSizes) { | ||
ast_utils_1.addAttributeToJSXTag(iconTag, 'sizes', asset.options.iconSizes); | ||
} | ||
ast_utils_1.addChildJSXTag(headNode, iconTag); | ||
} | ||
}); | ||
return { headNode: headNode, bodyNode: bodyNode }; | ||
}; | ||
//# sourceMappingURL=ast-builders.js.map |
@@ -23,2 +23,4 @@ import * as types from '@babel/types'; | ||
export declare const getTSAnnotationForType: (type: any, t?: typeof types) => types.TSStringKeyword | types.TSNumberKeyword | types.TSBooleanKeyword | types.TSUnknownKeyword; | ||
export declare const findAttributeByName: (jsxTag: types.JSXElement, attrName: string) => types.JSXAttribute; | ||
export declare const removeAttributeByName: (jsxTag: types.JSXElement, attrName: string) => void; | ||
export {}; |
@@ -196,2 +196,11 @@ "use strict"; | ||
}; | ||
exports.findAttributeByName = function (jsxTag, attrName) { | ||
return jsxTag.openingElement.attributes.find(function (attr) { return attr.type === 'JSXAttribute' && attr.name.name === attrName; }); | ||
}; | ||
exports.removeAttributeByName = function (jsxTag, attrName) { | ||
jsxTag.openingElement.attributes = jsxTag.openingElement.attributes.filter(function (attr) { | ||
return attr.type === 'JSXSpreadAttribute' || | ||
(attr.type === 'JSXAttribute' && attr.name.name !== attrName); | ||
}); | ||
}; | ||
//# sourceMappingURL=ast-utils.js.map |
import * as types from '@babel/types'; | ||
import { ImportIdentifier, UIDLEventHandlerStatement } from '@teleporthq/teleport-types'; | ||
import { ImportIdentifier, UIDLEventHandlerStatement, EntryFileOptions, UIDLGlobalAsset } from '@teleporthq/teleport-types'; | ||
export declare const createConstAssignment: (constName: string, asignment?: any, t?: typeof types) => types.VariableDeclaration; | ||
@@ -11,3 +11,4 @@ export declare const createDefaultExport: (exportReference: string, t?: typeof types) => types.ExportDefaultDeclaration; | ||
export declare const createGenericImportStatement: (path: string, imports: ImportIdentifier[], t?: typeof types) => types.ImportDeclaration; | ||
export declare const createJSXTag: (tagName: string, children?: (types.JSXElement | types.JSXFragment | types.JSXExpressionContainer | types.JSXText | types.JSXSpreadChild)[], selfClosing?: boolean, t?: typeof types) => types.JSXElement; | ||
declare type JSXChild = types.JSXText | types.JSXExpressionContainer | types.JSXSpreadChild | types.JSXElement | types.JSXFragment; | ||
export declare const createJSXTag: (tagName: string, children?: JSXChild[], selfClosing?: boolean, t?: typeof types) => types.JSXElement; | ||
export declare const createSelfClosingJSXTag: (tagName: string) => types.JSXElement; | ||
@@ -20,1 +21,6 @@ export declare const createJSXExpresionContainer: (expression: types.Expression, t?: typeof types) => types.JSXExpressionContainer; | ||
export declare const createStateChangeStatement: (statement: UIDLEventHandlerStatement, t?: typeof types) => types.ExpressionStatement; | ||
export declare const appendAssetsAST: (assets: UIDLGlobalAsset[], options: EntryFileOptions, headNode: types.JSXElement, bodyNode: types.JSXElement) => { | ||
headNode: types.JSXElement; | ||
bodyNode: types.JSXElement; | ||
}; | ||
export {}; |
import * as types from '@babel/types'; | ||
import { convertValueToLiteral, objectToObjectExpression } from '../utils/ast-utils'; | ||
import { convertValueToLiteral, objectToObjectExpression, addAttributeToJSXTag, addChildJSXTag, } from '../utils/ast-utils'; | ||
import { UIDLUtils } from '@teleporthq/teleport-shared'; | ||
export const createConstAssignment = (constName, asignment = null, t = types) => { | ||
@@ -71,3 +72,3 @@ const declarator = t.variableDeclarator(t.identifier(constName), asignment); | ||
// TODO: extensive list | ||
return obj.type === 'JSXElement' || obj.type === 'CallExpression'; | ||
return obj.type === 'JSXElement' || obj.type === 'CallExpression' || obj.type === 'Identifier'; | ||
}; | ||
@@ -95,2 +96,66 @@ // equivalent to (props) => props.title | ||
}; | ||
export const appendAssetsAST = (assets, options, headNode, bodyNode) => { | ||
assets.forEach((asset) => { | ||
const assetPath = UIDLUtils.prefixAssetsPath(options.assetsPrefix, asset.path); | ||
// link canonical for SEO | ||
if (asset.type === 'canonical' && assetPath) { | ||
const linkTag = createJSXTag('link'); | ||
addAttributeToJSXTag(linkTag, 'rel', 'canonical'); | ||
addAttributeToJSXTag(linkTag, 'href', assetPath); | ||
addChildJSXTag(headNode, linkTag); | ||
} | ||
// link stylesheet (external css, font) | ||
if ((asset.type === 'style' || asset.type === 'font') && assetPath) { | ||
const linkTag = createJSXTag('link'); | ||
addAttributeToJSXTag(linkTag, 'rel', 'stylesheet'); | ||
addAttributeToJSXTag(linkTag, 'href', assetPath); | ||
addChildJSXTag(headNode, linkTag); | ||
} | ||
// inline style | ||
if (asset.type === 'style' && asset.content) { | ||
const styleTag = createJSXTag('style'); | ||
addAttributeToJSXTag(styleTag, 'dangerouslySetInnerHTML', { __html: asset.content }); | ||
addChildJSXTag(headNode, styleTag); | ||
} | ||
// script (external or inline) | ||
if (asset.type === 'script') { | ||
const scriptTag = createJSXTag('script'); | ||
addAttributeToJSXTag(scriptTag, 'type', 'text/javascript'); | ||
if (assetPath) { | ||
addAttributeToJSXTag(scriptTag, 'src', assetPath); | ||
if (asset.options && asset.options.defer) { | ||
addAttributeToJSXTag(scriptTag, 'defer', true); | ||
} | ||
if (asset.options && asset.options.async) { | ||
addAttributeToJSXTag(scriptTag, 'async', true); | ||
} | ||
} | ||
else if (asset.content) { | ||
addAttributeToJSXTag(scriptTag, 'dangerouslySetInnerHTML', { | ||
__html: asset.content, | ||
}); | ||
} | ||
if (asset.options && asset.options.target === 'body') { | ||
addChildJSXTag(bodyNode, scriptTag); | ||
} | ||
else { | ||
addChildJSXTag(headNode, scriptTag); | ||
} | ||
} | ||
// icon | ||
if (asset.type === 'icon' && assetPath) { | ||
const iconTag = createJSXTag('link'); | ||
addAttributeToJSXTag(iconTag, 'rel', 'shortcut icon'); | ||
addAttributeToJSXTag(iconTag, 'href', assetPath); | ||
if (asset.options && asset.options.iconType) { | ||
addAttributeToJSXTag(iconTag, 'type', asset.options.iconType); | ||
} | ||
if (asset.options && asset.options.iconSizes) { | ||
addAttributeToJSXTag(iconTag, 'sizes', asset.options.iconSizes); | ||
} | ||
addChildJSXTag(headNode, iconTag); | ||
} | ||
}); | ||
return { headNode, bodyNode }; | ||
}; | ||
//# sourceMappingURL=ast-builders.js.map |
@@ -23,2 +23,4 @@ import * as types from '@babel/types'; | ||
export declare const getTSAnnotationForType: (type: any, t?: typeof types) => types.TSStringKeyword | types.TSNumberKeyword | types.TSBooleanKeyword | types.TSUnknownKeyword; | ||
export declare const findAttributeByName: (jsxTag: types.JSXElement, attrName: string) => types.JSXAttribute; | ||
export declare const removeAttributeByName: (jsxTag: types.JSXElement, attrName: string) => void; | ||
export {}; |
@@ -170,2 +170,9 @@ import * as types from '@babel/types'; | ||
}; | ||
export const findAttributeByName = (jsxTag, attrName) => { | ||
return jsxTag.openingElement.attributes.find((attr) => attr.type === 'JSXAttribute' && attr.name.name === attrName); | ||
}; | ||
export const removeAttributeByName = (jsxTag, attrName) => { | ||
jsxTag.openingElement.attributes = jsxTag.openingElement.attributes.filter((attr) => attr.type === 'JSXSpreadAttribute' || | ||
(attr.type === 'JSXAttribute' && attr.name.name !== attrName)); | ||
}; | ||
//# sourceMappingURL=ast-utils.js.map |
{ | ||
"name": "@teleporthq/teleport-plugin-common", | ||
"version": "0.10.0-alpha.4", | ||
"version": "0.10.0-alpha.5", | ||
"description": "Common building and modelating functions for ASTs and HASTs", | ||
@@ -30,8 +30,8 @@ "author": "teleportHQ", | ||
"@babel/types": "^7.5.5", | ||
"@teleporthq/teleport-shared": "^0.10.0-alpha.4", | ||
"@teleporthq/teleport-types": "^0.10.0-alpha.4", | ||
"@teleporthq/teleport-shared": "^0.10.0-alpha.5", | ||
"@teleporthq/teleport-types": "^0.10.0-alpha.5", | ||
"jss": "^10.0.0", | ||
"jss-preset-default": "^10.0.0" | ||
}, | ||
"gitHead": "8edece3f58b34ff7052c323471590460287fa50e" | ||
"gitHead": "86b1764602ada3cc80aeb800fd5d0261673a00c5" | ||
} |
import * as types from '@babel/types' | ||
import { convertValueToLiteral, objectToObjectExpression } from '../utils/ast-utils' | ||
import { ImportIdentifier, UIDLEventHandlerStatement } from '@teleporthq/teleport-types' | ||
import { | ||
convertValueToLiteral, | ||
objectToObjectExpression, | ||
addAttributeToJSXTag, | ||
addChildJSXTag, | ||
} from '../utils/ast-utils' | ||
import { | ||
ImportIdentifier, | ||
UIDLEventHandlerStatement, | ||
EntryFileOptions, | ||
UIDLGlobalAsset, | ||
} from '@teleporthq/teleport-types' | ||
import { UIDLUtils } from '@teleporthq/teleport-shared' | ||
@@ -117,3 +128,3 @@ export const createConstAssignment = (constName: string, asignment: any = null, t = types) => { | ||
// TODO: extensive list | ||
return obj.type === 'JSXElement' || obj.type === 'CallExpression' | ||
return obj.type === 'JSXElement' || obj.type === 'CallExpression' || obj.type === 'Identifier' | ||
} | ||
@@ -169,1 +180,77 @@ | ||
} | ||
export const appendAssetsAST = ( | ||
assets: UIDLGlobalAsset[], | ||
options: EntryFileOptions, | ||
headNode: types.JSXElement, | ||
bodyNode: types.JSXElement | ||
) => { | ||
assets.forEach((asset) => { | ||
const assetPath = UIDLUtils.prefixAssetsPath(options.assetsPrefix, asset.path) | ||
// link canonical for SEO | ||
if (asset.type === 'canonical' && assetPath) { | ||
const linkTag = createJSXTag('link') | ||
addAttributeToJSXTag(linkTag, 'rel', 'canonical') | ||
addAttributeToJSXTag(linkTag, 'href', assetPath) | ||
addChildJSXTag(headNode, linkTag) | ||
} | ||
// link stylesheet (external css, font) | ||
if ((asset.type === 'style' || asset.type === 'font') && assetPath) { | ||
const linkTag = createJSXTag('link') | ||
addAttributeToJSXTag(linkTag, 'rel', 'stylesheet') | ||
addAttributeToJSXTag(linkTag, 'href', assetPath) | ||
addChildJSXTag(headNode, linkTag) | ||
} | ||
// inline style | ||
if (asset.type === 'style' && asset.content) { | ||
const styleTag = createJSXTag('style') | ||
addAttributeToJSXTag(styleTag, 'dangerouslySetInnerHTML', { __html: asset.content }) | ||
addChildJSXTag(headNode, styleTag) | ||
} | ||
// script (external or inline) | ||
if (asset.type === 'script') { | ||
const scriptTag = createJSXTag('script') | ||
addAttributeToJSXTag(scriptTag, 'type', 'text/javascript') | ||
if (assetPath) { | ||
addAttributeToJSXTag(scriptTag, 'src', assetPath) | ||
if (asset.options && asset.options.defer) { | ||
addAttributeToJSXTag(scriptTag, 'defer', true) | ||
} | ||
if (asset.options && asset.options.async) { | ||
addAttributeToJSXTag(scriptTag, 'async', true) | ||
} | ||
} else if (asset.content) { | ||
addAttributeToJSXTag(scriptTag, 'dangerouslySetInnerHTML', { | ||
__html: asset.content, | ||
}) | ||
} | ||
if (asset.options && asset.options.target === 'body') { | ||
addChildJSXTag(bodyNode, scriptTag) | ||
} else { | ||
addChildJSXTag(headNode, scriptTag) | ||
} | ||
} | ||
// icon | ||
if (asset.type === 'icon' && assetPath) { | ||
const iconTag = createJSXTag('link') | ||
addAttributeToJSXTag(iconTag, 'rel', 'shortcut icon') | ||
addAttributeToJSXTag(iconTag, 'href', assetPath) | ||
if (asset.options && asset.options.iconType) { | ||
addAttributeToJSXTag(iconTag, 'type', asset.options.iconType) | ||
} | ||
if (asset.options && asset.options.iconSizes) { | ||
addAttributeToJSXTag(iconTag, 'sizes', asset.options.iconSizes) | ||
} | ||
addChildJSXTag(headNode, iconTag) | ||
} | ||
}) | ||
return { headNode, bodyNode } | ||
} |
@@ -248,1 +248,15 @@ import * as types from '@babel/types' | ||
} | ||
export const findAttributeByName = (jsxTag: types.JSXElement, attrName: string) => { | ||
return jsxTag.openingElement.attributes.find( | ||
(attr) => attr.type === 'JSXAttribute' && attr.name.name === attrName | ||
) as types.JSXAttribute | ||
} | ||
export const removeAttributeByName = (jsxTag: types.JSXElement, attrName: string) => { | ||
jsxTag.openingElement.attributes = jsxTag.openingElement.attributes.filter( | ||
(attr) => | ||
attr.type === 'JSXSpreadAttribute' || | ||
(attr.type === 'JSXAttribute' && attr.name.name !== attrName) | ||
) | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
656633
5641