Skip to content

Commit e7d6fd1

Browse files
committed
Add back dynamic-import support.
1 parent a9d9cf4 commit e7d6fd1

File tree

7 files changed

+132
-4
lines changed

7 files changed

+132
-4
lines changed

LICENSE.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,33 @@ The code in the `lib/import-meta` folder is compiled from code licensed as MIT:
3737
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3838
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
3939
> THE SOFTWARE.
40+
41+
Find the source code at https://github.com/acornjs/acorn-import-meta.
42+
43+
## acorn-dynamic-import
44+
45+
The code in the `lib/dynamic-import` folder is licensed as MIT:
46+
47+
> MIT License
48+
>
49+
> Copyright (c) 2016 Jordan Gensler
50+
>
51+
> Permission is hereby granted, free of charge, to any person obtaining a copy
52+
> of this software and associated documentation files (the "Software"), to deal
53+
> in the Software without restriction, including without limitation the rights
54+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
55+
> copies of the Software, and to permit persons to whom the Software is
56+
> furnished to do so, subject to the following conditions:
57+
>
58+
> The above copyright notice and this permission notice shall be included in all
59+
> copies or substantial portions of the Software.
60+
>
61+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
62+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
63+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
64+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
65+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
66+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
67+
> SOFTWARE.
68+
69+
Find the source code at https://github.com/kesne/acorn-dynamic-import.

build.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ function compile (name, output) { // eslint-disable-line no-unused-vars
1818
}
1919

2020
compile('acorn-import-meta', './lib/import-meta/index.js')
21+
compile('./lib/dynamic-import/source', './lib/dynamic-import/index.js')

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var xtend = require('xtend')
33

44
var CJSParser = acorn.Parser
55
var ESModulesParser = acorn.Parser
6+
.extend(require('./lib/dynamic-import'))
67
.extend(require('./lib/import-meta'))
78

89
function mapOptions (opts) {

lib/dynamic-import/index.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* Generated by `npm run build`, do not edit! */
2+
3+
/* eslint-disable no-underscore-dangle */
4+
5+
var DynamicImportKey = 'Import';
6+
7+
var ref = require('acorn');
8+
var tt = ref.tokTypes;
9+
10+
// NOTE: This allows `yield import()` to parse correctly.
11+
tt._import.startsExpr = true;
12+
13+
function parseDynamicImport() {
14+
var node = this.startNode();
15+
this.next();
16+
if (this.type !== tt.parenL) {
17+
this.unexpected();
18+
}
19+
return this.finishNode(node, DynamicImportKey);
20+
}
21+
22+
function parenAfter() {
23+
return /^(\s|\/\/.*|\/\*[^]*?\*\/)*\(/.test(this.input.slice(this.pos));
24+
}
25+
26+
function dynamicImport(Parser) {
27+
return (function (Parser) {
28+
function anonymous () {
29+
Parser.apply(this, arguments);
30+
}
31+
32+
if ( Parser ) anonymous.__proto__ = Parser;
33+
anonymous.prototype = Object.create( Parser && Parser.prototype );
34+
anonymous.prototype.constructor = anonymous;
35+
36+
anonymous.prototype.parseStatement = function parseStatement (context, topLevel, exports) {
37+
if (this.type === tt._import && parenAfter.call(this)) {
38+
return this.parseExpressionStatement(this.startNode(), this.parseExpression());
39+
}
40+
return Parser.prototype.parseStatement.call(this, context, topLevel, exports)
41+
};
42+
43+
anonymous.prototype.parseExprAtom = function parseExprAtom (refDestructuringErrors) {
44+
if (this.type === tt._import) {
45+
return parseDynamicImport.call(this);
46+
}
47+
return Parser.prototype.parseExprAtom.call(this, refDestructuringErrors);
48+
};
49+
50+
return anonymous;
51+
}(Parser));
52+
};
53+
54+
module.exports = dynamicImport
55+
dynamicImport.DynamicImportKey = DynamicImportKey

lib/dynamic-import/source.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* eslint-disable no-underscore-dangle */
2+
3+
const DynamicImportKey = 'Import';
4+
5+
const { tokTypes: tt } = require('acorn')
6+
7+
// NOTE: This allows `yield import()` to parse correctly.
8+
tt._import.startsExpr = true;
9+
10+
function parseDynamicImport() {
11+
const node = this.startNode();
12+
this.next();
13+
if (this.type !== tt.parenL) {
14+
this.unexpected();
15+
}
16+
return this.finishNode(node, DynamicImportKey);
17+
}
18+
19+
function parenAfter() {
20+
return /^(\s|\/\/.*|\/\*[^]*?\*\/)*\(/.test(this.input.slice(this.pos));
21+
}
22+
23+
function dynamicImport(Parser) {
24+
return class extends Parser {
25+
parseStatement(context, topLevel, exports) {
26+
if (this.type === tt._import && parenAfter.call(this)) {
27+
return this.parseExpressionStatement(this.startNode(), this.parseExpression());
28+
}
29+
return super.parseStatement(context, topLevel, exports)
30+
}
31+
32+
parseExprAtom(refDestructuringErrors) {
33+
if (this.type === tt._import) {
34+
return parseDynamicImport.call(this);
35+
}
36+
return super.parseExprAtom(refDestructuringErrors);
37+
}
38+
};
39+
};
40+
41+
module.exports = dynamicImport
42+
dynamicImport.DynamicImportKey = DynamicImportKey

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
},
99
"dependencies": {
1010
"acorn": "^6.0.1",
11-
"acorn-dynamic-import": "^3.0.0",
1211
"acorn-walk": "^6.0.1",
1312
"xtend": "^4.0.1"
1413
},

walk.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
var xtend = require('xtend')
22
var walk = require('acorn-walk')
3+
var dynamicImportKey = require('./lib/dynamic-import').DynamicImportKey
34

4-
var base = xtend(walk.base, {
5-
Import: function () {}
6-
})
5+
var base = xtend(walk.base)
6+
base[dynamicImportKey] = function () {}
77

88
function simple (node, visitors, baseVisitor, state, override) {
99
return walk.simple(node, visitors, baseVisitor || base, state, override)

0 commit comments

Comments
 (0)