Skip to content

Commit 1a62362

Browse files
committed
Add import.meta support.
1 parent f8d9ab1 commit 1a62362

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ function compile (name, output) { // eslint-disable-line no-unused-vars
1717
fs.writeFileSync(path.join(__dirname, output), HEADER + result.code, 'utf8')
1818
}
1919

20-
console.log('Nothing to do right now!')
20+
compile('acorn-import-meta/inject', './lib/import-meta/index.js')

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ function mapOptions (opts) {
88
allowHashBang: true,
99
allowReturnOutsideFunction: true,
1010
plugins: {
11-
dynamicImport: opts.sourceType === 'module'
11+
dynamicImport: opts.sourceType === 'module',
12+
importMeta: opts.sourceType === 'module'
1213
}
1314
}, opts)
1415
opts.plugins = xtend(opts.plugins, {})
@@ -28,3 +29,4 @@ module.exports = exports = xtend(acorn, {
2829
})
2930

3031
require('acorn-dynamic-import/lib/inject').default(exports)
32+
require('./lib/import-meta')(exports)

lib/import-meta/index.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* Generated by `npm run build`, do not edit! */
2+
3+
"use strict"
4+
5+
var skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g
6+
7+
module.exports = function (acorn) {
8+
var tt = acorn.tokTypes
9+
10+
var nextTokenIsDot = function (parser) {
11+
skipWhiteSpace.lastIndex = parser.pos
12+
var skip = skipWhiteSpace.exec(parser.input)
13+
var next = parser.pos + skip[0].length
14+
return parser.input.slice(next, next + 1) === "."
15+
}
16+
17+
acorn.plugins.importMeta = function (instance) {
18+
19+
instance.extend("parseExprAtom", function (superF) {
20+
return function(refDestructuringErrors) {
21+
if (this.type !== tt._import || !nextTokenIsDot(this)) { return superF.call(this, refDestructuringErrors) }
22+
23+
if (!this.options.allowImportExportEverywhere && !this.inModule) {
24+
this.raise(this.start, "'import' and 'export' may appear only with 'sourceType: module'")
25+
}
26+
27+
var node = this.startNode()
28+
node.meta = this.parseIdent(true)
29+
this.expect(tt.dot)
30+
node.property = this.parseIdent(true)
31+
if (node.property.name !== "meta") {
32+
this.raiseRecoverable(node.property.start, "The only valid meta property for import is import.meta")
33+
}
34+
return this.finishNode(node, "MetaProperty")
35+
}
36+
})
37+
38+
instance.extend("parseStatement", function (superF) {
39+
return function(declaration, topLevel, exports) {
40+
if (this.type !== tt._import) { return superF.call(this, declaration, topLevel, exports) }
41+
if (!nextTokenIsDot(this)) { return superF.call(this, declaration, topLevel, exports) }
42+
43+
var node = this.startNode()
44+
var expr = this.parseExpression()
45+
return this.parseExpressionStatement(node, expr)
46+
}
47+
})
48+
}
49+
return acorn
50+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"xtend": "^4.0.1"
1313
},
1414
"devDependencies": {
15+
"acorn-import-meta": "^0.2.1",
1516
"buble": "^0.18.0",
1617
"mkdirp": "^0.5.1",
1718
"standard": "^10.0.3",

0 commit comments

Comments
 (0)