From be14f25788c339484c7a513c0120f232fa5d7235 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 9 Feb 2016 16:54:31 -0500 Subject: [PATCH 1/3] rename existing tests --- test/parse/input/{withImport.html => withSubcomponent.html} | 0 test/parse/output/{withImport.json => withSubcomponent.json} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename test/parse/input/{withImport.html => withSubcomponent.html} (100%) rename test/parse/output/{withImport.json => withSubcomponent.json} (100%) 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/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 From 8ddafed9f9a3986f6fea4c5efbfa2c5e5a747c75 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 9 Feb 2016 18:38:05 -0500 Subject: [PATCH 2/3] support export default (part of #15) --- package.json | 2 +- src/parse.js | 6 ++++-- test/parse/input/withExportDefault.html | 9 +++++++++ test/parse/output/withExportDefault.json | 10 ++++++++++ 4 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 test/parse/input/withExportDefault.html create mode 100644 test/parse/output/withExportDefault.json 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..8ddaefc 100644 --- a/src/parse.js +++ b/src/parse.js @@ -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'; @@ -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; 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/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": [] +} From c740d062cb158502099a38fb8431169080cb8073 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 9 Feb 2016 20:13:07 -0500 Subject: [PATCH 3/3] extract dependencies from import declarations --- src/parse.js | 11 +++++++---- test/parse/input/withImports.html | 6 ++++++ ...xternalDependency.html => withRequire.html} | 0 test/parse/output/withImports.json | 18 ++++++++++++++++++ ...xternalDependency.json => withRequire.json} | 0 5 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 test/parse/input/withImports.html rename test/parse/input/{withExternalDependency.html => withRequire.html} (100%) create mode 100644 test/parse/output/withImports.json rename test/parse/output/{withExternalDependency.json => withRequire.json} (100%) diff --git a/src/parse.js b/src/parse.js index 8ddaefc..62d9d35 100644 --- a/src/parse.js +++ b/src/parse.js @@ -3,7 +3,8 @@ 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 ) { @@ -97,10 +98,12 @@ export default function parse ( source ) { result.script = replace( script, /export\s+default\s+/, () => 'component.exports = ' ); - match( result.script, requirePattern, ( match, doubleQuoted, singleQuoted ) => { - const source = doubleQuoted || singleQuoted; + 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/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/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