support dynamic import - closes #1003

pull/1006/head
Rich Harris 7 years ago committed by GitHub
parent a10cc11fc3
commit 5904a647db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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",

@ -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 = '</script>';
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);

@ -0,0 +1,9 @@
<script>
export default {
oncreate() {
import('./foo.js').then(foo => {
console.log(foo.default);
});
}
}
</script>

@ -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"
}
}
}

@ -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"

Loading…
Cancel
Save