From 5904a647dbb1ecbbf6f7dfa605ea67b17e440966 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 10 Dec 2017 20:09:09 -0500 Subject: [PATCH] support dynamic import - closes #1003 --- package.json | 1 + src/parse/read/script.ts | 10 +- test/parser/samples/dynamic-import/input.html | 9 + .../parser/samples/dynamic-import/output.json | 182 ++++++++++++++++++ yarn.lock | 10 + 5 files changed, 210 insertions(+), 2 deletions(-) create mode 100644 test/parser/samples/dynamic-import/input.html create mode 100644 test/parser/samples/dynamic-import/output.json diff --git a/package.json b/package.json index f1aead552d..833da2d4a7 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "@types/mocha": "^2.2.41", "@types/node": "^8.0.17", "acorn": "^5.1.1", + "acorn-dynamic-import": "^2.0.2", "chalk": "^2.0.1", "codecov": "^2.2.0", "console-group": "^0.3.2", diff --git a/src/parse/read/script.ts b/src/parse/read/script.ts index 232c32319b..d88c0a822d 100644 --- a/src/parse/read/script.ts +++ b/src/parse/read/script.ts @@ -1,10 +1,13 @@ -import { parse } from 'acorn'; +import * as acorn from 'acorn'; +import injectDynamicImport from 'acorn-dynamic-import/src/inject'; import repeat from '../../utils/repeat'; import { Parser } from '../index'; import { Node } from '../../interfaces'; const scriptClosingTag = ''; +injectDynamicImport(acorn); + export default function readScript(parser: Parser, start: number, attributes: Node[]) { const scriptStart = parser.index; const scriptEnd = parser.template.indexOf(scriptClosingTag, scriptStart); @@ -18,9 +21,12 @@ export default function readScript(parser: Parser, start: number, attributes: No let ast; try { - ast = parse(source, { + ast = acorn.parse(source, { ecmaVersion: 8, sourceType: 'module', + plugins: { + dynamicImport: true + } }); } catch (err) { parser.acornError(err); diff --git a/test/parser/samples/dynamic-import/input.html b/test/parser/samples/dynamic-import/input.html new file mode 100644 index 0000000000..553ca6c38c --- /dev/null +++ b/test/parser/samples/dynamic-import/input.html @@ -0,0 +1,9 @@ + \ No newline at end of file diff --git a/test/parser/samples/dynamic-import/output.json b/test/parser/samples/dynamic-import/output.json new file mode 100644 index 0000000000..b27a780a58 --- /dev/null +++ b/test/parser/samples/dynamic-import/output.json @@ -0,0 +1,182 @@ +{ + "hash": 1867472549, + "html": { + "start": null, + "end": null, + "type": "Fragment", + "children": [] + }, + "css": null, + "js": { + "start": 0, + "end": 131, + "attributes": [], + "content": { + "type": "Program", + "start": 8, + "end": 122, + "body": [ + { + "type": "ExportDefaultDeclaration", + "start": 10, + "end": 121, + "declaration": { + "type": "ObjectExpression", + "start": 25, + "end": 121, + "properties": [ + { + "type": "Property", + "start": 29, + "end": 118, + "method": true, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 29, + "end": 37, + "name": "oncreate" + }, + "kind": "init", + "value": { + "type": "FunctionExpression", + "start": 37, + "end": 118, + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 40, + "end": 118, + "body": [ + { + "type": "ExpressionStatement", + "start": 45, + "end": 114, + "expression": { + "type": "CallExpression", + "start": 45, + "end": 113, + "callee": { + "type": "MemberExpression", + "start": 45, + "end": 68, + "object": { + "type": "CallExpression", + "start": 45, + "end": 63, + "callee": { + "type": "Import", + "start": 45, + "end": 51 + }, + "arguments": [ + { + "type": "Literal", + "start": 52, + "end": 62, + "value": "./foo.js", + "raw": "'./foo.js'" + } + ] + }, + "property": { + "type": "Identifier", + "start": 64, + "end": 68, + "name": "then" + }, + "computed": false + }, + "arguments": [ + { + "type": "ArrowFunctionExpression", + "start": 69, + "end": 112, + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 69, + "end": 72, + "name": "foo" + } + ], + "body": { + "type": "BlockStatement", + "start": 76, + "end": 112, + "body": [ + { + "type": "ExpressionStatement", + "start": 82, + "end": 107, + "expression": { + "type": "CallExpression", + "start": 82, + "end": 106, + "callee": { + "type": "MemberExpression", + "start": 82, + "end": 93, + "object": { + "type": "Identifier", + "start": 82, + "end": 89, + "name": "console" + }, + "property": { + "type": "Identifier", + "start": 90, + "end": 93, + "name": "log" + }, + "computed": false + }, + "arguments": [ + { + "type": "MemberExpression", + "start": 94, + "end": 105, + "object": { + "type": "Identifier", + "start": 94, + "end": 97, + "name": "foo" + }, + "property": { + "type": "Identifier", + "start": 98, + "end": 105, + "name": "default" + }, + "computed": false + } + ] + } + } + ] + } + } + ] + } + } + ] + } + } + } + ] + } + } + ], + "sourceType": "module" + } + } +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index c38c354a78..bf6229baef 100644 --- a/yarn.lock +++ b/yarn.lock @@ -22,6 +22,12 @@ abbrev@1: version "1.1.1" resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8" +acorn-dynamic-import@^2.0.2: + version "2.0.2" + resolved "https://registry.yarnpkg.com/acorn-dynamic-import/-/acorn-dynamic-import-2.0.2.tgz#c752bd210bef679501b6c6cb7fc84f8f47158cc4" + dependencies: + acorn "^4.0.3" + acorn-globals@^4.0.0: version "4.1.0" resolved "https://registry.yarnpkg.com/acorn-globals/-/acorn-globals-4.1.0.tgz#ab716025dbe17c54d3ef81d32ece2b2d99fe2538" @@ -44,6 +50,10 @@ acorn@^3.0.4, acorn@^3.1.0, acorn@^3.3.0: version "3.3.0" resolved "https://registry.yarnpkg.com/acorn/-/acorn-3.3.0.tgz#45e37fb39e8da3f25baee3ff5369e2bb5f22017a" +acorn@^4.0.3: + version "4.0.13" + resolved "https://registry.yarnpkg.com/acorn/-/acorn-4.0.13.tgz#105495ae5361d697bd195c825192e1ad7f253787" + acorn@^5.0.0, acorn@^5.1.1, acorn@^5.1.2, acorn@^5.2.1: version "5.2.1" resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.2.1.tgz#317ac7821826c22c702d66189ab8359675f135d7"