|
1 | 1 | var acorn = require('acorn')
|
2 | 2 | var xtend = require('xtend')
|
3 | 3 |
|
| 4 | +var CJSParser = acorn.Parser |
| 5 | +var ESModulesParser = acorn.Parser |
| 6 | + .extend(require('./lib/import-meta')) |
| 7 | + |
4 | 8 | function mapOptions (opts) {
|
5 | 9 | if (!opts) opts = {}
|
6 |
| - opts = xtend({ |
| 10 | + return xtend({ |
7 | 11 | ecmaVersion: 2019,
|
8 | 12 | allowHashBang: true,
|
9 |
| - allowReturnOutsideFunction: true, |
10 |
| - plugins: { |
11 |
| - dynamicImport: opts.sourceType === 'module', |
12 |
| - importMeta: opts.sourceType === 'module' |
13 |
| - } |
| 13 | + allowReturnOutsideFunction: true |
14 | 14 | }, opts)
|
15 |
| - opts.plugins = xtend(opts.plugins, {}) |
16 |
| - return opts |
| 15 | +} |
| 16 | + |
| 17 | +function getParser (opts) { |
| 18 | + if (!opts) opts = {} |
| 19 | + return opts.sourceType === 'module' ? ESModulesParser : CJSParser |
17 | 20 | }
|
18 | 21 |
|
19 | 22 | module.exports = exports = xtend(acorn, {
|
20 | 23 | parse: function parse (src, opts) {
|
21 |
| - return acorn.parse(src, mapOptions(opts)) |
| 24 | + return getParser(opts).parse(src, mapOptions(opts)) |
22 | 25 | },
|
23 | 26 | parseExpressionAt: function parseExpressionAt (src, offset, opts) {
|
24 |
| - return acorn.parseExpressionAt(src, offset, mapOptions(opts)) |
| 27 | + return getParser(opts).parseExpressionAt(src, offset, mapOptions(opts)) |
25 | 28 | },
|
26 | 29 | tokenizer: function tokenizer (src, opts) {
|
27 |
| - return acorn.tokenizer(src, mapOptions(opts)) |
| 30 | + return getParser(opts).tokenizer(src, mapOptions(opts)) |
28 | 31 | }
|
29 | 32 | })
|
30 |
| - |
31 |
| -require('acorn-dynamic-import/lib/inject').default(exports) |
32 |
| -require('./lib/import-meta')(exports) |
0 commit comments