Skip to content

Commit 4f41182

Browse files
authored
feat: Use babel 7 parser and visitor (#15)
BREAKING CHANGE: the `plugins` in parser options has become an array, and we have to put `jsx` in the default value to support JSX syntax parsing. I did not add `typescript` because the fixture contains no typescript code, and `typescript` cannot co-exist with `flow`, not sure if we need to add it.
1 parent 7522323 commit 4f41182

File tree

5 files changed

+184
-11
lines changed

5 files changed

+184
-11
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ npm install --save konan
1717
- `require` call
1818
- `import/export` ES6 modules
1919
- `import()` [Dynamic import](https://github.com/tc39/proposal-dynamic-import)
20-
- You can use all language features supported by [babylon](https://github.com/babel/babylon), including `jsx` and `flow` syntax
20+
- You can use all language features supported by [babel parser](https://github.com/babel/babel/tree/master/packages/babel-parser), including `jsx` syntax by default
2121

2222
## Usage
2323

@@ -30,12 +30,12 @@ const vue = require('vue/dist/vue')
3030
import('./my-async-module').then()
3131
require(path.resolve('./'))
3232
`)
33-
/*
33+
/*
3434
result =>
3535
{
3636
strings: ['react', 'vue/dist/vue', './my-async-module'],
3737
expressions: ['path.resolve(\'./\')']
38-
}
38+
}
3939
*/
4040
```
4141

@@ -64,7 +64,7 @@ You can disable detecting dynamic `import()`-ed modules.
6464
Type: `object`<br>
6565
Default: `{sourceType: 'module', plugins: '*'}`
6666

67-
[babylon](https://github.com/babel/babylon) parse options.
67+
[babel parser](https://github.com/babel/babel/tree/master/packages/babel-parser) parse [options](https://babeljs.io/docs/en/next/babel-parser.html#options).
6868

6969
## FAQ
7070

index.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
const babylon = require('babylon')
2-
const traverse = require('babel-traverse').default
1+
const parser = require('@babel/parser')
2+
const traverse = require('@babel/traverse').default
33

44
module.exports = function(
55
src,
6-
{ dynamicImport = true, parse = { sourceType: 'module', plugins: '*' } } = {}
6+
{
7+
dynamicImport = true,
8+
parse = { sourceType: 'module', plugins: ['jsx'] }
9+
} = {}
710
) {
811
const modules = { strings: [], expressions: [] }
912

@@ -16,7 +19,7 @@ module.exports = function(
1619
return modules
1720
}
1821

19-
ast = babylon.parse(src, parse)
22+
ast = parser.parse(src, parse)
2023
} else {
2124
ast = src
2225
}

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
]
3535
},
3636
"dependencies": {
37-
"babel-traverse": "^6.23.1",
38-
"babylon": "^6.15.0"
37+
"@babel/parser": "^7.10.5",
38+
"@babel/traverse": "^7.10.5"
3939
}
4040
}

test/fixture.js

+2
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ var a = {
1919
}
2020

2121
const b = <div>jsx no-mad!</div>
22+
23+
const fragment = <></>

yarn.lock

+169-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,97 @@
22
# yarn lockfile v1
33

44

5+
"@babel/code-frame@^7.10.4":
6+
version "7.10.4"
7+
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.10.4.tgz#168da1a36e90da68ae8d49c0f1b48c7c6249213a"
8+
integrity sha512-vG6SvB6oYEhvgisZNFRmRCUkLz11c7rp+tbNTynGqc6mS1d5ATd/sGyV6W0KZZnXRKMTzZDRgQT3Ou9jhpAfUg==
9+
dependencies:
10+
"@babel/highlight" "^7.10.4"
11+
12+
"@babel/generator@^7.10.5":
13+
version "7.10.5"
14+
resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.10.5.tgz#1b903554bc8c583ee8d25f1e8969732e6b829a69"
15+
integrity sha512-3vXxr3FEW7E7lJZiWQ3bM4+v/Vyr9C+hpolQ8BGFr9Y8Ri2tFLWTixmwKBafDujO1WVah4fhZBeU1bieKdghig==
16+
dependencies:
17+
"@babel/types" "^7.10.5"
18+
jsesc "^2.5.1"
19+
source-map "^0.5.0"
20+
21+
"@babel/helper-function-name@^7.10.4":
22+
version "7.10.4"
23+
resolved "https://registry.yarnpkg.com/@babel/helper-function-name/-/helper-function-name-7.10.4.tgz#d2d3b20c59ad8c47112fa7d2a94bc09d5ef82f1a"
24+
integrity sha512-YdaSyz1n8gY44EmN7x44zBn9zQ1Ry2Y+3GTA+3vH6Mizke1Vw0aWDM66FOYEPw8//qKkmqOckrGgTYa+6sceqQ==
25+
dependencies:
26+
"@babel/helper-get-function-arity" "^7.10.4"
27+
"@babel/template" "^7.10.4"
28+
"@babel/types" "^7.10.4"
29+
30+
"@babel/helper-get-function-arity@^7.10.4":
31+
version "7.10.4"
32+
resolved "https://registry.yarnpkg.com/@babel/helper-get-function-arity/-/helper-get-function-arity-7.10.4.tgz#98c1cbea0e2332f33f9a4661b8ce1505b2c19ba2"
33+
integrity sha512-EkN3YDB+SRDgiIUnNgcmiD361ti+AVbL3f3Henf6dqqUyr5dMsorno0lJWJuLhDhkI5sYEpgj6y9kB8AOU1I2A==
34+
dependencies:
35+
"@babel/types" "^7.10.4"
36+
37+
"@babel/helper-split-export-declaration@^7.10.4":
38+
version "7.10.4"
39+
resolved "https://registry.yarnpkg.com/@babel/helper-split-export-declaration/-/helper-split-export-declaration-7.10.4.tgz#2c70576eaa3b5609b24cb99db2888cc3fc4251d1"
40+
integrity sha512-pySBTeoUff56fL5CBU2hWm9TesA4r/rOkI9DyJLvvgz09MB9YtfIYe3iBriVaYNaPe+Alua0vBIOVOLs2buWhg==
41+
dependencies:
42+
"@babel/types" "^7.10.4"
43+
44+
"@babel/helper-validator-identifier@^7.10.4":
45+
version "7.10.4"
46+
resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.10.4.tgz#a78c7a7251e01f616512d31b10adcf52ada5e0d2"
47+
integrity sha512-3U9y+43hz7ZM+rzG24Qe2mufW5KhvFg/NhnNph+i9mgCtdTCtMJuI1TMkrIUiK7Ix4PYlRF9I5dhqaLYA/ADXw==
48+
49+
"@babel/highlight@^7.10.4":
50+
version "7.10.4"
51+
resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.10.4.tgz#7d1bdfd65753538fabe6c38596cdb76d9ac60143"
52+
integrity sha512-i6rgnR/YgPEQzZZnbTHHuZdlE8qyoBNalD6F+q4vAFlcMEcqmkoG+mPqJYJCo63qPf74+Y1UZsl3l6f7/RIkmA==
53+
dependencies:
54+
"@babel/helper-validator-identifier" "^7.10.4"
55+
chalk "^2.0.0"
56+
js-tokens "^4.0.0"
57+
58+
"@babel/parser@^7.10.4", "@babel/parser@^7.10.5":
59+
version "7.10.5"
60+
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.10.5.tgz#e7c6bf5a7deff957cec9f04b551e2762909d826b"
61+
integrity sha512-wfryxy4bE1UivvQKSQDU4/X6dr+i8bctjUjj8Zyt3DQy7NtPizJXT8M52nqpNKL+nq2PW8lxk4ZqLj0fD4B4hQ==
62+
63+
"@babel/template@^7.10.4":
64+
version "7.10.4"
65+
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.10.4.tgz#3251996c4200ebc71d1a8fc405fba940f36ba278"
66+
integrity sha512-ZCjD27cGJFUB6nmCB1Enki3r+L5kJveX9pq1SvAUKoICy6CZ9yD8xO086YXdYhvNjBdnekm4ZnaP5yC8Cs/1tA==
67+
dependencies:
68+
"@babel/code-frame" "^7.10.4"
69+
"@babel/parser" "^7.10.4"
70+
"@babel/types" "^7.10.4"
71+
72+
"@babel/traverse@^7.10.5":
73+
version "7.10.5"
74+
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.10.5.tgz#77ce464f5b258be265af618d8fddf0536f20b564"
75+
integrity sha512-yc/fyv2gUjPqzTz0WHeRJH2pv7jA9kA7mBX2tXl/x5iOE81uaVPuGPtaYk7wmkx4b67mQ7NqI8rmT2pF47KYKQ==
76+
dependencies:
77+
"@babel/code-frame" "^7.10.4"
78+
"@babel/generator" "^7.10.5"
79+
"@babel/helper-function-name" "^7.10.4"
80+
"@babel/helper-split-export-declaration" "^7.10.4"
81+
"@babel/parser" "^7.10.5"
82+
"@babel/types" "^7.10.5"
83+
debug "^4.1.0"
84+
globals "^11.1.0"
85+
lodash "^4.17.19"
86+
87+
"@babel/types@^7.10.4", "@babel/types@^7.10.5":
88+
version "7.10.5"
89+
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.10.5.tgz#d88ae7e2fde86bfbfe851d4d81afa70a997b5d15"
90+
integrity sha512-ixV66KWfCI6GKoA/2H9v6bQdbfXEwwpOdQ8cRvb4F+eyvhlaHxWFMQB4+3d9QFJXZsiiiqVrewNV0DFEQpyT4Q==
91+
dependencies:
92+
"@babel/helper-validator-identifier" "^7.10.4"
93+
lodash "^4.17.19"
94+
to-fast-properties "^2.0.0"
95+
596
abab@^1.0.3:
697
version "1.0.3"
798
resolved "https://registry.npmjs.org/abab/-/abab-1.0.3.tgz#b81de5f7274ec4e756d797cd834f303642724e5d"
@@ -71,6 +162,13 @@ ansi-styles@^2.2.1:
71162
version "2.2.1"
72163
resolved "https://registry.npmjs.org/ansi-styles/-/ansi-styles-2.2.1.tgz#b432dd3358b634cf75e1e4664368240533c1ddbe"
73164

165+
ansi-styles@^3.2.1:
166+
version "3.2.1"
167+
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.1.tgz#41fbb20243e50b12be0f04b8dedbf07520ce841d"
168+
integrity sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==
169+
dependencies:
170+
color-convert "^1.9.0"
171+
74172
ansicolors@~0.2.1:
75173
version "0.2.1"
76174
resolved "https://registry.npmjs.org/ansicolors/-/ansicolors-0.2.1.tgz#be089599097b74a5c9c4a84a0cdbcdb62bd87aef"
@@ -278,7 +376,7 @@ babel-template@^6.16.0, babel-template@^6.22.0:
278376
babylon "^6.11.0"
279377
lodash "^4.2.0"
280378

281-
babel-traverse@^6.18.0, babel-traverse@^6.22.0, babel-traverse@^6.22.1, babel-traverse@^6.23.1:
379+
babel-traverse@^6.18.0, babel-traverse@^6.22.0, babel-traverse@^6.22.1:
282380
version "6.23.1"
283381
resolved "https://registry.npmjs.org/babel-traverse/-/babel-traverse-6.23.1.tgz#d3cb59010ecd06a97d81310065f966b699e14f48"
284382
dependencies:
@@ -443,6 +541,15 @@ chalk@^1.0.0, chalk@^1.1.0, chalk@^1.1.1, chalk@^1.1.3:
443541
strip-ansi "^3.0.0"
444542
supports-color "^2.0.0"
445543

544+
chalk@^2.0.0:
545+
version "2.4.2"
546+
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424"
547+
integrity sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==
548+
dependencies:
549+
ansi-styles "^3.2.1"
550+
escape-string-regexp "^1.0.5"
551+
supports-color "^5.3.0"
552+
446553
character-entities-legacy@^1.0.0:
447554
version "1.1.1"
448555
resolved "https://registry.npmjs.org/character-entities-legacy/-/character-entities-legacy-1.1.1.tgz#f40779df1a101872bb510a3d295e1fccf147202f"
@@ -518,6 +625,18 @@ collapse-white-space@^1.0.2:
518625
version "1.0.3"
519626
resolved "https://registry.npmjs.org/collapse-white-space/-/collapse-white-space-1.0.3.tgz#4b906f670e5a963a87b76b0e1689643341b6023c"
520627

628+
color-convert@^1.9.0:
629+
version "1.9.3"
630+
resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-1.9.3.tgz#bb71850690e1f136567de629d2d5471deda4c1e8"
631+
integrity sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==
632+
dependencies:
633+
color-name "1.1.3"
634+
635+
color-name@1.1.3:
636+
version "1.1.3"
637+
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25"
638+
integrity sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=
639+
521640
colors@1.0.3:
522641
version "1.0.3"
523642
resolved "https://registry.npmjs.org/colors/-/colors-1.0.3.tgz#0433f44d809680fdeb60ed260f1b0c262e82a40b"
@@ -646,6 +765,13 @@ debug@^2.1.1, debug@^2.2.0:
646765
dependencies:
647766
ms "0.7.2"
648767

768+
debug@^4.1.0:
769+
version "4.1.1"
770+
resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791"
771+
integrity sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==
772+
dependencies:
773+
ms "^2.1.1"
774+
649775
decamelize@^1.0.0, decamelize@^1.1.1, decamelize@^1.1.2:
650776
version "1.2.0"
651777
resolved "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
@@ -1241,6 +1367,11 @@ glob@^7.0.0, glob@^7.0.3, glob@^7.0.5:
12411367
once "^1.3.0"
12421368
path-is-absolute "^1.0.0"
12431369

1370+
globals@^11.1.0:
1371+
version "11.12.0"
1372+
resolved "https://registry.yarnpkg.com/globals/-/globals-11.12.0.tgz#ab8795338868a0babd8525758018c2a7eb95c42e"
1373+
integrity sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==
1374+
12441375
globals@^9.0.0, globals@^9.14.0:
12451376
version "9.14.0"
12461377
resolved "https://registry.npmjs.org/globals/-/globals-9.14.0.tgz#8859936af0038741263053b39d0e76ca241e4034"
@@ -1331,6 +1462,11 @@ has-flag@^2.0.0:
13311462
version "2.0.0"
13321463
resolved "https://registry.npmjs.org/has-flag/-/has-flag-2.0.0.tgz#e8207af1cc7b30d446cc70b734b5e8be18f88d51"
13331464

1465+
has-flag@^3.0.0:
1466+
version "3.0.0"
1467+
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd"
1468+
integrity sha1-tdRU3CGZriJWmfNGfloH87lVuv0=
1469+
13341470
has@^1.0.1:
13351471
version "1.0.1"
13361472
resolved "https://registry.npmjs.org/has/-/has-1.0.1.tgz#8461733f538b0837c9361e39a9ab9e9704dc2f28"
@@ -1919,6 +2055,11 @@ js-tokens@^3.0.0:
19192055
version "3.0.1"
19202056
resolved "https://registry.npmjs.org/js-tokens/-/js-tokens-3.0.1.tgz#08e9f132484a2c45a30907e9dc4d5567b7f114d7"
19212057

2058+
js-tokens@^4.0.0:
2059+
version "4.0.0"
2060+
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
2061+
integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==
2062+
19222063
js-types@^1.0.0:
19232064
version "1.0.0"
19242065
resolved "https://registry.npmjs.org/js-types/-/js-types-1.0.0.tgz#d242e6494ed572ad3c92809fc8bed7f7687cbf03"
@@ -1962,6 +2103,11 @@ jsesc@^1.3.0:
19622103
version "1.3.0"
19632104
resolved "https://registry.npmjs.org/jsesc/-/jsesc-1.3.0.tgz#46c3fec8c1892b12b0833db9bc7622176dbab34b"
19642105

2106+
jsesc@^2.5.1:
2107+
version "2.5.2"
2108+
resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-2.5.2.tgz#80564d2e483dacf6e8ef209650a67df3f0c283a4"
2109+
integrity sha512-OYu7XEzjkCQ3C5Ps3QIZsQfNpqoJyZZA99wd9aWd05NCtC5pWOkShK2mkL6HXQR6/Cy2lbNdPlZBpuQHXE63gA==
2110+
19652111
json-schema@0.2.3:
19662112
version "0.2.3"
19672113
resolved "https://registry.npmjs.org/json-schema/-/json-schema-0.2.3.tgz#b480c892e59a2f05954ce727bd3f2a4e882f9e13"
@@ -2148,6 +2294,11 @@ lodash@^4.0.0, lodash@^4.13.1, lodash@^4.14.0, lodash@^4.2.0, lodash@^4.3.0:
21482294
version "4.17.4"
21492295
resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
21502296

2297+
lodash@^4.17.19:
2298+
version "4.17.19"
2299+
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.19.tgz#e48ddedbe30b3321783c5b4301fbd353bc1e4a4b"
2300+
integrity sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ==
2301+
21512302
log-symbols@^1.0.2:
21522303
version "1.0.2"
21532304
resolved "https://registry.npmjs.org/log-symbols/-/log-symbols-1.0.2.tgz#376ff7b58ea3086a0f09facc74617eca501e1a18"
@@ -2285,6 +2436,11 @@ ms@0.7.2:
22852436
version "0.7.2"
22862437
resolved "https://registry.npmjs.org/ms/-/ms-0.7.2.tgz#ae25cf2512b3885a1d95d7f037868d8431124765"
22872438

2439+
ms@^2.1.1:
2440+
version "2.1.2"
2441+
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
2442+
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==
2443+
22882444
multimatch@^2.1.0:
22892445
version "2.1.0"
22902446
resolved "https://registry.npmjs.org/multimatch/-/multimatch-2.1.0.tgz#9c7906a22fb4c02919e2f5f75161b4cdbd4b2a2b"
@@ -3068,6 +3224,13 @@ supports-color@^3.1.2:
30683224
dependencies:
30693225
has-flag "^1.0.0"
30703226

3227+
supports-color@^5.3.0:
3228+
version "5.5.0"
3229+
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.5.0.tgz#e2e69a44ac8772f78a1ec0b35b689df6530efc8f"
3230+
integrity sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==
3231+
dependencies:
3232+
has-flag "^3.0.0"
3233+
30713234
symbol-tree@^3.2.1:
30723235
version "3.2.1"
30733236
resolved "https://registry.npmjs.org/symbol-tree/-/symbol-tree-3.2.1.tgz#8549dd1d01fa9f893c18cc9ab0b106b4d9b168cb"
@@ -3121,6 +3284,11 @@ to-fast-properties@^1.0.1:
31213284
version "1.0.2"
31223285
resolved "https://registry.npmjs.org/to-fast-properties/-/to-fast-properties-1.0.2.tgz#f3f5c0c3ba7299a7ef99427e44633257ade43320"
31233286

3287+
to-fast-properties@^2.0.0:
3288+
version "2.0.0"
3289+
resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-2.0.0.tgz#dc5e698cbd079265bc73e0377681a4e4e83f616e"
3290+
integrity sha1-3F5pjL0HkmW8c+A3doGk5Og/YW4=
3291+
31243292
tough-cookie@^2.3.2, tough-cookie@~2.3.0:
31253293
version "2.3.2"
31263294
resolved "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.3.2.tgz#f081f76e4c85720e6c37a5faced737150d84072a"

0 commit comments

Comments
 (0)