Skip to content

Commit 87fda26

Browse files
committed
3.0.0 release
1 parent e1db25b commit 87fda26

File tree

7 files changed

+50
-335
lines changed

7 files changed

+50
-335
lines changed

lib/get-partials.js

Lines changed: 0 additions & 87 deletions
This file was deleted.

lib/get-script.js

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,22 @@
11
const transformScript = require('./transform-script');
22

3-
module.exports = ({
4-
id,
5-
source,
6-
template,
7-
templateType,
8-
style,
9-
styleScoped,
10-
partials,
11-
}) => {
3+
module.exports = ({ id, source, template, style, styleScoped }) => {
124
// Parse Script
135
let script;
146
if (source.indexOf('<script>') >= 0) {
157
const scripts = source.split('<script>');
168
script = scripts[scripts.length - 1].split('</script>')[0].trim();
179
} else {
18-
script = 'export default {}';
19-
}
20-
if (!script || !script.trim()) script = 'export default {}';
21-
22-
if (!template) {
23-
return script;
10+
script = 'export default () => { return $render }';
2411
}
12+
if (!script || !script.trim()) script = 'export default { return $render }';
2513

2614
const code = transformScript({
2715
id,
2816
script,
2917
template,
30-
templateType,
3118
style,
3219
styleScoped,
33-
partials,
3420
});
3521
return code;
3622
};

lib/get-template.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
const minify = require('html-minifier').minify;
2-
3-
module.exports = ({ source, options }) => {
1+
module.exports = ({ source }) => {
42
let template = null;
53
const hasTemplate = source.match(/<template([ ]?)([a-z0-9-]*)>/);
6-
const templateType = hasTemplate[2] || 't7';
7-
const minifyTemplate = !options || options.minifyTemplate !== false;
84
if (hasTemplate) {
95
template = source
106
.split(/<template[ ]?[a-z0-9-]*>/)
@@ -17,20 +13,8 @@ module.exports = ({ source, options }) => {
1713
.replace(/\/template>([ \n]*){{\/raw}}/g, '/template>{{/raw}}')
1814
.replace(/([ \n])<template/g, '$1{{#raw}}<template')
1915
.replace(/\/template>([ \n])/g, '/template>{{/raw}}$1');
20-
const originalTemplate = template;
21-
if (minifyTemplate) {
22-
try {
23-
template = minify(template, {
24-
removeAttributeQuotes: true,
25-
collapseWhitespace: true,
26-
});
27-
} catch (err) {
28-
template = originalTemplate;
29-
}
30-
}
3116
}
3217
return {
3318
template,
34-
templateType,
3519
};
3620
};

lib/index.js

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,16 @@
1-
const path = require('path');
2-
const loaderUtils = require('loader-utils');
3-
const Template7 = require('template7');
4-
51
const getTemplate = require('./get-template');
62
const getStyle = require('./get-style');
73
const getScript = require('./get-script');
8-
const getPartials = require('./get-partials');
94

105
function generateId(mask = 'xxxxxxxxxx', map = '0123456789abcdef') {
116
const length = map.length;
127
return mask.replace(/x/g, () => map[Math.floor(Math.random() * length)]);
138
}
149

1510
function loader(source) {
16-
const loaderContext = this;
17-
const options = loaderUtils.getOptions(loaderContext);
18-
if (options && options.helpersPath) {
19-
try {
20-
// eslint-disable-next-line
21-
const helpers = require(path.resolve(options.helpersPath));
22-
helpers.forEach((helperName) => {
23-
Template7.registerHelper(helperName, () => {});
24-
});
25-
} catch (e) {
26-
// error
27-
}
28-
}
29-
3011
const id = generateId();
3112

32-
const { template, templateType } = getTemplate({ source });
33-
34-
// Parse external partials.
35-
const { partials } = getPartials({
36-
source,
37-
template,
38-
options,
39-
});
13+
const { template } = getTemplate({ source });
4014

4115
// Parse Styles
4216
const { style, styleScoped } = getStyle({ id, source });
@@ -46,10 +20,8 @@ function loader(source) {
4620
id,
4721
source,
4822
template,
49-
templateType,
5023
style,
5124
styleScoped,
52-
partials,
5325
});
5426

5527
return script;

lib/transform-script.js

Lines changed: 41 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,77 @@
11
const babel = require('@babel/core');
2-
const Template7 = require('template7');
32

43
function transformToAst(code) {
54
const { ast } = babel.transformSync(code, {
65
ast: true,
7-
babelrc: false,
8-
configFile: false,
96
});
107
return ast;
118
}
129

1310
function transformFromAst(ast) {
14-
const { code } = babel.transformFromAst(ast, '', {
15-
babelrc: false,
16-
configFile: false,
17-
});
11+
const { code } = babel.transformFromAst(ast, '', {});
1812
return code;
1913
}
2014

21-
module.exports = ({
22-
script,
23-
template,
24-
templateType,
25-
id,
26-
style,
27-
styleScoped,
28-
partials,
29-
}) => {
15+
module.exports = ({ script, template, id, style, styleScoped }) => {
3016
let astExtend;
31-
let isClassComponent =
32-
script.indexOf('export default class') >= 0 ||
33-
script.indexOf('class extends') >= 0;
34-
let isClassDeclared;
35-
let isClassExported;
36-
let astExtendClass = `
37-
class {{PAGE_COMPONENT_CLASS_NAME}} extends Component {
38-
render() {
39-
${
40-
templateType === 't7'
41-
? `return (${Template7.compile(template)})(this)`
42-
: `return \`${template}\``
43-
}
44-
}
17+
const astExtendFunction = `
18+
function {{COMPONENT_NAME}}(props, ctx) {
19+
4520
}
46-
{{PAGE_COMPONENT_CLASS_NAME}}.id = '${id}';
21+
{{COMPONENT_NAME}}.id = '${id}';
4722
${
4823
style
4924
? `
50-
{{PAGE_COMPONENT_CLASS_NAME}}.style = \`${style}\`;
25+
{{COMPONENT_NAME}}.style = \`${style}\`;
5126
`.trim()
5227
: ''
5328
}
54-
{{PAGE_COMPONENT_CLASS_NAME}}.styleScoped = ${styleScoped};
29+
{{COMPONENT_NAME}}.styleScoped = ${styleScoped};
5530
56-
export default {{PAGE_COMPONENT_CLASS_NAME}};
57-
`;
58-
const astExtendObj = `
59-
export default {
60-
id: '${id}',
61-
${
62-
templateType === 't7'
63-
? `render() { return (${Template7.compile(template)})(this)},`
64-
: `render() {return \`${template}\`},`
65-
}
66-
${style ? `style: \`${style}\`,` : ''}
67-
styleScoped: ${styleScoped},
68-
}
31+
export default {{COMPONENT_NAME}};
6932
`;
7033

71-
const ast = transformToAst(script);
34+
const ast = transformToAst(
35+
`/** @jsx $jsx */\nimport { $jsx } from 'framework7';\n${script}`,
36+
);
7237

7338
ast.program.body.forEach((node, index) => {
74-
if (node.type === 'ClassDeclaration' && node.superClass) {
75-
if (isClassExported) return;
76-
isClassComponent = true;
77-
isClassDeclared = true;
78-
astExtendClass = astExtendClass
79-
.replace('export default {{PAGE_COMPONENT_CLASS_NAME}};', '')
80-
.replace(/{{PAGE_COMPONENT_CLASS_NAME}}/g, node.id.name);
81-
astExtend = transformToAst(astExtendClass);
82-
node.body.body.push(...astExtend.program.body[0].body.body);
83-
astExtend.program.body.splice(0, 1); // remove fist declaration
84-
ast.program.body.splice(index + 1, 0, ...astExtend.program.body); // insert component static props
85-
}
8639
if (node.type === 'ExportDefaultDeclaration') {
87-
if (isClassComponent) {
88-
if (isClassDeclared) return;
89-
isClassExported = true;
90-
astExtendClass = astExtendClass.replace(
91-
/{{PAGE_COMPONENT_CLASS_NAME}}/g,
92-
'F7PageComponent',
93-
);
94-
astExtend = transformToAst(astExtendClass);
95-
96-
if (node.declaration.type === 'ClassDeclaration') {
97-
astExtend.program.body[0].superClass = node.declaration.superClass;
98-
astExtend.program.body[0].body.body.push(
99-
...node.declaration.body.body,
100-
);
101-
102-
ast.program.body.splice(index, 1);
103-
ast.program.body.push(...astExtend.program.body);
104-
}
105-
} else {
106-
astExtend = transformToAst(astExtendObj);
107-
node.declaration.properties.push(
108-
...astExtend.program.body[0].declaration.properties,
40+
if (
41+
node.declaration &&
42+
(node.declaration.type === 'ArrowFunctionExpression' ||
43+
node.declaration.type === 'FunctionDeclaration')
44+
) {
45+
astExtend = transformToAst(
46+
astExtendFunction.replace(
47+
/{{COMPONENT_NAME}}/g,
48+
'framework7Component',
49+
),
10950
);
51+
astExtend.program.body[0].params = node.declaration.params;
52+
astExtend.program.body[0].body = node.declaration.body;
53+
ast.program.body.splice(index, 1, ...astExtend.program.body);
11054
}
11155
}
11256
});
11357

114-
let code = transformFromAst(ast);
58+
const code = transformFromAst(ast).replace(
59+
'$render',
60+
`function ($$ctx) {
61+
var $ = $$ctx.$$;
62+
var $h = $$ctx.$h;
63+
var $root = $$ctx.$root;
64+
var $f7 = $$ctx.$f7;
65+
var $f7route = $$ctx.$f7route;
66+
var $f7router = $$ctx.$f7router;
67+
var $theme = $$ctx.$theme;
68+
var $update = $$ctx.$update;
69+
var $store = $$ctx.$store;
11570
116-
if (templateType === 't7' && code.indexOf('Template7Helpers') >= 0) {
117-
code = `
118-
import Template7 from 'template7';
119-
const Template7Helpers = Template7.helpers;
120-
121-
${partials.join('\n')}
122-
123-
${code}
124-
`;
125-
}
71+
return $h\`${template}\`
72+
}
73+
`,
74+
);
12675

12776
return code;
12877
};

0 commit comments

Comments
 (0)