Skip to content

Commit

Permalink
support export default (part of #15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Feb 9, 2016
1 parent be14f25 commit 8ddafed
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"repository": "https://github.com/ractivejs/rcu",
"dependencies": {
"eval2": "^0.3.0",
"tippex": "^1.1.0",
"tippex": "^1.2.0",
"vlq": "^0.2.0"
},
"devDependencies": {
Expand Down
6 changes: 4 additions & 2 deletions src/parse.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { match } from 'tippex';
import { match, replace } from 'tippex';
import { Ractive } from './init.js';
import getName from './getName.js';
import getLinePosition from './utils/getLinePosition.js';
Expand Down Expand Up @@ -93,7 +93,9 @@ export default function parse ( source ) {
result.scriptStart = getLinePosition( lines, contentStart );
result.scriptEnd = getLinePosition( lines, contentEnd );

result.script = source.slice( contentStart, contentEnd );
const script = source.slice( contentStart, contentEnd );

result.script = replace( script, /export\s+default\s+/, () => 'component.exports = ' );

match( result.script, requirePattern, ( match, doubleQuoted, singleQuoted ) => {
const source = doubleQuoted || singleQuoted;
Expand Down
9 changes: 9 additions & 0 deletions test/parse/input/withExportDefault.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>Hello world!</h1>

<script>
export default {
oninit: function () {
console.log( 'hello world' );
}
};
</script>
10 changes: 10 additions & 0 deletions test/parse/output/withExportDefault.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"source": "<h1>Hello world!</h1>\n\n<script>\n\texport default {\n\t\toninit: function () {\n\t\t\tconsole.log( 'hello world' );\n\t\t}\n\t};\n</script>\n",
"template": {"v":3,"t":[{"t":7,"e":"h1","f":["Hello world!"],"p":[1,1,0]}]},
"script": "\n\tcomponent.exports = {\n\t\toninit: function () {\n\t\t\tconsole.log( 'hello world' );\n\t\t}\n\t};\n",
"scriptStart": { "line": 2, "column": 8, "char": 31 },
"scriptEnd": { "line": 8, "column": 0, "char": 115 },
"css": "",
"imports": [],
"modules": []
}

0 comments on commit 8ddafed

Please sign in to comment.