pull/1167/merge
Fernando Tolentino 8 years ago committed by GitHub
commit acd8aa7869
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -35,6 +35,8 @@ export default function readScript(parser: Parser, start: number, attributes: No
if (!ast.body.length) return null; if (!ast.body.length) return null;
ast.start = scriptStart; ast.start = scriptStart;
visitImports(ast, parser);
return { return {
start, start,
end: parser.index, end: parser.index,
@ -42,3 +44,120 @@ export default function readScript(parser: Parser, start: number, attributes: No
content: ast, content: ast,
}; };
} }
function visitImports(ast, parser) {
ast.body
.filter(stmt => stmt.type === 'ImportDeclaration')
.forEach((stmt) => {
const m = /[\.\\]?(\w+)\.html$/g.exec(stmt.source.value);
if (m) {
let componentName;
if (stmt.specifiers.length && stmt.specifiers[0].type === 'ImportDefaultSpecifier') {
componentName = stmt.specifiers[0].local.name;
} else {
componentName = m[1];
stmt.specifiers.push(
{
"type": "ImportDefaultSpecifier",
"start": stmt.start,
"end": stmt.end,
"local": {
"type": "Identifier",
"start": stmt.source.start,
"end": stmt.source.end,
"name": componentName
}
}
);
}
exportComponent(ast, parser, stmt, componentName);
}
});
}
function exportComponent(ast, parser, importStmt, componentName) {
let wasExported = false;
let exportStatement, exportedComponents;
ast.body
.filter(stmt => stmt.type === 'ExportDefaultDeclaration')
.some((stmt) => {
exportStatement = stmt;
stmt.declaration.properties.some((p) => {
const isComponents = p.type === 'Property' && p.key.name === 'components';
if (isComponents) {
if (p.value.type !== 'ObjectExpression')
parser.error(`export default must have components as an object`);
else {
exportedComponents = p;
wasExported = exportedComponents.value.properties.some(comp => comp.type === 'Property' && comp.key.name === componentName);
}
}
return wasExported;
});
return wasExported;
});
if (!wasExported) {
if (!exportStatement) {
exportStatement = {
"type": "ExportDefaultDeclaration",
"start": importStmt.start,
"end": importStmt.end,
"declaration": {
"type": "ObjectExpression",
"start": importStmt.start,
"end": importStmt.end,
"properties": [
]
}
};
ast.body.push(exportStatement);
}
if (!exportedComponents) {
exportedComponents = {
"type": "Property",
"start": exportStatement.start,
"end": exportStatement.end,
"method": false,
"shorthand": false,
"computed": false,
"key": {
"type": "Identifier",
"start": exportStatement.start,
"end": exportStatement.end,
"name": "components"
},
"value": {
"type": "ObjectExpression",
"start": exportStatement.start,
"end": exportStatement.end,
"properties": []
},
"kind": "init"
};
exportStatement.declaration.properties.push(exportedComponents);
}
exportedComponents.value.properties.push(
{
"type": "Property",
"start": importStmt.start,
"end": importStmt.end,
"method": false,
"shorthand": true,
"computed": false,
"key": {
"type": "Identifier",
"start": importStmt.start,
"end": importStmt.start,
"name": componentName
},
"kind": "init",
"value": {
"type": "Identifier",
"start": importStmt.start,
"end": importStmt.start,
"name": componentName
}
}
);
}
}

@ -3,11 +3,5 @@
</div> </div>
<script> <script>
import Nested from './Nested.html'; import './Nested.html';
export default {
components: {
Nested
}
};
</script> </script>

@ -0,0 +1,17 @@
export default {
snapshot(target) {
const p = target.querySelector('p');
return {
p,
text: p.childNodes[0]
};
},
test(assert, target, snapshot) {
const p = target.querySelector('p');
assert.equal(p, snapshot.p);
assert.equal(p.childNodes[0], snapshot.text);
}
};

@ -0,0 +1,5 @@
<RenamedNested/>
<script>
import RenamedNested from './Nested.html';
</script>

@ -0,0 +1,17 @@
export default {
snapshot(target) {
const p = target.querySelector('p');
return {
p,
text: p.childNodes[0]
};
},
test(assert, target, snapshot) {
const p = target.querySelector('p');
assert.equal(p, snapshot.p);
assert.equal(p.childNodes[0], snapshot.text);
}
};

@ -0,0 +1,5 @@
<Nested/>
<script>
import './Nested.html';
</script>

@ -3,17 +3,13 @@
<script> <script>
import { Store } from '../../../../store.js'; import { Store } from '../../../../store.js';
import Nested from './Nested.html'; import './Nested.html';
export default { export default {
store () { store () {
return new Store({ return new Store({
name: 'world' name: 'world'
}); });
},
components: {
Nested
} }
}; };
</script> </script>

@ -3,9 +3,5 @@
</svg> </svg>
<script> <script>
import Rect from './Rect.html'; import './Rect.html';
export default {
components: { Rect }
};
</script> </script>

Before

Width:  |  Height:  |  Size: 179 B

After

Width:  |  Height:  |  Size: 123 B

@ -3,9 +3,5 @@
</svg> </svg>
<script> <script>
import Rect from './Rect.html'; import './Rect.html';
export default {
components: { Rect }
};
</script> </script>

Before

Width:  |  Height:  |  Size: 179 B

After

Width:  |  Height:  |  Size: 123 B

Loading…
Cancel
Save