Skip to content

Commit

Permalink
Transform tagged template
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-sky committed Nov 27, 2018
1 parent ebc7c1f commit 9db4040
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/babel-plugin-nornj-in-jsx/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions packages/babel-plugin-nornj-in-jsx/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const nj = require('nornj').default;
// const transformWith = require('./withTag');
const transformExTag = require('./exTag');
const transformExAttr = require('./exAttr');
const transformExpression = require('./expression');
const transformTaggedTemplate = require('./taggedTemplate');
const astUtil = require('./util/ast');
const utils = require('./util/utils');

Expand All @@ -19,7 +19,8 @@ module.exports = function (babel) {
// };
const exTagHandler = transformExTag(babel);
const exAttrHandler = transformExAttr(babel);
const expressionHandler = transformExpression(babel);
const taggedTemplateHandler = transformTaggedTemplate(babel);
let TaggedTemplates = ['nj', 'n', 't', 's'];

const visitor = {
JSXElement: {
Expand Down Expand Up @@ -57,8 +58,9 @@ module.exports = function (babel) {
}
},
TaggedTemplateExpression(path, state) {
if (path.node.tag.name === 'n') {
path.replaceWith(expressionHandler(path.node, path.hub.file, state));
const taggedName = path.node.tag.name;
if (TaggedTemplates.indexOf(taggedName) >= 0) {
path.replaceWith(taggedTemplateHandler(path.node, path.hub.file, state, taggedName));
}
},
ImportDeclaration(path, state) {
Expand Down Expand Up @@ -120,7 +122,8 @@ module.exports = function (babel) {
[
types.importDefaultSpecifier(types.identifier('nj')),
types.importSpecifier(types.identifier('n'), types.identifier('expression')),
types.importSpecifier(types.identifier('t'), types.identifier('template'))
types.importSpecifier(types.identifier('t'), types.identifier('template')),
types.importSpecifier(types.identifier('s'), types.identifier('css'))
],
types.stringLiteral('nornj')
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const generate = require('./util/generate');
module.exports = function (babel) {
const types = babel.types;

return function (node, file, state) {
return function (node, file, state, taggedName) {
const { quasis, expressions } = node.quasi;
return generate.createRenderTmpl(babel, quasis, expressions, state.opts, { isExpresson: true });
return generate.createRenderTmpl(babel, quasis, expressions, state.opts, taggedName);
};
};
14 changes: 12 additions & 2 deletions packages/babel-plugin-nornj-in-jsx/src/util/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,11 @@ const CTX_DATAS = 'datas';
const CTX_DATA = 'data';
const CTX_GET_DATA = 'getData';

function createRenderTmpl(babel, quasis, expressions, opts, taggedTmplConfig) {
function createRenderTmpl(babel, quasis, expressions, opts, taggedName) {
const types = babel.types;

let tmplStr = '';
if (!taggedTmplConfig) {
if (!taggedName) {
let paramCount = 0;
quasis.forEach((q, i) => {
tmplStr += q.value.cooked;
Expand All @@ -203,6 +203,16 @@ function createRenderTmpl(babel, quasis, expressions, opts, taggedTmplConfig) {
});
}
else {
let taggedTmplConfig = {};
switch (taggedName) {
case 'n':
taggedTmplConfig = { isExpresson: true };
break;
case 's':
taggedTmplConfig = { isCss: true };
break;
}

tmplStr = Object.assign({ quasis: quasis.map(q => q.value.cooked) }, taggedTmplConfig);
}

Expand Down

0 comments on commit 9db4040

Please sign in to comment.