diff --git a/package.json b/package.json index 3222246..a738afa 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/parse.js b/src/parse.js index 19863ab..62d9d35 100644 --- a/src/parse.js +++ b/src/parse.js @@ -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 ) { @@ -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; diff --git a/test/parse/input/withExportDefault.html b/test/parse/input/withExportDefault.html new file mode 100644 index 0000000..c04c418 --- /dev/null +++ b/test/parse/input/withExportDefault.html @@ -0,0 +1,9 @@ +

Hello world!

+ + diff --git a/test/parse/input/withImports.html b/test/parse/input/withImports.html new file mode 100644 index 0000000..88744e7 --- /dev/null +++ b/test/parse/input/withImports.html @@ -0,0 +1,6 @@ + diff --git a/test/parse/input/withExternalDependency.html b/test/parse/input/withRequire.html similarity index 100% rename from test/parse/input/withExternalDependency.html rename to test/parse/input/withRequire.html diff --git a/test/parse/input/withImport.html b/test/parse/input/withSubcomponent.html similarity index 100% rename from test/parse/input/withImport.html rename to test/parse/input/withSubcomponent.html diff --git a/test/parse/output/withExportDefault.json b/test/parse/output/withExportDefault.json new file mode 100644 index 0000000..4c2432b --- /dev/null +++ b/test/parse/output/withExportDefault.json @@ -0,0 +1,10 @@ +{ + "source": "

Hello world!

\n\n\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": [] +} diff --git a/test/parse/output/withImports.json b/test/parse/output/withImports.json new file mode 100644 index 0000000..863e5b3 --- /dev/null +++ b/test/parse/output/withImports.json @@ -0,0 +1,18 @@ +{ + "source": "\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 + } +} diff --git a/test/parse/output/withExternalDependency.json b/test/parse/output/withRequire.json similarity index 100% rename from test/parse/output/withExternalDependency.json rename to test/parse/output/withRequire.json diff --git a/test/parse/output/withImport.json b/test/parse/output/withSubcomponent.json similarity index 100% rename from test/parse/output/withImport.json rename to test/parse/output/withSubcomponent.json