1
0
mirror of https://github.com/bobwen-dev/react-templates synced 2025-04-12 00:56:39 +02:00
react-templates/src/reactSupport.js
nino-porcino 46caeea387 Fix #138, #139, #134, #144, #145, #147, #157, #158, #161, #162 (#142)
* Single child for rt-template (fixes #138)

* Nested scope functions (fixes #139)

* Forbid nested rt-import (fixes #134)

* Vendor prefixes style keys (#144)

* Renamed test file

* Forbid expressions in style keys (#145)

* fixed rt-import and AMD (#147)

* Updated documentation

* Fixed links in README.md

* Sanitize comments (fix #158)

* Disable comments when es6 (fix#157)

* Added test case for comments

* Simplified TypeScript output

* Test cases for simplified TypeScript output

* Fixed wrong TypeScript output (#161)

* fix repeat with custom index (#162)
2016-07-06 11:25:08 +03:00

90 lines
4.2 KiB
JavaScript

'use strict';
const _ = require('lodash');
/**
* @param {Context} context
* @return {boolean}
*/
function shouldUseCreateElement(context) {
switch (context.options.targetVersion) {
case '0.11.2':
case '0.11.1':
case '0.11.0':
case '0.10.0':
return false;
default:
return true;
}
}
const reactSupportedAttributes = ['accept', 'acceptCharset', 'accessKey', 'action', 'allowFullScreen', 'allowTransparency', 'alt', 'async', 'autoComplete', 'autoPlay', 'cellPadding', 'cellSpacing', 'charSet', 'checked',
'classID', 'className', 'cols', 'colSpan', 'content', 'contentEditable', 'contextMenu', 'controls', 'coords', 'crossOrigin', 'data', 'dateTime', 'defer', 'dir', 'disabled', 'download',
'draggable', 'encType', 'form', 'formNoValidate', 'frameBorder', 'height', 'hidden', 'href', 'hrefLang', 'htmlFor', 'httpEquiv', 'icon', 'id', 'label', 'lang', 'list', 'loop', 'manifest',
'max', 'maxLength', 'media', 'mediaGroup', 'method', 'min', 'multiple', 'muted', 'name', 'noValidate', 'open', 'pattern', 'placeholder', 'poster', 'preload', 'radioGroup', 'readOnly', 'rel',
'required', 'role', 'rows', 'rowSpan', 'sandbox', 'scope', 'scrolling', 'seamless', 'selected', 'shape', 'size', 'sizes', 'span', 'spellCheck', 'src', 'srcDoc', 'srcSet', 'start', 'step',
'style', 'tabIndex', 'target', 'title', 'type', 'useMap', 'value', 'width', 'wmode'];
const classNameProp = 'className';
const attributesMapping = {'class': classNameProp, 'rt-class': classNameProp, 'for': 'htmlFor'}; //eslint-disable-line quote-props
_.forEach(reactSupportedAttributes, attributeReactName => {
if (attributeReactName !== attributeReactName.toLowerCase()) {
attributesMapping[attributeReactName.toLowerCase()] = attributeReactName;
}
});
const htmlSelfClosingTags = ['area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
const templateAMDTemplate = _.template("define(<%= name ? '\"'+name + '\", ' : '' %>[<%= requirePaths %>], function (<%= AMDArguments %>) {\n'use strict';\n<%= AMDSubstitutions %>return <%= renderFunction %>;\n});");
const templateCommonJSTemplate = _.template("'use strict';\n<%= vars %>\nmodule.exports = <%= renderFunction %>;\n");
const templateES6Template = _.template('<%= vars %>\nexport default <%= renderFunction %>\n');
const templatePJSTemplate = _.template('var <%= name %> = <%= renderFunction %>');
const templateTypescriptTemplate = _.template('<%= vars %>\nexport = <%= renderFunction %>;\n');
const templateJSRTTemplate = _.template('<%= renderFunction %>');
const templates = {
amd: templateAMDTemplate,
commonjs: templateCommonJSTemplate,
typescript: templateTypescriptTemplate,
es6: templateES6Template,
none: templatePJSTemplate,
jsrt: templateJSRTTemplate
};
const isImportAsterisk = _.matches({member: '*'});
const defaultCase = _.constant(true);
const buildImportTypeScript = _.cond([
[isImportAsterisk, d => `import ${d.alias} = require('${d.moduleName}');`],
[_.matches({member: 'default'}), d => `import ${d.alias} from '${d.moduleName}';`],
[defaultCase, d => `import { ${d.member} as ${d.alias} } from '${d.moduleName}';`]
]);
const buildImportES6 = _.cond([
[isImportAsterisk, d => `import * as ${d.alias} from '${d.moduleName}';`],
[_.matches({member: 'default'}), d => `import ${d.alias} from '${d.moduleName}';`],
[defaultCase, d => `import { ${d.member} as ${d.alias} } from '${d.moduleName}';`]
]);
const buildImportCommonJS = _.cond([
[isImportAsterisk, d => `var ${d.alias} = require('${d.moduleName}');`],
[defaultCase, d => `var ${d.alias} = require('${d.moduleName}').${d.member};`]
]);
const buildImport = {
typescript: buildImportTypeScript,
es6: buildImportES6,
commonjs: buildImportCommonJS,
amd: buildImportCommonJS,
none: buildImportCommonJS,
jsrt: buildImportCommonJS
};
module.exports = {
htmlSelfClosingTags,
attributesMapping,
classNameProp,
shouldUseCreateElement,
templates,
buildImport
};