Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
17 changes: 11 additions & 6 deletions src/parse.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
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';

const requirePattern = /require\s*\(\s*(?:"([^"]+)"|'([^']+)')\s*\)/g;
const requirePattern = /\brequire\s*\(\s*[\'"]([^"\']+)["\']\s*\)/g;
const importPattern = /\bimport\s+(?:.+\s+from\s+)?[\'"]([^"\']+)["\']/g; // https://gist.github.com/pilwon/ff55634a29bb4456e0dd
const TEMPLATE_VERSION = 3;

export default function parse ( source ) {
Expand Down Expand Up @@ -93,12 +94,16 @@ 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 );

match( result.script, requirePattern, ( match, doubleQuoted, singleQuoted ) => {
const source = doubleQuoted || singleQuoted;
result.script = replace( script, /export\s+default\s+/, () => 'component.exports = ' );

const addUniqueSource = ( match, source ) => {
if ( !~modules.indexOf( source ) ) modules.push( source );
});
};

match( result.script, requirePattern, addUniqueSource );
match( result.script, importPattern, addUniqueSource );
}

return result;
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>
6 changes: 6 additions & 0 deletions test/parse/input/withImports.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
import { foo } from 'foo';
import bar from 'bar';
import * as baz from 'baz';
import 'qux';
</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": []
}
18 changes: 18 additions & 0 deletions test/parse/output/withImports.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"source": "<script>\n\timport { foo } from 'foo';\n\timport bar from 'bar';\n\timport * as baz from 'baz';\n\timport 'qux';\n</script>\n",
"template": {"v":3,"t":[]},
"script": "\n\timport { foo } from 'foo';\n\timport bar from 'bar';\n\timport * as baz from 'baz';\n\timport 'qux';\n",
"css": "",
"imports": [],
"modules": [ "foo", "bar", "baz", "qux" ],
"scriptStart": {
"line": 0,
"column": 8,
"char": 8
},
"scriptEnd": {
"line": 5,
"column": 0,
"char": 105
}
}